Example usage for org.objectweb.asm ClassReader getSuperName

List of usage examples for org.objectweb.asm ClassReader getSuperName

Introduction

In this page you can find the example usage for org.objectweb.asm ClassReader getSuperName.

Prototype

public String getSuperName() 

Source Link

Document

Returns the internal of name of the super class (see Type#getInternalName() ).

Usage

From source file:asmlib.DuplicateMethodChecker.java

License:Open Source License

public static void verify(ClassReader cr, PrintWriter pw) {
    InfoClass info = new InfoClass(cr.getClassName(), cr.getSuperName());
    cr.accept(new InfoClassAdapter(info), 0);

    List<InfoMethod> methodList = info.methods();
    NavigableSet<InfoMethod> methodSet = new TreeSet<InfoMethod>(methodList);

    if (methodList.size() == methodSet.size())
        return;//from   w  ww.j a  va2 s.c o  m

    while (!methodSet.isEmpty() && (methodList.size() != methodSet.size())) {
        InfoMethod current = methodSet.pollFirst();
        methodList.remove(current);

        boolean duplicate = false;
        while (methodList.contains(current)) {
            duplicate = true;
            methodList.remove(current);
        }

        if (duplicate)
            pw.println("DUPLICATE METHOD DETECTED: " + current.name() + current.desc());
    }
}

From source file:asmlib.InfoClass.java

License:Open Source License

public static InfoClass fromType(Type t) throws IOException {
    org.objectweb.asm.ClassReader cr = new org.objectweb.asm.ClassReader(t.commonName());
    InfoClass infoClass = new InfoClass(cr.getClassName(), cr.getSuperName());
    cr.accept(new InfoClassAdapter(infoClass), 0);
    return infoClass;
}

From source file:asmlib.Util.java

License:Open Source License

public static void populateSuperclasses(InfoClass infoClass) throws IOException {
    if ((infoClass.superclass() != null) || infoClass.type().equals(Type.OBJECT))
        return;/*from w w  w . java 2s.c  o m*/

    ClassReader cr = new ClassReader(infoClass.superclassType().commonName());

    InfoClass superclass = new InfoClass(cr.getClassName(), cr.getSuperName());
    cr.accept(new InfoClassAdapter(superclass), 0);

    infoClass.setSuperclass(superclass);

    populateSuperclasses(superclass);
}

From source file:asmlib.Util.java

License:Open Source License

public static void populateSuperinterfaces(InfoClass infoClass) throws IOException {
    for (Type superInterfaceName : infoClass.interfaceTypes()) {
        ClassReader cr = new ClassReader(superInterfaceName.commonName());
        InfoClass superiface = new InfoClass(cr.getClassName(), cr.getSuperName());
        cr.accept(new InfoClassAdapter(superiface), 0);
        infoClass.addInterface(superiface);
        populateSuperinterfaces(superiface);
    }/*from  w w  w  . jav a 2s. c o  m*/
}

From source file:blue.lapis.methodremapper.Remapper.java

License:Open Source License

private Map<String, String> getMappings(String name, ClassReader reader) throws IOException {
    if (this.classes.containsKey(name)) {
        return this.classes.get(name);
    }//from   w w  w. ja v  a  2 s  . com

    logger.trace("Creating mappings for {}", name);

    if (reader == null) {
        logger.trace("Loading class {}", name);
        reader = this.provider.getClass(name);
    }

    Map<String, String> mappings = null;
    if (reader != null) {
        Map<String, String> builder = findMappings(reader.getSuperName(), null);

        String[] interfaces = reader.getInterfaces();
        if (interfaces != null) {
            for (String iface : interfaces) {
                builder = findMappings(iface, builder);
            }
        }

        if (builder != null) {
            mappings = ImmutableMap.copyOf(builder);
        }
    }

    this.classes.put(name, mappings);
    return mappings;
}

From source file:ch.eiafr.cojac.ModifiedClassWriter.java

License:Apache License

/**
 * Returns the internal names of the ancestor classes of the given type.
 * /*  w ww  . ja v  a 2 s. c o  m*/
 * @param type
 *            the internal name of a class or interface.
 * @param info
 *            the ClassReader corresponding to 'type'.
 * @return a StringBuilder containing the ancestor classes of 'type',
 *         separated by ';'. The returned string has the following format:
 *         ";type1;type2 ... ;typeN", where type1 is 'type', and typeN is a
 *         direct subclass of Object. If 'type' is Object, the returned
 *         string is empty.
 * @throws IOException
 *             if the bytecode of 'type' or of some of its ancestor class
 *             cannot be loaded.
 */
private StringBuilder typeAncestors(String type, ClassReader info) throws IOException {
    StringBuilder b = new StringBuilder();
    while (!"java/lang/Object".equals(type)) {
        b.append(';').append(type);
        type = info.getSuperName();
        info = typeInfo(type);
    }
    return b;
}

From source file:ch.eiafr.cojac.ModifiedClassWriter.java

License:Apache License

/**
 * Returns true if the given type implements the given interface.
 * /*from   w ww  . jav  a 2  s .  co  m*/
 * @param type
 *            the internal name of a class or interface.
 * @param info
 *            the ClassReader corresponding to 'type'.
 * @param itf
 *            the internal name of a interface.
 * @return true if 'type' implements directly or indirectly 'itf'
 * @throws IOException
 *             if the bytecode of 'type' or of some of its ancestor class
 *             cannot be loaded.
 */
private boolean typeImplements(String type, ClassReader info, String itf) throws IOException {
    while (!"java/lang/Object".equals(type)) {
        String[] itfs = info.getInterfaces();
        for (int i = 0; i < itfs.length; ++i) {
            if (itfs[i].equals(itf)) {
                return true;
            }
        }
        for (int i = 0; i < itfs.length; ++i) {
            if (typeImplements(itfs[i], typeInfo(itfs[i]), itf)) {
                return true;
            }
        }
        type = info.getSuperName();
        info = typeInfo(type);
    }
    return false;
}

From source file:chibill.DeobLoader.loader.Remappers.java

License:Open Source License

private void findAndMergeSuperMaps(String name) {
    try {/* ww  w.ja  va 2 s. c  o  m*/
        String superName = null;
        String[] interfaces = new String[0];
        byte[] classBytes = classLoader.getClassBytes(name);
        if (classBytes != null) {
            ClassReader cr = new ClassReader(classBytes);
            superName = cr.getSuperName();
            interfaces = cr.getInterfaces();
        }
        mergeSuperMaps(name, superName, interfaces);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:co.cask.cdap.internal.asm.Classes.java

License:Apache License

/**
 * Checks if the given class extends or implements a super type.
 *
 * @param className name of the class to check
 * @param superTypeName name of the super type
 * @param resourceProvider a {@link Function} to provide {@link URL} of a class from the class name
 * @param cache a cache for memorizing previous decision for classes of the same super type
 * @return true if the given class name is a sub-class of the given super type
 * @throws IOException if failed to read class information
 *//* w  w  w .  j  a va  2  s  .c  o m*/
public static boolean isSubTypeOf(String className, final String superTypeName,
        final Function<String, URL> resourceProvider, final Map<String, Boolean> cache) throws IOException {
    // Base case
    if (superTypeName.equals(className)) {
        cache.put(className, true);
        return true;
    }

    // Check the cache first
    Boolean cachedResult = cache.get(className);
    if (cachedResult != null) {
        return cachedResult;
    }

    // Try to get the URL resource of the given class
    URL url = resourceProvider.apply(className);
    if (url == null) {
        // Ignore it if cannot find the class file for the given class.
        // Normally this shouldn't happen, however it is to guard against mis-packaged artifact jar that included
        // invalid/incomplete jars. Anyway, if this happen, the class won't be loadable in runtime.
        return false;
    }

    // Inspect the bytecode and check the super class/interfaces recursively
    boolean result = false;
    try (InputStream input = url.openStream()) {
        ClassReader cr = new ClassReader(input);
        String superName = cr.getSuperName();
        if (superName != null) {
            result = isSubTypeOf(Type.getObjectType(superName).getClassName(), superTypeName, resourceProvider,
                    cache);
        }

        if (!result) {
            String[] interfaces = cr.getInterfaces();
            if (interfaces != null) {
                for (String intf : interfaces) {
                    if (isSubTypeOf(Type.getObjectType(intf).getClassName(), superTypeName, resourceProvider,
                            cache)) {
                        result = true;
                        break;
                    }
                }
            }
        }
    }

    cache.put(className, result);
    return result;
}

From source file:com.android.tools.layoutlib.create.AsmAnalyzer.java

License:Apache License

/**
 * Checks all the classes defined in the JarClassName instance and uses BCEL to
 * determine if they are derived from the given FQCN super class name.
 * Inserts the super class and all the class objects found in the map.
 *///from  ww  w.  j a va2 s.  c  om
void findClassesDerivingFrom(String super_name, Map<String, ClassReader> zipClasses,
        Map<String, ClassReader> inOutFound) throws LogAbortException {
    ClassReader super_clazz = findClass(super_name, zipClasses, inOutFound);

    for (Entry<String, ClassReader> entry : zipClasses.entrySet()) {
        String className = entry.getKey();
        if (super_name.equals(className)) {
            continue;
        }
        ClassReader classReader = entry.getValue();
        ClassReader parent_cr = classReader;
        while (parent_cr != null) {
            String parent_name = internalToBinaryClassName(parent_cr.getSuperName());
            if (parent_name == null) {
                // not found
                break;
            } else if (super_name.equals(parent_name)) {
                inOutFound.put(className, classReader);
                break;
            }
            parent_cr = zipClasses.get(parent_name);
        }
    }
}