Search in sources :

Example 1 with ConstantValue

use of org.apache.bcel.classfile.ConstantValue in project ant by apache.

the class JavaClassHelper method getConstants.

/**
 * Get the constants declared in a file as name=value
 *
 * @param bytes the class as a array of bytes
 * @return a StringBuffer contains the name=value pairs
 * @exception IOException if an error occurs
 */
public static StringBuffer getConstants(final byte[] bytes) throws IOException {
    final StringBuffer sb = new StringBuffer();
    final ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    final ClassParser parser = new ClassParser(bis, "");
    final JavaClass javaClass = parser.parse();
    final Field[] fields = javaClass.getFields();
    for (final Field field : fields) {
        if (field != null) {
            final ConstantValue cv = field.getConstantValue();
            if (cv != null) {
                String cvs = cv.toString();
                // Remove start and end quotes if field is a String
                if (cvs.startsWith("\"") && cvs.endsWith("\"")) {
                    cvs = cvs.substring(1, cvs.length() - 1);
                }
                sb.append(field.getName());
                sb.append('=');
                sb.append(cvs);
                sb.append(LS);
            }
        }
    }
    return sb;
}
Also used : Field(org.apache.bcel.classfile.Field) JavaClass(org.apache.bcel.classfile.JavaClass) ByteArrayInputStream(java.io.ByteArrayInputStream) ConstantValue(org.apache.bcel.classfile.ConstantValue) ClassParser(org.apache.bcel.classfile.ClassParser)

Example 2 with ConstantValue

use of org.apache.bcel.classfile.ConstantValue in project fb-contrib by mebigfatguy.

the class SillynessPotPourri method looksLikeStaticFieldValue.

private boolean looksLikeStaticFieldValue(String constant) {
    if (staticConstants == null) {
        staticConstants = new HashSet<>();
        Field[] fields = getClassContext().getJavaClass().getFields();
        for (Field f : fields) {
            if (((f.getAccessFlags() & (Const.ACC_FINAL | Const.ACC_STATIC)) == (Const.ACC_FINAL | Const.ACC_STATIC)) && Values.SIG_JAVA_LANG_STRING.equals(f.getSignature())) {
                ConstantValue cv = f.getConstantValue();
                if (cv != null) {
                    int cvIndex = cv.getConstantValueIndex();
                    staticConstants.add(getConstantPool().getConstantString(cvIndex, Const.CONSTANT_String));
                }
            }
        }
    }
    return staticConstants.contains(constant);
}
Also used : Field(org.apache.bcel.classfile.Field) XField(edu.umd.cs.findbugs.ba.XField) ConstantValue(org.apache.bcel.classfile.ConstantValue)

Aggregations

ConstantValue (org.apache.bcel.classfile.ConstantValue)2 Field (org.apache.bcel.classfile.Field)2 XField (edu.umd.cs.findbugs.ba.XField)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ClassParser (org.apache.bcel.classfile.ClassParser)1 JavaClass (org.apache.bcel.classfile.JavaClass)1