Java Code Examples for org.apache.bcel.classfile.Constant#getTag()

The following examples show how to use org.apache.bcel.classfile.Constant#getTag() . 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: ClassDumper.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the indices of all class symbol references to {@code targetClassName} in the constant
 * pool of {@code sourceJavaClass}.
 */
ImmutableSet<Integer> constantPoolIndexForClass(
    JavaClass sourceJavaClass, String targetClassName) {
  ImmutableSet.Builder<Integer> constantPoolIndicesForTarget = ImmutableSet.builder();

  ConstantPool sourceConstantPool = sourceJavaClass.getConstantPool();
  Constant[] constantPool = sourceConstantPool.getConstantPool();
  // constantPool indexes start from 1. 0th entry is null.
  for (int poolIndex = 1; poolIndex < constantPool.length; poolIndex++) {
    Constant constant = constantPool[poolIndex];
    if (constant != null) {
      byte constantTag = constant.getTag();
      if (constantTag == Const.CONSTANT_Class) {
        ConstantClass constantClass = (ConstantClass) constant;
        ClassSymbol classSymbol = makeSymbol(constantClass, sourceConstantPool, sourceJavaClass);
        if (targetClassName.equals(classSymbol.getClassBinaryName())) {
          constantPoolIndicesForTarget.add(poolIndex);
        }
      }
    }
  }

  return constantPoolIndicesForTarget.build();
}
 
Example 2
Source File: listclass.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
public static String[] getClassDependencies(final ConstantPool pool) {
    final String[] tempArray = new String[pool.getLength()];
    int size = 0;
    final StringBuilder buf = new StringBuilder();

    for (int idx = 0; idx < pool.getLength(); idx++) {
        final Constant c = pool.getConstant(idx);
        if (c != null && c.getTag() == Constants.CONSTANT_Class) {
            final ConstantUtf8 c1 = (ConstantUtf8) pool.getConstant(((ConstantClass) c).getNameIndex());
            buf.setLength(0);
            buf.append(c1.getBytes());
            for (int n = 0; n < buf.length(); n++) {
                if (buf.charAt(n) == '/') {
                    buf.setCharAt(n, '.');
                }
            }

            tempArray[size++] = buf.toString();
        }
    }

    final String[] dependencies = new String[size];
    System.arraycopy(tempArray, 0, dependencies, 0, size);
    return dependencies;
}
 
Example 3
Source File: Package.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Add this class to allClasses. Then go through all its dependents
 * and add them to the dependents list if they are not in allClasses
 */
void addDependents(final JavaClass clazz) throws IOException {
    final String name = clazz.getClassName().replace('.', '/');
    allClasses.put(name, clazz);
    final ConstantPool pool = clazz.getConstantPool();
    for (int i = 1; i < pool.getLength(); i++) {
        final Constant cons = pool.getConstant(i);
        //System.out.println("("+i+") " + cons );
        if (cons != null && cons.getTag() == Constants.CONSTANT_Class) {
            final int idx = ((ConstantClass) pool.getConstant(i)).getNameIndex();
            final String clas = ((ConstantUtf8) pool.getConstant(idx)).getBytes();
            addClassString(clas, name);
        }
    }
}
 
Example 4
Source File: ClassDumper.java    From cloud-opensource-java with Apache License 2.0 4 votes vote down vote up
private static SymbolReferences.Builder findSymbolReferences(
    ClassFile source, JavaClass javaClass) {
  SymbolReferences.Builder builder = new SymbolReferences.Builder();

  ConstantPool constantPool = javaClass.getConstantPool();
  Constant[] constants = constantPool.getConstantPool();
  for (Constant constant : constants) {
    if (constant == null) {
       continue;
    }

    byte constantTag = constant.getTag();
    switch (constantTag) {
      case Const.CONSTANT_Class:
        ConstantClass constantClass = (ConstantClass) constant;
        ClassSymbol classSymbol = makeSymbol(constantClass, constantPool, javaClass);
        // skip array class because it is provided by runtime
        if (classSymbol.getClassBinaryName().startsWith("[")) {
          break;
        }
        builder.addClassReference(source, classSymbol);
        break;
      case Const.CONSTANT_Methodref:
      case Const.CONSTANT_InterfaceMethodref:
        // Both ConstantMethodref and ConstantInterfaceMethodref are subclass of ConstantCP
        ConstantCP constantMethodref = (ConstantCP) constant;
        builder.addMethodReference(source, makeSymbol(constantMethodref, constantPool));
        break;
      case Const.CONSTANT_Fieldref:
        ConstantFieldref constantFieldref = (ConstantFieldref) constant;
        builder.addFieldReference(source, makeSymbol(constantFieldref, constantPool));
        break;
      default:
        break;
    }
  }

  for (String interfaceName : javaClass.getInterfaceNames()) {
    builder.addClassReference(source, new InterfaceSymbol(interfaceName));
  }

  return builder;
}
 
