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;
}
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);
}
Aggregations