ClassConstantspublic final class ClassConstants extends BaseFilterReader implements ChainableReaderAssembles the constants declared in a Java class in
key1=value1(line separator)key2=value2
format.
Notes:
- This filter uses the BCEL external toolkit.
- This assembles only those constants that are not created
using the syntax
new whatever()
- This assembles constants declared using the basic datatypes
and String only.
- The access modifiers of the declared constants do not matter.
Example:
<classconstants/>
Or:
<filterreader
classname="org.apache.tools.ant.filters.ClassConstants"/> |
Fields Summary |
---|
private String | queuedDataData that must be read from, if not null. | private static final String | JAVA_CLASS_HELPERHelper Class to be invoked via reflection. |
Constructors Summary |
---|
public ClassConstants()Constructor for "dummy" instances.
super();
| public ClassConstants(Reader in)Creates a new filtered reader. The contents of the passed-in reader
are expected to be the name of the class from which to produce a
list of constants.
super(in);
|
Methods Summary |
---|
public java.io.Reader | chain(java.io.Reader rdr)Creates a new ClassConstants using the passed in
Reader for instantiation.
ClassConstants newFilter = new ClassConstants(rdr);
return newFilter;
| public int | read()Reads and assembles the constants declared in a class file.
int ch = -1;
if (queuedData != null && queuedData.length() == 0) {
queuedData = null;
}
if (queuedData != null) {
ch = queuedData.charAt(0);
queuedData = queuedData.substring(1);
if (queuedData.length() == 0) {
queuedData = null;
}
} else {
final String clazz = readFully();
if (clazz == null) {
ch = -1;
} else {
final byte[] bytes = clazz.getBytes("ISO-8859-1");
try {
final Class javaClassHelper =
Class.forName(JAVA_CLASS_HELPER);
if (javaClassHelper != null) {
final Class[] params = {
byte[].class
};
final Method getConstants =
javaClassHelper.getMethod("getConstants", params);
final Object[] args = {
bytes
};
// getConstants is a static method, no need to
// pass in the object
final StringBuffer sb = (StringBuffer)
getConstants.invoke(null, args);
if (sb.length() > 0) {
queuedData = sb.toString();
return read();
}
}
} catch (NoClassDefFoundError ex) {
throw ex;
} catch (RuntimeException ex) {
throw ex;
} catch (InvocationTargetException ex) {
Throwable t = ex.getTargetException();
if (t instanceof NoClassDefFoundError) {
throw (NoClassDefFoundError) t;
}
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
throw new BuildException(t);
} catch (Exception ex) {
throw new BuildException(ex);
}
}
}
return ch;
|
|