List of usage examples for org.objectweb.asm ClassReader getInterfaces
public String[] getInterfaces()
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); }/* ww w .j a va 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 true if the given type implements the given interface. * //from w ww . j a v a 2s. 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 {//from w w w . j av a2 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 *//*from w ww . ja v a2s.co 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.datatorrent.stram.webapp.TypeGraph.java
License:Apache License
private void addNode(InputStream input, String resName) throws IOException { try {//w ww .j a v a 2 s .c o m ClassReader reader = new ClassReader(input); ClassNode classN = new ClassNodeType(); reader.accept(classN, ClassReader.SKIP_CODE); CompactClassNode ccn = CompactUtil.compactClassNode(classN); String typeName = classN.name.replace('/', '.'); TypeGraphVertex tgv = null; TypeGraphVertex ptgv = null; if (typeGraph.containsKey(typeName)) { tgv = typeGraph.get(typeName); tgv.setClassNode(ccn); tgv.setJarName(resName); // If tgv was already populated for superclass/interface, jar name needs to be updated } else { tgv = new TypeGraphVertex(typeName, resName, ccn); typeGraph.put(typeName, tgv); } String immediateP = reader.getSuperName(); if (immediateP != null) { immediateP = immediateP.replace('/', '.'); ptgv = typeGraph.get(immediateP); if (ptgv == null) { ptgv = new TypeGraphVertex(immediateP, resName); typeGraph.put(immediateP, ptgv); } tgv.ancestors.add(ptgv); ptgv.descendants.add(tgv); } if (reader.getInterfaces() != null) { for (String iface : reader.getInterfaces()) { iface = iface.replace('/', '.'); ptgv = typeGraph.get(iface); if (ptgv == null) { ptgv = new TypeGraphVertex(iface, resName); typeGraph.put(iface, ptgv); } tgv.ancestors.add(ptgv); ptgv.descendants.add(tgv); } } updateInitializableDescendants(tgv); } finally { if (input != null) { input.close(); } } }
From source file:com.develorium.metracer.asm.MetracerClassWriter.java
License:Apache License
private boolean typeImplements(String theType, ClassReader theReader, String theInterface) throws IOException { while (!"java/lang/Object".equals(theType)) { String[] itfs = theReader.getInterfaces(); for (int i = 0; i < itfs.length; ++i) { if (itfs[i].equals(theInterface)) { return true; }//w w w.jav a 2 s . co m } for (int i = 0; i < itfs.length; ++i) { if (typeImplements(itfs[i], typeInfo(itfs[i]), theInterface)) { return true; } } theType = theReader.getSuperName(); theReader = typeInfo(theType); } return false; }
From source file:com.facebook.presto.server.PluginDiscovery.java
License:Apache License
private static List<String> classInterfaces(String name, ClassLoader classLoader) { ImmutableList.Builder<String> list = ImmutableList.builder(); ClassReader reader = readClass(name, classLoader); for (String binaryName : reader.getInterfaces()) { list.add(javaName(binaryName));//from ww w . ja v a2 s . c o m } if (reader.getSuperName() != null) { list.addAll(classInterfaces(javaName(reader.getSuperName()), classLoader)); } return list.build(); }
From source file:com.geeksaga.light.profiler.asm.TypeHierarchyUtil.java
License:Apache License
private TypeHierarchy obtainHierarchyOf(ClassReader reader) { return new TypeHierarchy(Type.getObjectType(reader.getClassName()), reader.getSuperName() == null ? null : Type.getObjectType(reader.getSuperName()), interfacesTypesFrom(reader.getInterfaces()), (reader.getAccess() & ACC_INTERFACE) != 0); }
From source file:com.getperka.cli.classpath.Search.java
License:Apache License
/** * Given a Type, find all Types to which it is assignable by crawling its supertype / * superinterface hierarchy. The results of this method will be cached to improve performance. *///from w ww . j av a2 s. c om private Set<Type> allSupertypes(Type type) { synchronized (allSupertypes) { if (type.getSort() != Type.OBJECT) { return Collections.emptySet(); } Set<Type> toReturn = allSupertypes.get(type); if (toReturn != null) { return toReturn; } // Prevent cycles allSupertypes.put(type, Collections.<Type>emptySet()); InputStream stream = loader.getResourceAsStream(type.getInternalName() + ".class"); if (stream == null) { return Collections.emptySet(); } ClassReader reader; try { reader = new ClassReader(stream); } catch (IOException e) { return fail(e); } toReturn = new LinkedHashSet<Type>(); String superTypeName = reader.getSuperName(); if (superTypeName != null) { Type superType = Type.getObjectType(superTypeName); toReturn.add(superType); toReturn.addAll(allSupertypes(superType)); } String[] intfNames = reader.getInterfaces(); if (intfNames != null) { for (String intf : intfNames) { Type intfType = Type.getObjectType(intf); toReturn.add(intfType); toReturn.addAll(allSupertypes(intfType)); } } toReturn = Collections.unmodifiableSet(toReturn); allSupertypes.put(type, toReturn); return toReturn; } }
From source file:com.google.gwt.jvm.staticcompiler.Compiler.java
License:Apache License
protected void inspectClass(String internalName, byte[] bytes, CompiledClassHandler handler) { ClassReader reader = new ClassReader(bytes); System.out.println("maybeCompile " + reader.getClassName() + " extends " + reader.getSuperName()); boolean isUiBinder = false; for (String i : reader.getInterfaces()) { System.out.println("i " + i); if (i.equals("com/google/gwt/uibinder/client/UiBinder")) { isUiBinder = true;/*from w w w . j a va 2 s .c o m*/ } } if (isUiBinder) { if (gwtCompiler == null) { gwtCompiler = new JavaGwtCompiler(gwtModule); gwtCompiler.setLogger(logger); } String className = Name.InternalName.toSourceName(internalName); CompiledClass compiledClass = gwtCompiler.compile(className, UiBinderGenerator.class); if (handler != null) { handler.accept(compiledClass.getInternalName(), compiledClass.getBytes()); } gwtCreateClasses.put(Name.InternalName.toBinaryName(internalName), compiledClass.getSourceName()); } // This is insufficient. We also need to track anything that has JSO as the // root of its type heirachy. if (reader.getSuperName() .equals(Name.BinaryName.toInternalName(JavaScriptObject.class.getCanonicalName()))) { System.out.println("Javascriptobject"); } }