ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <Jan.Mate...@rzf.fin-nrw.de>
Subject AW: Building a <fileset> using annotations
Date Wed, 22 Oct 2008 05:52:44 GMT
There are no built in selectors. But you can write your own and plug in via <typedef>.
Here is a working example. But it has the drawback, that the *.class files must be in the
same directory as the *.java files (which is mostly NOT the case).


Jan


<project>
    <typedef name="deprecated" classname="AnnotationSelector" classpath="C:\TEMP\ec-test\AntSelector\bin"/>
    <fileset id="fs" dir="." includes="**/*.java">
        <deprecated/>
    </fileset>
    <echo>selected Files: ${toString:fs}</echo>
</project>

import java.io.File;
import java.lang.annotation.Annotation;
import java.net.URL;
import java.net.URLClassLoader;

public class AnnotationSelector extends org.apache.tools.ant.types.selectors.BaseSelector
{

	private static final Class<? extends Annotation> ANNOTATION_TO_SELECT = Deprecated.class;

	@Override
	public boolean isSelected(File basedir, String filename, File file) {
		try {
			return selectIfAnnotated(basedir, filename, file);
		} catch (ClassNotFoundException e) {
			System.err.print("Class '");
			System.err.print(e.getMessage());
			System.err.print("' could not be found. Are the BINARIES on the classpath? Therefore ");
			System.err.print(filename);
			System.err.println(" is deselected.");
			return false;
		} catch (Exception e) {
			return false;
		}
	}

	private boolean selectIfAnnotated(File basedir, String filename, File file) throws Exception
{
		// Create a ClassLoader for the filesets basedir which we can use for analyzing the specified
class.
		URLClassLoader loader = new URLClassLoader(new URL[]{basedir.toURI().toURL()});
		// Get a class name from the file name.
		String classname = filename.substring(0, filename.lastIndexOf('.')).replaceAll("\\\\", "\\.");
		// Load that class.
		Class<?> clazz = loader.loadClass(classname);
		// Check the presence of the annotation an select if annotated.
		return clazz.isAnnotationPresent(ANNOTATION_TO_SELECT);
	}

}

>-----Urspr√ľngliche Nachricht-----
>Von: news [mailto:news@ger.gmane.org] Im Auftrag von Chris
>Gesendet: Mittwoch, 22. Oktober 2008 02:48
>An: user@ant.apache.org
>Betreff: Building a <fileset> using annotations
>
>Is it possible in Ant to create a <fileset> that consists of 
>.java files 
>that have some annotation?
>
>For example, if I create a custom annotation, like "@published", and 
>attach it to several classes, is there some way to get a list of such 
>classes in a fileset?
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>For additional commands, e-mail: user-help@ant.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message