ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <>
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).


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

import java.lang.annotation.Annotation;

public class AnnotationSelector extends

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

	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("' could not be found. Are the BINARIES on the classpath? Therefore ");
			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
		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 [] Im Auftrag von Chris
>Gesendet: Mittwoch, 22. Oktober 2008 02:48
>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:
>For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message