001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 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 */
022
023package org.granite.generator.ant;
024
025import java.io.File;
026import java.lang.annotation.Annotation;
027import java.util.ArrayList;
028import java.util.List;
029
030import org.apache.tools.ant.BuildException;
031import org.apache.tools.ant.types.Parameter;
032import org.apache.tools.ant.types.selectors.BaseExtendSelector;
033
034/**
035 * @author Franck WOLFF
036 */
037public class ClassSelector extends BaseExtendSelector {
038
039        Class<? extends Annotation>[] annotations = null;
040        Class<?>[] superclasses = null;
041        Class<?>[] classes = null;
042        
043        @SuppressWarnings("unchecked")
044        private void initializeParameters() throws BuildException {
045                log("init...");
046                if (annotations == null) {
047                        List<Class<?>> annotationsList = new ArrayList<Class<?>>();
048                        List<Class<?>> superclassesList = new ArrayList<Class<?>>();
049                        List<Class<?>> classesList = new ArrayList<Class<?>>();
050
051                        for (Parameter parameter : getParameters()) {
052                                if ("annotatedwith".equalsIgnoreCase(parameter.getName())) {
053                                        try {
054                                                Class<?> annotation = Thread.currentThread().getContextClassLoader().loadClass(parameter.getValue());
055                                                annotationsList.add(annotation);
056                                        } catch (Exception e) {
057                                                throw new BuildException("Could not load annotation: " + parameter.getValue());
058                                        }
059                                }
060                                else if ("instanceof".equalsIgnoreCase(parameter.getName())) {
061                                        try {
062                                                Class<?> superclass = Thread.currentThread().getContextClassLoader().loadClass(parameter.getValue());
063                                                superclassesList.add(superclass);
064                                        } catch (Exception e) {
065                                                throw new BuildException("Could not load superclass: " + parameter.getValue());
066                                        }
067                                }
068                                else if ("type".equalsIgnoreCase(parameter.getName())) {
069                                        try {
070                                                Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(parameter.getValue());
071                                                classesList.add(clazz);
072                                        } catch (Exception e) {
073                                                throw new BuildException("Could not load type: " + parameter.getValue());
074                                        }
075                                }
076                                else
077                                        throw new BuildException("Illegal param name: " + parameter.getName());
078                        }
079
080                        annotations = (Class<? extends Annotation>[])annotationsList.toArray(new Class<?>[annotationsList.size()]);
081                        superclasses = superclassesList.toArray(new Class<?>[superclassesList.size()]);
082                        classes = classesList.toArray(new Class<?>[classesList.size()]);
083                }
084        }
085        
086        @Override
087        public boolean isSelected(File basedir, String fileName, File file) throws BuildException {
088                initializeParameters();
089                log(fileName);
090                if (!fileName.endsWith(".class"))
091                        return false;
092                
093                String className = fileName.substring(0, fileName.length() - 6).replace(File.separatorChar, '.');
094                try {
095                        Class<?> type = Thread.currentThread().getContextClassLoader().loadClass(className);
096                        for (Class<?> clazz : classes) {
097                                if (clazz.equals(type))
098                                        return true;
099                        }
100                        for (Class<?> superclass : superclasses) {
101                                if (superclass.isAssignableFrom(type))
102                                        return true;
103                        }
104                        for (Class<? extends Annotation> annotation : annotations) {
105                                if (type.isAnnotationPresent(annotation))
106                                        return true;
107                        }
108                } catch (Exception e) {
109                        throw new BuildException("Could not load class: " + className, e);
110                }
111                
112                return false;
113        }
114}