Example 5
Source File: ClassModel.java    From aparapi with Apache License 2.0 4 votes vote down vote up
public ConstantPool(JavaClass cls) {
    org.apache.bcel.classfile.ConstantPool cp = cls.getConstantPool();
   final int size = cp.getLength();
   add(new EmptyEntry(0)); // slot 0

   for (int i = 1; i < size; i++) {
      final Constant constant = cp.getConstant(i);
      if (constant != null) {
          final byte tag = constant.getTag();
          switch (tag) {
              case Const.CONSTANT_Class:
                  final int ni = ((ConstantClass) constant).getNameIndex();
                  add(new ClassEntry(ni, i));
                  break;
              case Const.CONSTANT_String:
                  final int si = ((ConstantString) constant).getStringIndex();
                  add(new StringEntry(si, i));
                  break;
              case Const.CONSTANT_Utf8:
                  add(new UTF8Entry(((ConstantUtf8) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_Double:
                  add(new DoubleEntry(((ConstantDouble) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_Float:
                  add(new FloatEntry(((ConstantFloat) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_Long:
                  add(new LongEntry(((ConstantLong) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_Integer:
                  add(new IntegerEntry(((ConstantInteger) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_NameAndType:
                  final int ntInd = ((ConstantNameAndType) constant).getNameIndex();
                  final int siNT = ((ConstantNameAndType) constant).getSignatureIndex();
                  add(new NameAndTypeEntry(ntInd, siNT, i));
                  break;
              case Const.CONSTANT_InterfaceMethodref:
                  final int refClassIndexIMR = ((ConstantCP) constant).getClassIndex();
                  final int nameAndTypeIndIMR = ((ConstantCP) constant).getNameAndTypeIndex();
                  add(new InterfaceMethodEntry(refClassIndexIMR, nameAndTypeIndIMR, i));
                  break;
              case Const.CONSTANT_Methodref:
                  final int refClassIndexM = ((ConstantCP) constant).getClassIndex();
                  final int nameAndTypeIndM = ((ConstantCP) constant).getNameAndTypeIndex();
                  add(new MethodEntry(refClassIndexM, nameAndTypeIndM, i));
                  break;
              case Const.CONSTANT_Fieldref:
                  final int refClassIndex = ((ConstantCP) constant).getClassIndex();
                  final int nameAndTypeInd = ((ConstantCP) constant).getNameAndTypeIndex();
                  add(new FieldEntry(refClassIndex, nameAndTypeInd, i));
                  break;
              case Const.CONSTANT_MethodHandle:
                  final ConstantMethodHandle cmh = (ConstantMethodHandle) constant;
                  final int refKind = cmh.getReferenceKind();
                  final int refInd = cmh.getReferenceIndex();
                  add(new MethodHandleEntry(refKind, refInd, i));
                  break;
              case Const.CONSTANT_MethodType:
                  final ConstantMethodType cmt = (ConstantMethodType) constant;
                  final int descInd = cmt.getDescriptorIndex();
                  add(new MethodTypeEntry(descInd, i));
                  break;
              case Const.CONSTANT_InvokeDynamic:
                  final ConstantInvokeDynamic cid = (ConstantInvokeDynamic) constant;
                  final int bsMethAttInd = cid.getBootstrapMethodAttrIndex();
                  final int ntIndID = cid.getNameAndTypeIndex();
                  add(new InvokeDynamicEntry(bsMethAttInd, ntIndID, i));
                  break;
              default:
                  System.out.printf("slot %04x unexpected Constant constantPoolType = %s\n", i, constant);
          }
      } else {
          add(new EmptyEntry(i));
      }
   }
}