Java Code Examples for org.apache.bcel.classfile.JavaClass#getAttributes()

The following examples show how to use org.apache.bcel.classfile.JavaClass#getAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Util.java    From spotbugs with GNU Lesser General Public License v2.1 7 votes vote down vote up
/**
 * Determine the outer class of obj.
 *
 * @param obj
 * @return JavaClass for outer class, or null if obj is not an outer class
 * @throws ClassNotFoundException
 */

@CheckForNull
public static JavaClass getOuterClass(JavaClass obj) throws ClassNotFoundException {
    for (Attribute a : obj.getAttributes()) {
        if (a instanceof InnerClasses) {
            for (InnerClass ic : ((InnerClasses) a).getInnerClasses()) {
                if (obj.getClassNameIndex() == ic.getInnerClassIndex()) {
                    // System.out.println("Outer class is " +
                    // ic.getOuterClassIndex());
                    ConstantClass oc = (ConstantClass) obj.getConstantPool().getConstant(ic.getOuterClassIndex());
                    String ocName = oc.getBytes(obj.getConstantPool());
                    return Repository.lookupClass(ocName);
                }
            }
        }
    }
    return null;
}
 
Example 2
Source File: ClassDumper.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
static ImmutableSet<String> listInnerClassNames(JavaClass javaClass) {
  ImmutableSet.Builder<String> innerClassNames = ImmutableSet.builder();
  String topLevelClassName = javaClass.getClassName();
  ConstantPool constantPool = javaClass.getConstantPool();
  for (Attribute attribute : javaClass.getAttributes()) {
    if (attribute.getTag() == Const.ATTR_INNER_CLASSES) {
      // This innerClasses variable does not include double-nested inner classes
      InnerClasses innerClasses = (InnerClasses) attribute;
      for (InnerClass innerClass : innerClasses.getInnerClasses()) {
        int classIndex = innerClass.getInnerClassIndex();
        String innerClassName = constantPool.getConstantString(classIndex, Const.CONSTANT_Class);
        int outerClassIndex = innerClass.getOuterClassIndex();
        if (outerClassIndex > 0) {
          String outerClassName =
              constantPool.getConstantString(outerClassIndex, Const.CONSTANT_Class);
          String normalOuterClassName = outerClassName.replace('/', '.');
          if (!normalOuterClassName.equals(topLevelClassName)) {
            continue;
          }
        }

        // Class names stored in constant pool have '/' as separator. We want '.' (as binary name)
        String normalInnerClassName = innerClassName.replace('/', '.');
        innerClassNames.add(normalInnerClassName);
      }
    }
  }
  return innerClassNames.build();
}
 
Example 3
Source File: DumbMethods.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visit(JavaClass obj) {
    String superclassName = obj.getSuperclassName();
    isSynthetic = "java.rmi.server.RemoteStub".equals(superclassName);
    Attribute[] attributes = obj.getAttributes();
    if (attributes != null) {
        for (Attribute a : attributes) {
            if (a instanceof Synthetic) {
                isSynthetic = true;
            }
        }
    }

}
 
Example 4
Source File: UnreadFields.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static boolean classHasParameter(JavaClass obj) {
    for (Attribute a : obj.getAttributes()) {
        if (a instanceof Signature) {
            String sig = ((Signature) a).getSignature();
            return sig.charAt(0) == '<';
        }
    }
    return false;
}
 
