use of org.apache.bcel.classfile.Field 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.Field in project contribution by checkstyle.
the class HiddenInheritedFieldCheck method visitObject.
/**
* @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor
*/
public void visitObject(Object aJavaClass) {
final JavaClass javaClass = (JavaClass) aJavaClass;
final String className = javaClass.getClassName();
final JavaClass[] superClasses = javaClass.getSuperClasses();
final Field[] fields = javaClass.getFields();
// Check all fields
for (int i = 0; i < fields.length; i++) {
final Field field = fields[i];
// Go through all superclasses
for (int j = 0; j < superClasses.length; j++) {
final JavaClass superClass = superClasses[j];
final String superClassName = superClass.getClassName();
final Field[] superClassFields = superClass.getFields();
// Go through the filds of the superclasses
for (int k = 0; k < superClassFields.length; k++) {
final Field superClassField = superClassFields[k];
if (!superClassField.isPrivate() && superClassField.getName().equals(field.getName()) && !ignore(className, field)) {
log(javaClass, 0, "hidden.inherited.field", new Object[] { fields[i], superClassName });
}
}
}
}
}
use of org.apache.bcel.classfile.Field in project contribution by checkstyle.
the class UnreadFieldCheck method leaveSet.
/**
* @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor
*/
public void leaveSet(Set aJavaClasses) {
final Iterator it = aJavaClasses.iterator();
while (it.hasNext()) {
final JavaClass javaClass = (JavaClass) it.next();
final String className = javaClass.getClassName();
final JavaClassDefinition classDef = findJavaClassDef(javaClass);
final FieldDefinition[] fieldDefs = classDef.getFieldDefs();
for (int i = 0; i < fieldDefs.length; i++) {
if (fieldDefs[i].getReadReferenceCount() == 0) {
final Field field = fieldDefs[i].getField();
if (!field.isFinal() && (!ignore(className, field))) {
log(javaClass, 0, "unread.field", new Object[] { fieldDefs[i] });
}
}
}
}
}
use of org.apache.bcel.classfile.Field in project fb-contrib by mebigfatguy.
the class DubiousListCollection method getFieldAnnotation.
/**
* builds a field annotation by finding the field in the classes' field list
*
* @param fieldName
* the field for which to built the field annotation
*
* @return the field annotation of the specified field
*/
@Nullable
private FieldAnnotation getFieldAnnotation(final String fieldName) {
JavaClass cls = getClassContext().getJavaClass();
Field[] fields = cls.getFields();
for (Field f : fields) {
if (f.getName().equals(fieldName)) {
return new FieldAnnotation(cls.getClassName(), fieldName, f.getSignature(), f.isStatic());
}
}
// shouldn't happen
return null;
}
use of org.apache.bcel.classfile.Field in project fb-contrib by mebigfatguy.
the class FieldCouldBeLocal method visitClassContext.
/**
* overrides the visitor to collect localizable fields, and then report those that survive all method checks.
*
* @param classContext
* the context object that holds the JavaClass parsed
*/
@Override
public void visitClassContext(ClassContext classContext) {
try {
localizableFields = new HashMap<>();
visitedBlocks = new BitSet();
clsContext = classContext;
clsName = clsContext.getJavaClass().getClassName();
clsSig = SignatureUtils.classToSignature(clsName);
JavaClass cls = classContext.getJavaClass();
Field[] fields = cls.getFields();
ConstantPool cp = classContext.getConstantPoolGen().getConstantPool();
for (Field f : fields) {
if (!f.isStatic() && !f.isVolatile() && (f.getName().indexOf(Values.SYNTHETIC_MEMBER_CHAR) < 0) && f.isPrivate()) {
FieldAnnotation fa = new FieldAnnotation(cls.getClassName(), f.getName(), f.getSignature(), false);
boolean hasExternalAnnotation = false;
for (AnnotationEntry entry : f.getAnnotationEntries()) {
ConstantUtf8 cutf = (ConstantUtf8) cp.getConstant(entry.getTypeIndex());
if (!cutf.getBytes().startsWith(Values.JAVA)) {
hasExternalAnnotation = true;
break;
}
}
localizableFields.put(f.getName(), new FieldInfo(fa, hasExternalAnnotation));
}
}
if (!localizableFields.isEmpty()) {
buildMethodFieldModifiers(classContext);
super.visitClassContext(classContext);
for (FieldInfo fi : localizableFields.values()) {
FieldAnnotation fa = fi.getFieldAnnotation();
SourceLineAnnotation sla = fi.getSrcLineAnnotation();
BugInstance bug = new BugInstance(this, BugType.FCBL_FIELD_COULD_BE_LOCAL.name(), NORMAL_PRIORITY).addClass(this).addField(fa);
if (sla != null) {
bug.addSourceLine(sla);
}
bugReporter.reportBug(bug);
}
}
} finally {
localizableFields = null;
visitedBlocks = null;
clsContext = null;
methodFieldModifiers = null;
}
}
Aggregations