FileDocCategorySizeDatePackage
ClassConstants.javaAPI DocApache Ant 1.705843Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.filters

ClassConstants

public final class ClassConstants extends BaseFilterReader implements ChainableReader
Assembles the constants declared in a Java class in key1=value1(line separator)key2=value2 format.

Notes:

  1. This filter uses the BCEL external toolkit.
  2. This assembles only those constants that are not created using the syntax new whatever()
  3. This assembles constants declared using the basic datatypes and String only.
  4. 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
queuedData
Data that must be read from, if not null.
private static final String
JAVA_CLASS_HELPER
Helper Class to be invoked via reflection.
Constructors Summary
public ClassConstants()
Constructor for "dummy" instances.

see
BaseFilterReader#BaseFilterReader()


               
      
        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.

param
in A Reader object providing the underlying stream. Must not be null.

        super(in);
    
Methods Summary
public java.io.Readerchain(java.io.Reader rdr)
Creates a new ClassConstants using the passed in Reader for instantiation.

param
rdr A Reader object providing the underlying stream. Must not be null.
return
a new filter based on this configuration, but filtering the specified reader

        ClassConstants newFilter = new ClassConstants(rdr);
        return newFilter;
    
public intread()
Reads and assembles the constants declared in a class file.

return
the next character in the list of constants, or -1 if the end of the resulting stream has been reached
exception
IOException if the underlying stream throws an IOException during reading, or if the constants for the specified class cannot be read (for example due to the class not being found).


        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;