org.apache.bcel.classfile.ConstantInterfaceMethodref Java Examples

The following examples show how to use org.apache.bcel.classfile.ConstantInterfaceMethodref. 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: PreorderVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns true if given constant pool probably has a reference to any of supplied methods
 * Useful to exclude from analysis uninteresting classes
 * @param cp constant pool
 * @param methods methods collection
 * @return true if method is found
 */
public static boolean hasInterestingMethod(ConstantPool cp, Collection<MethodDescriptor> methods) {
    for (Constant c : cp.getConstantPool()) {
        if (c instanceof ConstantMethodref || c instanceof ConstantInterfaceMethodref) {
            ConstantCP desc = (ConstantCP) c;
            ConstantNameAndType nameAndType = (ConstantNameAndType) cp.getConstant(desc.getNameAndTypeIndex());
            String className = cp.getConstantString(desc.getClassIndex(), Const.CONSTANT_Class);
            String name = ((ConstantUtf8) cp.getConstant(nameAndType.getNameIndex())).getBytes();
            String signature = ((ConstantUtf8) cp.getConstant(nameAndType.getSignatureIndex())).getBytes();
            // We don't know whether method is static thus cannot use equals
            int hash = FieldOrMethodDescriptor.getNameSigHashCode(name, signature);
            for (MethodDescriptor method : methods) {
                if (method.getNameSigHashCode() == hash
                        && (method.getSlashedClassName().isEmpty() || method.getSlashedClassName().equals(className))
                        && method.getName().equals(name) && method.getSignature().equals(signature)) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example #2
Source File: ConstantPoolGen.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * Add a new InterfaceMethodref constant to the ConstantPool, if it is not already
 * in there.
 *
 * @param class_name class name string to add
 * @param method_name method name string to add
 * @param signature signature string to add
 * @return index of entry
 */
public int addInterfaceMethodref( final String class_name, final String method_name, final String signature ) {
    int ret;
    int class_index;
    int name_and_type_index;
    if ((ret = lookupInterfaceMethodref(class_name, method_name, signature)) != -1) {
        return ret; // Already in CP
    }
    adjustSize();
    class_index = addClass(class_name);
    name_and_type_index = addNameAndType(method_name, signature);
    ret = index;
    constants[index++] = new ConstantInterfaceMethodref(class_index, name_and_type_index);
    final String key = class_name + IMETHODREF_DELIM + method_name + IMETHODREF_DELIM + signature;
    if (!cpTable.containsKey(key)) {
        cpTable.put(key, new Index(ret));
    }
    return ret;
}
 
Example #3
Source File: ClassDumper.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
private static MethodSymbol makeSymbol(
    ConstantCP constantMethodref, ConstantPool constantPool) {
  String className = constantMethodref.getClass(constantPool);
  ConstantNameAndType constantNameAndType = constantNameAndType(constantMethodref, constantPool);
  String methodName = constantNameAndType.getName(constantPool);
  String descriptor = constantNameAndType.getSignature(constantPool);
  // constantMethodref is either ConstantMethodref or ConstantInterfaceMethodref
  boolean isInterfaceMethod = constantMethodref instanceof ConstantInterfaceMethodref;
  return new MethodSymbol(className, methodName, descriptor, isInterfaceMethod);
}
 
Example #4
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantInterfaceMethodref(final ConstantInterfaceMethodref obj) {
    if (obj.getTag() != Const.CONSTANT_InterfaceMethodref) {
        throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
    }
    checkIndex(obj, obj.getClassIndex(), CONST_Class);
    checkIndex(obj, obj.getNameAndTypeIndex(), CONST_NameAndType);
}
 
Example #5
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantInterfaceMethodref(final ConstantInterfaceMethodref obj) {
    if (obj.getTag() != Const.CONSTANT_InterfaceMethodref) {
        throw new ClassConstraintException("ConstantInterfaceMethodref '"+tostring(obj)+"' has wrong tag!");
    }
    final int name_and_type_index = obj.getNameAndTypeIndex();
    final ConstantNameAndType cnat = (ConstantNameAndType) (cp.getConstant(name_and_type_index));
    final String name = ((ConstantUtf8) (cp.getConstant(cnat.getNameIndex()))).getBytes(); // Field or Method name
    if (!validInterfaceMethodName(name)) {
        throw new ClassConstraintException("Invalid (interface) method name '"+name+"' referenced by '"+tostring(obj)+"'.");
    }

    final int class_index = obj.getClassIndex();
    final ConstantClass cc = (ConstantClass) (cp.getConstant(class_index));
    final String className = ((ConstantUtf8) (cp.getConstant(cc.getNameIndex()))).getBytes(); // Class Name in internal form
    if (! validClassName(className)) {
        throw new ClassConstraintException("Illegal class name '"+className+"' used by '"+tostring(obj)+"'.");
    }

    final String sig  = ((ConstantUtf8) (cp.getConstant(cnat.getSignatureIndex()))).getBytes(); // Field or Method sig.(=descriptor)

    try{
        final Type   t  = Type.getReturnType(sig);
        if ( name.equals(Const.STATIC_INITIALIZER_NAME) && (t != Type.VOID) ) {
            addMessage("Class or interface initialization method '"+Const.STATIC_INITIALIZER_NAME+
                "' usually has VOID return type instead of '"+t+
                "'. Note this is really not a requirement of The Java Virtual Machine Specification, Second Edition.");
        }
    }
    catch (final ClassFormatException cfe) {
        throw new ClassConstraintException("Illegal descriptor (==signature) '"+sig+"' used by '"+tostring(obj)+"'.", cfe);
    }

}
 
Example #6
Source File: BetterVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void visit(ConstantInterfaceMethodref obj) {
    visit((ConstantCP) obj);
}
 
Example #7
Source File: BetterVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visitConstantInterfaceMethodref(ConstantInterfaceMethodref obj) {
    visit(obj);
}
 
Example #8
Source File: TransitiveHull.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantInterfaceMethodref(final ConstantInterfaceMethodref cimr) {
    visitRef(cimr, true);
}
 
Example #9
Source File: StringRepresentation.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantInterfaceMethodref(final ConstantInterfaceMethodref obj) {
    tostring = toString(obj);
}
 
Example #10
Source File: CounterVisitor.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantInterfaceMethodref(final ConstantInterfaceMethodref obj)
{
    constantInterfaceMethodrefCount++;
}