org.apache.bcel.classfile.InnerClasses Java Examples

The following examples show how to use org.apache.bcel.classfile.InnerClasses. 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: PreorderVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitInnerClasses(InnerClasses obj) {
    super.visitInnerClasses(obj);
    InnerClass[] inner_classes = obj.getInnerClasses();
    for (InnerClass inner_class : inner_classes) {
        inner_class.accept(this);
    }
}
 
Example #4
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitInnerClasses(final InnerClasses obj) {//vmspec2 4.7.5

    // exactly one InnerClasses attr per ClassFile if some inner class is refernced: see visitJavaClass()

    checkIndex(obj, obj.getNameIndex(), CONST_Utf8);

    final String name = ((ConstantUtf8) cp.getConstant(obj.getNameIndex())).getBytes();
    if (! name.equals("InnerClasses")) {
        throw new ClassConstraintException(
            "The InnerClasses attribute '"+tostring(obj)+"' is not correctly named 'InnerClasses' but '"+name+"'.");
    }

    final InnerClass[] ics = obj.getInnerClasses();

    for (final InnerClass ic : ics) {
        checkIndex(obj, ic.getInnerClassIndex(), CONST_Class);
        final int outer_idx = ic.getOuterClassIndex();
        if (outer_idx != 0) {
            checkIndex(obj, outer_idx, CONST_Class);
        }
        final int innername_idx = ic.getInnerNameIndex();
        if (innername_idx != 0) {
            checkIndex(obj, innername_idx, CONST_Utf8);
        }
        int acc = ic.getInnerAccessFlags();
        acc = acc & (~ (Const.ACC_PUBLIC | Const.ACC_PRIVATE | Const.ACC_PROTECTED |
                        Const.ACC_STATIC | Const.ACC_FINAL | Const.ACC_INTERFACE | Const.ACC_ABSTRACT));
        if (acc != 0) {
            addMessage(
                "Unknown access flag for inner class '"+tostring(ic)+"' set (InnerClasses attribute '"+tostring(obj)+"').");
        }
    }
    // Semantical consistency is not yet checked by Sun, see vmspec2 4.7.5.
    // [marked TODO in JustIce]
}
 
Example #5
Source File: BetterVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void visit(InnerClasses obj) {
    visit((Attribute) obj);
}
 
Example #6
Source File: BetterVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visitInnerClasses(InnerClasses obj) {
    visit(obj);
}
 
Example #7
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.");
    }
}
 
Example #8
Source File: StringRepresentation.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitInnerClasses(final InnerClasses obj) {
    tostring = toString(obj);
}
 
Example #9
Source File: CounterVisitor.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitInnerClasses(final InnerClasses obj)
{
    innerClassesCount++;
}