KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > filters > util > JavaClassHelper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.filters.util;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import org.apache.bcel.classfile.ClassParser;
23 import org.apache.bcel.classfile.ConstantValue;
24 import org.apache.bcel.classfile.Field;
25 import org.apache.bcel.classfile.JavaClass;
26
27 // CheckStyle:HideUtilityClassConstructorCheck OFF - bc
28
/**
29  * Helper class that filters constants from a Java Class
30  *
31  */

32 public final class JavaClassHelper {
33     /** System specific line separator. */
34     private static final String JavaDoc LS = System.getProperty("line.separator");
35
36     /**
37      * Get the constants declared in a file as name=value
38      *
39      * @param bytes the class as a array of bytes
40      * @return a StringBuffer contains the name=value pairs
41      * @exception IOException if an error occurs
42      */

43     public static StringBuffer JavaDoc getConstants(byte[] bytes)
44         throws IOException JavaDoc {
45         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
46         final ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(bytes);
47         final ClassParser parser = new ClassParser(bis, "");
48         final JavaClass javaClass = parser.parse();
49         final Field[] fields = javaClass.getFields();
50         for (int i = 0; i < fields.length; i++) {
51             final Field field = fields[i];
52             if (field != null) {
53                 final ConstantValue cv = field.getConstantValue();
54                 if (cv != null) {
55                     String JavaDoc cvs = cv.toString();
56                     //Remove start and end quotes if field is a String
57
if (cvs.startsWith("\"") && cvs.endsWith("\"")) {
58                         cvs = cvs.substring(1, cvs.length() - 1);
59                     }
60                     sb.append(field.getName());
61                     sb.append('=');
62                     sb.append(cvs);
63                     sb.append(LS);
64                 }
65             }
66         }
67         return sb;
68     }
69 }
70
Popular Tags