Example usage for org.objectweb.asm.commons Method getMethod

List of usage examples for org.objectweb.asm.commons Method getMethod

Introduction

In this page you can find the example usage for org.objectweb.asm.commons Method getMethod.

Prototype

public static Method getMethod(final String method, final boolean defaultPackage) 

Source Link

Document

Returns a Method corresponding to the given Java method declaration.

Usage

From source file:de.thetaphi.forbiddenapis.Checker.java

License:Apache License

/** Adds the method signature to the list of disallowed methods. The Signature is checked against the given ClassLoader. */
private void addSignature(final String line, final String defaultMessage,
        final boolean failOnUnresolvableSignatures) throws ParseException {
    final String clazz, field, signature, message;
    final Method method;
    int p = line.indexOf('@');
    if (p >= 0) {
        signature = line.substring(0, p).trim();
        message = line.substring(p + 1).trim();
    } else {/*  w ww. j  a  v  a2 s  .co m*/
        signature = line;
        message = defaultMessage;
    }
    p = signature.indexOf('#');
    if (p >= 0) {
        clazz = signature.substring(0, p);
        final String s = signature.substring(p + 1);
        p = s.indexOf('(');
        if (p >= 0) {
            if (p == 0) {
                throw new ParseException("Invalid method signature (method name missing): " + signature);
            }
            // we ignore the return type, its just to match easier (so return type is void):
            try {
                method = Method.getMethod("void " + s, true);
            } catch (IllegalArgumentException iae) {
                throw new ParseException("Invalid method signature: " + signature);
            }
            field = null;
        } else {
            field = s;
            method = null;
        }
    } else {
        clazz = signature;
        method = null;
        field = null;
    }
    // create printout message:
    final String printout = (message != null && message.length() > 0) ? (signature + " [" + message + "]")
            : signature;
    // check class & method/field signature, if it is really existent (in classpath), but we don't really load the class into JVM:
    if (AsmUtils.isGlob(clazz)) {
        if (method != null || field != null) {
            throw new ParseException(String.format(Locale.ENGLISH,
                    "Class level glob pattern cannot be combined with methods/fields: %s", signature));
        }
        forbiddenClassPatterns.add(new ClassPatternRule(clazz, printout));
    } else {
        final ClassSignature c;
        try {
            c = getClassFromClassLoader(clazz);
        } catch (ClassNotFoundException cnfe) {
            reportParseFailed(failOnUnresolvableSignatures, cnfe.getMessage(), signature);
            return;
        }
        if (method != null) {
            assert field == null;
            // list all methods with this signature:
            boolean found = false;
            for (final Method m : c.methods) {
                if (m.getName().equals(method.getName())
                        && Arrays.equals(m.getArgumentTypes(), method.getArgumentTypes())) {
                    found = true;
                    forbiddenMethods.put(c.className + '\000' + m, printout);
                    // don't break when found, as there may be more covariant overrides!
                }
            }
            if (!found) {
                reportParseFailed(failOnUnresolvableSignatures, "Method not found", signature);
                return;
            }
        } else if (field != null) {
            assert method == null;
            if (!c.fields.contains(field)) {
                reportParseFailed(failOnUnresolvableSignatures, "Field not found", signature);
                return;
            }
            forbiddenFields.put(c.className + '\000' + field, printout);
        } else {
            assert field == null && method == null;
            // only add the signature as class name
            forbiddenClasses.put(c.className, printout);
        }
    }
}

From source file:org.apache.lucene.validation.ForbiddenApisCheckTask.java

License:Apache License

/** Adds the method signature to the list of disallowed methods. The Signature is checked against the given ClassLoader. */
private void addSignature(final String signature) throws BuildException {
    final String clazz, field;
    final Method method;
    int p = signature.indexOf('#');
    if (p >= 0) {
        clazz = signature.substring(0, p);
        final String s = signature.substring(p + 1);
        p = s.indexOf('(');
        if (p >= 0) {
            if (p == 0) {
                throw new BuildException("Invalid method signature (method name missing): " + signature);
            }/*www.  j  a  v  a2s  .co m*/
            // we ignore the return type, its just to match easier (so return type is void):
            try {
                method = Method.getMethod("void " + s, true);
            } catch (IllegalArgumentException iae) {
                throw new BuildException("Invalid method signature: " + signature);
            }
            field = null;
        } else {
            field = s;
            method = null;
        }
    } else {
        clazz = signature;
        method = null;
        field = null;
    }
    // check class & method/field signature, if it is really existent (in classpath), but we don't really load the class into JVM:
    final ClassSignatureLookup c = getClassFromClassLoader(clazz);
    if (method != null) {
        assert field == null;
        // list all methods with this signature:
        boolean found = false;
        for (final Method m : c.methods) {
            if (m.getName().equals(method.getName())
                    && Arrays.equals(m.getArgumentTypes(), method.getArgumentTypes())) {
                found = true;
                forbiddenMethods.put(c.reader.getClassName() + '\000' + m, signature);
                // don't break when found, as there may be more covariant overrides!
            }
        }
        if (!found) {
            throw new BuildException("No method found with following signature: " + signature);
        }
    } else if (field != null) {
        assert method == null;
        if (!c.fields.contains(field)) {
            throw new BuildException("No field found with following name: " + signature);
        }
        forbiddenFields.put(c.reader.getClassName() + '\000' + field, signature);
    } else {
        assert field == null && method == null;
        // only add the signature as class name
        forbiddenClasses.put(c.reader.getClassName(), signature);
    }
}