Example 5
Source File: BCELUtil.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static boolean isSynthetic(JavaClass j) {
    if (j.isSynthetic()) {
        return true;
    }

    for (Attribute a : j.getAttributes()) {
        if (a instanceof Synthetic) {
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: ClassGen.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize with existing class.
 * @param clazz JavaClass object (e.g. read from file)
 */
public ClassGen(final JavaClass clazz) {
    super(clazz.getAccessFlags());
    classNameIndex = clazz.getClassNameIndex();
    superclass_name_index = clazz.getSuperclassNameIndex();
    className = clazz.getClassName();
    superClassName = clazz.getSuperclassName();
    fileName = clazz.getSourceFileName();
    cp = new ConstantPoolGen(clazz.getConstantPool());
    major = clazz.getMajor();
    minor = clazz.getMinor();
    final Attribute[] attributes = clazz.getAttributes();
    // J5TODO: Could make unpacking lazy, done on first reference
    final AnnotationEntryGen[] annotations = unpackAnnotations(attributes);
    final Method[] methods = clazz.getMethods();
    final Field[] fields = clazz.getFields();
    final String[] interfaces = clazz.getInterfaceNames();
    for (final String interface1 : interfaces) {
        addInterface(interface1);
    }
    for (final Attribute attribute : attributes) {
        if (!(attribute instanceof Annotations)) {
            addAttribute(attribute);
        }
    }
    for (final AnnotationEntryGen annotation : annotations) {
        addAnnotationEntry(annotation);
    }
    for (final Method method : methods) {
        addMethod(method);
    }
    for (final Field field : fields) {
        addField(field);
    }
}
 
Example 7
Source File: AbstractTestCase.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
protected Attribute[] findAttribute(final String name, final JavaClass clazz)
{
    final Attribute[] all = clazz.getAttributes();
    final List<Attribute> chosenAttrsList = new ArrayList<>();
    for (final Attribute element : all) {
        if (verbose) {
            System.err.println("Attribute: " + element.getName());
        }
        if (element.getName().equals(name)) {
            chosenAttrsList.add(element);
        }
    }
    return chosenAttrsList.toArray(new Attribute[] {});
}
 
Example 8
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitJavaClass(final JavaClass obj) {
    final Attribute[] atts = obj.getAttributes();
    boolean foundSourceFile = false;
    boolean foundInnerClasses = false;

    // Is there an InnerClass referenced?
    // This is a costly check; existing verifiers don't do it!
    final boolean hasInnerClass = new InnerClassDetector(jc).innerClassReferenced();

    for (final Attribute att : atts) {
        if ((!(att instanceof SourceFile)) &&
                (!(att instanceof Deprecated)) &&
                (!(att instanceof InnerClasses)) &&
                (!(att instanceof Synthetic))) {
            addMessage("Attribute '" + tostring(att) + "' as an attribute of the ClassFile structure '" +
                tostring(obj) + "' is unknown and will therefore be ignored.");
        }

        if (att instanceof SourceFile) {
            if (!foundSourceFile) {
                foundSourceFile = true;
            } else {
                throw new ClassConstraintException("A ClassFile structure (like '" +
                    tostring(obj) + "') may have no more than one SourceFile attribute."); //vmspec2 4.7.7
            }
        }

        if (att instanceof InnerClasses) {
            if (!foundInnerClasses) {
                foundInnerClasses = true;
            } else {
                if (hasInnerClass) {
                    throw new ClassConstraintException("A Classfile structure (like '" + tostring(obj) +
                        "') must have exactly one InnerClasses attribute"+
                        " if at least one Inner Class is referenced (which is the case)."+
                        " More than one InnerClasses attribute was found.");
                }
            }
            if (!hasInnerClass) {
                addMessage("No referenced Inner Class found, but InnerClasses attribute '" + tostring(att) +
                    "' found. Strongly suggest removal of that attribute.");
            }
        }

    }
    if (hasInnerClass && !foundInnerClasses) {
        //throw new ClassConstraintException("A Classfile structure (like '"+tostring(obj)+
        // "') must have exactly one InnerClasses attribute if at least one Inner Class is referenced (which is the case)."+
        // " No InnerClasses attribute was found.");
        //vmspec2, page 125 says it would be a constraint: but existing verifiers
        //don't check it and javac doesn't satisfy it when it comes to anonymous
        //inner classes
        addMessage("A Classfile structure (like '"+tostring(obj)+
            "') must have exactly one InnerClasses attribute if at least one Inner Class is referenced (which is the case)."+
                " No InnerClasses attribute was found.");
    }
}