001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2014 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.generator.ant;
023
024import java.io.File;
025import java.lang.annotation.Annotation;
026import java.util.ArrayList;
027import java.util.List;
028
029import org.apache.tools.ant.BuildException;
030import org.apache.tools.ant.types.Parameter;
031import org.apache.tools.ant.types.selectors.BaseExtendSelector;
032
033/**
034 * @author Franck WOLFF
035 */
036public class ClassSelector extends BaseExtendSelector {
037
038        Class<? extends Annotation>[] annotations = null;
039        Class<?>[] superclasses = null;
040        Class<?>[] classes = null;
041        
042        @SuppressWarnings("unchecked")
043        private void initializeParameters() throws BuildException {
044                log("init...");
045                if (annotations == null) {
046                        List<Class<?>> annotationsList = new ArrayList<Class<?>>();
047                        List<Class<?>> superclassesList = new ArrayList<Class<?>>();
048                        List<Class<?>> classesList = new ArrayList<Class<?>>();
049
050                        for (Parameter parameter : getParameters()) {
051                                if ("annotatedwith".equalsIgnoreCase(parameter.getName())) {
052                                        try {
053                                                Class<?> annotation = Thread.currentThread().getContextClassLoader().loadClass(parameter.getValue());
054                                                annotationsList.add(annotation);
055                                        } catch (Exception e) {
056                                                throw new BuildException("Could not load annotation: " + parameter.getValue());
057                                        }
058                                }
059                                else if ("instanceof".equalsIgnoreCase(parameter.getName())) {
060                                        try {
061                                                Class<?> superclass = Thread.currentThread().getContextClassLoader().loadClass(parameter.getValue());
062                                                superclassesList.add(superclass);
063                                        } catch (Exception e) {
064                                                throw new BuildException("Could not load superclass: " + parameter.getValue());
065                                        }
066                                }
067                                else if ("type".equalsIgnoreCase(parameter.getName())) {
068                                        try {
069                                                Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(parameter.getValue());
070                                                classesList.add(clazz);
071                                        } catch (Exception e) {
072                                                throw new BuildException("Could not load type: " + parameter.getValue());
073                                        }
074                                }
075                                else
076                                        throw new BuildException("Illegal param name: " + parameter.getName());
077                        }
078
079                        annotations = (Class<? extends Annotation>[])annotationsList.toArray(new Class<?>[annotationsList.size()]);
080                        superclasses = superclassesList.toArray(new Class<?>[superclassesList.size()]);
081                        classes = classesList.toArray(new Class<?>[classesList.size()]);
082                }
083        }
084        
085        @Override
086        public boolean isSelected(File basedir, String fileName, File file) throws BuildException {
087                initializeParameters();
088                log(fileName);
089                if (!fileName.endsWith(".class"))
090                        return false;
091                
092                String className = fileName.substring(0, fileName.length() - 6).replace(File.separatorChar, '.');
093                try {
094                        Class<?> type = Thread.currentThread().getContextClassLoader().loadClass(className);
095                        for (Class<?> clazz : classes) {
096                                if (clazz.equals(type))
097                                        return true;
098                        }
099                        for (Class<?> superclass : superclasses) {
100                                if (superclass.isAssignableFrom(type))
101                                        return true;
102                        }
103                        for (Class<? extends Annotation> annotation : annotations) {
104                                if (type.isAnnotationPresent(annotation))
105                                        return true;
106                        }
107                } catch (Exception e) {
108                        throw new BuildException("Could not load class: " + className, e);
109                }
110                
111                return false;
112        }
113}