Java Code Examples for org.apache.bcel.classfile.Method#getArgumentTypes()

The following examples show how to use org.apache.bcel.classfile.Method#getArgumentTypes() . 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: LocalVariableTypeTableTestCase.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
public int findFirstStringLocalVariableOffset(final Method method) {
    final Type[] argumentTypes = method.getArgumentTypes();
    int offset = -1;

    for (int i = 0, count = argumentTypes.length; i < count; i++) {
        if (Type.STRING.getSignature().equals(argumentTypes[i].getSignature())) {
            if (method.isStatic()) {
                offset = i;
            } else {
                offset = i + 1;
            }

            break;
        }
    }

    return offset;
}
 
Example 2
Source File: IteratorIdioms.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visit(Method method) {
    if (method.isPublic() && "next".equals(method.getName()) && method.getArgumentTypes().length == 0) {
        shouldVisitCode = true;
        super.visit(method);
    } else {
        shouldVisitCode = false;
    }
}
 
Example 3
Source File: BuildStringPassthruGraph.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void visitMethod(Method obj) {
    argNums = null;
    Type[] argumentTypes = obj.getArgumentTypes();
    if (argumentTypes.length == 0) {
        return;
    }
    int lvNum = obj.isStatic() ? 0 : 1;
    nArgs = argumentTypes.length;
    int argCount = lvNum;
    for (Type type : argumentTypes) {
        argCount += type.getSize();
    }
    for (int i = 0; i < nArgs; i++) {
        if (argumentTypes[i].getSignature().equals("Ljava/lang/String;")) {
            if (argNums == null) {
                argNums = new int[argCount];
                Arrays.fill(argNums, -1);
            }
            argNums[lvNum] = i;
        }
        lvNum += argumentTypes[i].getSize();
    }
    if (argNums != null) {
        passedParameters = new List[nArgs];
    }
    super.visitMethod(obj);
}
 
Example 4
Source File: BuildUnconditionalParamDerefDatabase.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void considerMethod(ClassContext classContext, Method method) {
    boolean hasReferenceParameters = false;
    for (Type argument : method.getArgumentTypes()) {
        if (argument instanceof ReferenceType) {
            hasReferenceParameters = true;
        }
    }

    if (hasReferenceParameters && classContext.getMethodGen(method) != null) {
        if (VERBOSE_DEBUG) {
            System.out.println("Check " + method);
        }
        analyzeMethod(classContext, method);
    }
}
 
Example 5
Source File: GenerateStubDialog.java    From j-j-jvm with Apache License 2.0 5 votes vote down vote up
protected String method2str(final Method method) {
  String modifier = "";

  if (method.isPrivate()) {
    modifier = "private ";
  } else if (method.isProtected()) {
    modifier = "protected ";
  } else if (method.isPublic()) {
    modifier = "public ";
  }

  if (method.isStatic()) {
    modifier += "static ";
  }

  if (method.isFinal()) {
    modifier += "final ";
  }

  modifier += method.getReturnType().toString();

  modifier += ' ' + method.getName();

  final StringBuilder buffer = new StringBuilder();
  org.apache.bcel.generic.Type[] argTypes = method.getArgumentTypes();

  for (int li = 0; li < argTypes.length; li++) {
    if (li > 0) {
      buffer.append(", ");
    }
    buffer.append(argTypes[li].toString());
  }

  modifier += '(' + buffer.toString() + ')';

  return modifier;
}
 
Example 6
Source File: InstructionFinderTestCase.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
public void testSearchAll() throws Exception
{
    final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".util.InstructionFinder");
    final Method[] methods = clazz.getMethods();
    Method searchM = null;
    for (final Method m : methods)
    {
        if (m.getName().equals("search") && (m.getArgumentTypes().length == 3))
        {
            searchM = m;
            break;
        }
    }

    if (searchM == null) {
        throw new Exception("search method not found");
    }

    final byte[] bytes = searchM.getCode().getCode();
    final InstructionList il = new InstructionList(bytes);
    final InstructionFinder finder = new InstructionFinder(il);
    final Iterator<?> it = finder.search(".*", il.getStart(), null);

    final InstructionHandle[] ihs = (InstructionHandle[])it.next();
    int size = 0;
    for (final InstructionHandle ih : ihs)
    {
        size += ih.getInstruction().getLength();
    }
    assertEquals(bytes.length, size);

}
 
Example 7
Source File: HiddenStaticMethodCheck.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/** @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 Method[] methods = javaClass.getMethods();
    // Check all methods
    for (int i = 0; i < methods.length; i++) {
        final Method method = methods[i];
        // Check that the method is a possible match
        if (!method.isPrivate() && method.isStatic())  {
            // Go through all their superclasses
            for (int j = 0; j < superClasses.length; j++) {
                final JavaClass superClass = superClasses[j];
                final String superClassName = superClass.getClassName();
                final Method[] superClassMethods = superClass.getMethods();
                // Go through the methods of the superclasses
                for (int k = 0; k < superClassMethods.length; k++) {
                    final Method superClassMethod = superClassMethods[k];
                    if (superClassMethod.getName().equals(method.getName()) &&
                        !ignore(className, method)) {
                        Type[] methodTypes = method.getArgumentTypes();
                        Type[] superTypes = superClassMethod.
                            getArgumentTypes();
                        if (methodTypes.length == superTypes.length) {
                            boolean match = true;
                            for (int arg = 0; arg < methodTypes.length; arg++) {
                                if (!methodTypes[arg].equals(superTypes[arg])) {
                                    match = false;
                                }
                            }
                            // Same method parameters
                            if (match) {
                                log(
                                    javaClass,
                                    0,
                                    "hidden.static.method",
                                    new Object[] {method, superClassName});
                            }
                        }
                    }
                }
            }
        }
    }
}
 
Example 8
Source File: HiddenStaticMethodCheck.java    From contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
/** @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 Method[] methods = javaClass.getMethods();
    // Check all methods
    for (int i = 0; i < methods.length; i++) {
        final Method method = methods[i];
        // Check that the method is a possible match
        if (!method.isPrivate() && method.isStatic())  {
            // Go through all their superclasses
            for (int j = 0; j < superClasses.length; j++) {
                final JavaClass superClass = superClasses[j];
                final String superClassName = superClass.getClassName();
                final Method[] superClassMethods = superClass.getMethods();
                // Go through the methods of the superclasses
                for (int k = 0; k < superClassMethods.length; k++) {
                    final Method superClassMethod = superClassMethods[k];
                    if (superClassMethod.getName().equals(method.getName()) &&
                        !ignore(className, method)) {
                        Type[] methodTypes = method.getArgumentTypes();
                        Type[] superTypes = superClassMethod.
                            getArgumentTypes();
                        if (methodTypes.length == superTypes.length) {
                            boolean match = true;
                            for (int arg = 0; arg < methodTypes.length; arg++) {
                                if (!methodTypes[arg].equals(superTypes[arg])) {
                                    match = false;
                                }
                            }
                            // Same method parameters
                            if (match) {
                                log(
                                    javaClass,
                                    0,
                                    "hidden.static.method",
                                    new Object[] {method, superClassName});
                            }
                        }
                    }
                }
            }
        }
    }
}