Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 195   Methods: 4
NCLOC: 73   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ClassConstants.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.filters;
 55   
 
 56   
 import java.io.IOException;
 57   
 import java.io.Reader;
 58   
 import java.lang.reflect.InvocationTargetException;
 59   
 import java.lang.reflect.Method;
 60   
 
 61   
 
 62   
 /**
 63   
  * Assembles the constants declared in a Java class in
 64   
  * <code>key1=value1(line separator)key2=value2</code>
 65   
  * format.
 66   
  *<p>
 67   
  * Notes:
 68   
  * <ol>
 69   
  * <li>This filter uses the BCEL external toolkit.
 70   
  * <li>This assembles only those constants that are not created
 71   
  * using the syntax <code>new whatever()</code>
 72   
  * <li>This assembles constants declared using the basic datatypes
 73   
  * and String only.</li>
 74   
  * <li>The access modifiers of the declared constants do not matter.</li>
 75   
  *</ol>
 76   
  * Example:<br>
 77   
  * <pre>&lt;classconstants/&gt;</pre>
 78   
  * Or:
 79   
  * <pre>&lt;filterreader classname=&quot;org.apache.tools.ant.filters.ClassConstants&quot;/&gt;</pre>
 80   
  * @author Magesh Umasankar
 81   
  */
 82   
 public final class ClassConstants
 83   
     extends BaseFilterReader
 84   
     implements ChainableReader {
 85   
     /** Data that must be read from, if not null. */
 86   
     private String queuedData = null;
 87   
 
 88   
     /** Helper Class to be invoked via reflection. */
 89   
     private static final String JAVA_CLASS_HELPER =
 90   
         "org.apache.tools.ant.filters.util.JavaClassHelper";
 91   
 
 92   
     /**
 93   
      * Constructor for "dummy" instances.
 94   
      * 
 95   
      * @see BaseFilterReader#BaseFilterReader()
 96   
      */
 97  0
     public ClassConstants() {
 98  0
         super();
 99   
     }
 100   
 
 101   
     /**
 102   
      * Creates a new filtered reader. The contents of the passed-in reader
 103   
      * are expected to be the name of the class from which to produce a 
 104   
      * list of constants.
 105   
      *
 106   
      * @param in A Reader object providing the underlying stream.
 107   
      *           Must not be <code>null</code>.
 108   
      */
 109  0
     public ClassConstants(final Reader in) {
 110  0
         super(in);
 111   
     }
 112   
 
 113   
     /**
 114   
      * Reads and assembles the constants declared in a class file.
 115   
      * 
 116   
      * @return the next character in the list of constants, or -1
 117   
      * if the end of the resulting stream has been reached
 118   
      * 
 119   
      * @exception IOException if the underlying stream throws an IOException
 120   
      * during reading, or if the constants for the specified class cannot
 121   
      * be read (for example due to the class not being found).
 122   
      */
 123  0
     public final int read() throws IOException {
 124   
 
 125  0
         int ch = -1;
 126   
 
 127  0
         if (queuedData != null && queuedData.length() == 0) {
 128  0
             queuedData = null;
 129   
         }
 130   
 
 131  0
         if (queuedData != null) {
 132  0
             ch = queuedData.charAt(0);
 133  0
             queuedData = queuedData.substring(1);
 134  0
             if (queuedData.length() == 0) {
 135  0
                 queuedData = null;
 136   
             }
 137   
         } else {
 138  0
             final String clazz = readFully();
 139  0
             if (clazz == null) {
 140  0
                 ch = -1;
 141   
             } else {
 142  0
                 final byte[] bytes = clazz.getBytes();
 143  0
                 try {
 144  0
                     final Class javaClassHelper =
 145   
                         Class.forName(JAVA_CLASS_HELPER);
 146  0
                     if (javaClassHelper != null) {
 147  0
                         final Class params[] = {
 148   
                             byte[].class
 149   
                         };
 150  0
                         final Method getConstants =
 151   
                             javaClassHelper.getMethod("getConstants", params);
 152  0
                         final Object[] args = {
 153   
                             bytes
 154   
                         };
 155   
                         // getConstants is a static method, no need to
 156   
                         // pass in the object
 157  0
                         final StringBuffer sb = (StringBuffer)
 158   
                                 getConstants.invoke(null, args);
 159  0
                         if (sb.length() > 0) {
 160  0
                             queuedData = sb.toString();
 161  0
                             return read();
 162   
                         }
 163   
                     }
 164   
                 } catch (ClassNotFoundException cnfe) {
 165  0
                     throw new IOException(cnfe.getMessage());
 166   
                 } catch (NoSuchMethodException nsme) {
 167  0
                     throw new IOException(nsme.getMessage());
 168   
                 } catch (IllegalAccessException iae) {
 169  0
                     throw new IOException(iae.getMessage());
 170   
                 } catch (IllegalArgumentException iarge) {
 171  0
                     throw new IOException(iarge.getMessage());
 172   
                 } catch (InvocationTargetException ite) {
 173  0
                     throw new IOException(ite.getMessage());
 174   
                 }
 175   
             }
 176   
         }
 177  0
         return ch;
 178   
     }
 179   
 
 180   
     /**
 181   
      * Creates a new ClassConstants using the passed in
 182   
      * Reader for instantiation.
 183   
      * 
 184   
      * @param rdr A Reader object providing the underlying stream.
 185   
      *            Must not be <code>null</code>.
 186   
      * 
 187   
      * @return a new filter based on this configuration, but filtering
 188   
      *         the specified reader
 189   
      */
 190  0
     public final Reader chain(final Reader rdr) {
 191  0
         ClassConstants newFilter = new ClassConstants(rdr);
 192  0
         return newFilter;
 193   
     }
 194   
 }
 195