source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/filters/ClassConstants.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 5.5 KB
Line 
1/*
2 * Copyright 2002,2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.filters;
18
19import java.io.IOException;
20import java.io.Reader;
21import java.lang.reflect.InvocationTargetException;
22import java.lang.reflect.Method;
23
24
25/**
26 * Assembles the constants declared in a Java class in
27 * <code>key1=value1(line separator)key2=value2</code>
28 * format.
29 *<p>
30 * Notes:
31 * <ol>
32 * <li>This filter uses the BCEL external toolkit.
33 * <li>This assembles only those constants that are not created
34 * using the syntax <code>new whatever()</code>
35 * <li>This assembles constants declared using the basic datatypes
36 * and String only.</li>
37 * <li>The access modifiers of the declared constants do not matter.</li>
38 *</ol>
39 * Example:<br>
40 * <pre>&lt;classconstants/&gt;</pre>
41 * Or:
42 * <pre>&lt;filterreader
43 * classname=&quot;org.apache.tools.ant.filters.ClassConstants&quot;/&gt;</pre>
44 */
45public final class ClassConstants
46 extends BaseFilterReader
47 implements ChainableReader {
48 /** Data that must be read from, if not null. */
49 private String queuedData = null;
50
51 /** Helper Class to be invoked via reflection. */
52 private static final String JAVA_CLASS_HELPER =
53 "org.apache.tools.ant.filters.util.JavaClassHelper";
54
55 /**
56 * Constructor for "dummy" instances.
57 *
58 * @see BaseFilterReader#BaseFilterReader()
59 */
60 public ClassConstants() {
61 super();
62 }
63
64 /**
65 * Creates a new filtered reader. The contents of the passed-in reader
66 * are expected to be the name of the class from which to produce a
67 * list of constants.
68 *
69 * @param in A Reader object providing the underlying stream.
70 * Must not be <code>null</code>.
71 */
72 public ClassConstants(final Reader in) {
73 super(in);
74 }
75
76 /**
77 * Reads and assembles the constants declared in a class file.
78 *
79 * @return the next character in the list of constants, or -1
80 * if the end of the resulting stream has been reached
81 *
82 * @exception IOException if the underlying stream throws an IOException
83 * during reading, or if the constants for the specified class cannot
84 * be read (for example due to the class not being found).
85 */
86 public final int read() throws IOException {
87
88 int ch = -1;
89
90 if (queuedData != null && queuedData.length() == 0) {
91 queuedData = null;
92 }
93
94 if (queuedData != null) {
95 ch = queuedData.charAt(0);
96 queuedData = queuedData.substring(1);
97 if (queuedData.length() == 0) {
98 queuedData = null;
99 }
100 } else {
101 final String clazz = readFully();
102 if (clazz == null) {
103 ch = -1;
104 } else {
105 final byte[] bytes = clazz.getBytes();
106 try {
107 final Class javaClassHelper =
108 Class.forName(JAVA_CLASS_HELPER);
109 if (javaClassHelper != null) {
110 final Class[] params = {
111 byte[].class
112 };
113 final Method getConstants =
114 javaClassHelper.getMethod("getConstants", params);
115 final Object[] args = {
116 bytes
117 };
118 // getConstants is a static method, no need to
119 // pass in the object
120 final StringBuffer sb = (StringBuffer)
121 getConstants.invoke(null, args);
122 if (sb.length() > 0) {
123 queuedData = sb.toString();
124 return read();
125 }
126 }
127 } catch (ClassNotFoundException cnfe) {
128 throw new IOException(cnfe.getMessage());
129 } catch (NoSuchMethodException nsme) {
130 throw new IOException(nsme.getMessage());
131 } catch (IllegalAccessException iae) {
132 throw new IOException(iae.getMessage());
133 } catch (IllegalArgumentException iarge) {
134 throw new IOException(iarge.getMessage());
135 } catch (InvocationTargetException ite) {
136 throw new IOException(ite.getMessage());
137 }
138 }
139 }
140 return ch;
141 }
142
143 /**
144 * Creates a new ClassConstants using the passed in
145 * Reader for instantiation.
146 *
147 * @param rdr A Reader object providing the underlying stream.
148 * Must not be <code>null</code>.
149 *
150 * @return a new filter based on this configuration, but filtering
151 * the specified reader
152 */
153 public final Reader chain(final Reader rdr) {
154 ClassConstants newFilter = new ClassConstants(rdr);
155 return newFilter;
156 }
157}
Note: See TracBrowser for help on using the repository browser.