List of usage examples for org.objectweb.asm Opcodes ASM4
int ASM4
To view the source code for org.objectweb.asm Opcodes ASM4.
Click Source Link
From source file:org.apache.drill.exec.compile.bytecode.InstructionModifier.java
License:Apache License
public InstructionModifier(int access, String name, String desc, String signature, String[] exceptions, TrackingInstructionList list, MethodVisitor inner) { super(Opcodes.ASM4, new DirectSorter(access, desc, inner)); this.list = list; this.adder = (DirectSorter) mv; }
From source file:org.apache.drill.exec.compile.bytecode.ValueHolderReplacementVisitor.java
License:Apache License
public ValueHolderReplacementVisitor(ClassVisitor cw) { super(Opcodes.ASM4, cw); }
From source file:org.apache.drill.exec.compile.DrillInitMethodVisitor.java
License:Apache License
public DrillInitMethodVisitor(String className, MethodVisitor mv) { super(Opcodes.ASM4, mv); this.className = className; }
From source file:org.apache.drill.exec.compile.MergeAdapter.java
License:Apache License
private MergeAdapter(ClassSet set, ClassVisitor cv, ClassNode cn) { super(Opcodes.ASM4, cv); this.classToMerge = cn; this.set = set; for (Object o : classToMerge.methods) { String name = ((MethodNode) o).name; if (name.equals("<init>")) { continue; }// w w w .j a va2 s .c o m if (name.equals(SignatureHolder.DRILL_INIT_METHOD)) { hasInit = true; } mergingNames.add(name); } }
From source file:org.apache.ignite.internal.processors.hadoop.GridHadoopClassLoader.java
License:Apache License
/** * @param name Name.//ww w .jav a 2 s.c om * @param replace Replacement. * @return Class. */ private Class<?> loadFromBytes(final String name, final String replace) { synchronized (getClassLoadingLock(name)) { // First, check if the class has already been loaded Class c = findLoadedClass(name); if (c != null) return c; byte[] bytes = bytesCache.get(name); if (bytes == null) { InputStream in = loadClassBytes(getParent(), replace); ClassReader rdr; try { rdr = new ClassReader(in); } catch (IOException e) { throw new RuntimeException(e); } ClassWriter w = new ClassWriter(Opcodes.ASM4); rdr.accept(new RemappingClassAdapter(w, new Remapper() { /** */ String replaceType = replace.replace('.', '/'); /** */ String nameType = name.replace('.', '/'); @Override public String map(String type) { if (type.equals(replaceType)) return nameType; return type; } }), ClassReader.EXPAND_FRAMES); bytes = w.toByteArray(); bytesCache.put(name, bytes); } return defineClass(name, bytes, 0, bytes.length); } }
From source file:org.apache.ignite.internal.processors.hadoop.GridHadoopClassLoader.java
License:Apache License
/** * @param clsName Class name.//from w w w.ja va 2 s. c o m * @return {@code true} If the class has external dependencies. */ boolean hasExternalDependencies(final String clsName, final Set<String> visited) { if (isHadoop(clsName)) // Hadoop must not be in classpath but Idea sucks, so filtering explicitly as external. return true; // Try to get from parent to check if the type accessible. InputStream in = loadClassBytes(getParent(), clsName); if (in == null) // The class is external itself, it must be loaded from this class loader. return true; if (!isIgfsOrGgHadoop(clsName)) // Other classes should not have external dependencies. return false; final ClassReader rdr; try { rdr = new ClassReader(in); } catch (IOException e) { throw new RuntimeException("Failed to read class: " + clsName, e); } visited.add(clsName); final AtomicBoolean hasDeps = new AtomicBoolean(); rdr.accept(new ClassVisitor(Opcodes.ASM4) { AnnotationVisitor av = new AnnotationVisitor(Opcodes.ASM4) { // TODO }; FieldVisitor fv = new FieldVisitor(Opcodes.ASM4) { @Override public AnnotationVisitor visitAnnotation(String desc, boolean b) { onType(desc); return av; } }; MethodVisitor mv = new MethodVisitor(Opcodes.ASM4) { @Override public AnnotationVisitor visitAnnotation(String desc, boolean b) { onType(desc); return av; } @Override public AnnotationVisitor visitParameterAnnotation(int i, String desc, boolean b) { onType(desc); return av; } @Override public AnnotationVisitor visitAnnotationDefault() { return av; } @Override public void visitFieldInsn(int i, String owner, String name, String desc) { onType(owner); onType(desc); } @Override public void visitFrame(int i, int i2, Object[] locTypes, int i3, Object[] stackTypes) { for (Object o : locTypes) { if (o instanceof String) onType((String) o); } for (Object o : stackTypes) { if (o instanceof String) onType((String) o); } } @Override public void visitLocalVariable(String name, String desc, String signature, Label lb, Label lb2, int i) { onType(desc); } @Override public void visitMethodInsn(int i, String owner, String name, String desc) { onType(owner); } @Override public void visitMultiANewArrayInsn(String desc, int dim) { onType(desc); } @Override public void visitTryCatchBlock(Label lb, Label lb2, Label lb3, String e) { onType(e); } }; void onClass(String depCls) { assert validateClassName(depCls) : depCls; if (depCls.startsWith("java.")) // Filter out platform classes. return; if (visited.contains(depCls)) return; Boolean res = cache.get(depCls); if (res == Boolean.TRUE || (res == null && hasExternalDependencies(depCls, visited))) hasDeps.set(true); } void onType(String type) { if (type == null) return; int off = 0; while (type.charAt(off) == '[') off++; // Handle arrays. if (off != 0) type = type.substring(off); if (type.length() == 1) return; // Get rid of primitives. if (type.charAt(type.length() - 1) == ';') { assert type.charAt(0) == 'L' : type; type = type.substring(1, type.length() - 1); } type = type.replace('/', '.'); onClass(type); } @Override public void visit(int i, int i2, String name, String signature, String superName, String[] ifaces) { onType(superName); if (ifaces != null) { for (String iface : ifaces) onType(iface); } } @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { onType(desc); return av; } @Override public void visitInnerClass(String name, String outerName, String innerName, int i) { onType(name); } @Override public FieldVisitor visitField(int i, String name, String desc, String signature, Object val) { onType(desc); return fv; } @Override public MethodVisitor visitMethod(int i, String name, String desc, String signature, String[] exceptions) { if (exceptions != null) { for (String e : exceptions) onType(e); } return mv; } }, 0); if (hasDeps.get()) // We already know that we have dependencies, no need to check parent. return true; // Here we are known to not have any dependencies but possibly we have a parent which have them. int idx = clsName.lastIndexOf('$'); if (idx == -1) // No parent class. return false; String parentCls = clsName.substring(0, idx); if (visited.contains(parentCls)) return false; Boolean res = cache.get(parentCls); if (res == null) res = hasExternalDependencies(parentCls, visited); return res; }
From source file:org.apache.ignite.internal.processors.hadoop.HadoopClassLoader.java
License:Apache License
/** * @param clsName Class name.//w ww . ja v a 2 s . c o m * @return {@code true} If the class has external dependencies. */ boolean hasExternalDependencies(final String clsName, final Set<String> visited) { if (isHadoop(clsName)) // Hadoop must not be in classpath but Idea sucks, so filtering explicitly as external. return true; // Try to get from parent to check if the type accessible. InputStream in = loadClassBytes(getParent(), clsName); if (in == null) // The class is external itself, it must be loaded from this class loader. return true; if (!isHadoopIgfs(clsName)) // Other classes should not have external dependencies. return false; final ClassReader rdr; try { rdr = new ClassReader(in); } catch (IOException e) { throw new RuntimeException("Failed to read class: " + clsName, e); } visited.add(clsName); final AtomicBoolean hasDeps = new AtomicBoolean(); rdr.accept(new ClassVisitor(Opcodes.ASM4) { AnnotationVisitor av = new AnnotationVisitor(Opcodes.ASM4) { // TODO }; FieldVisitor fv = new FieldVisitor(Opcodes.ASM4) { @Override public AnnotationVisitor visitAnnotation(String desc, boolean b) { onType(desc); return av; } }; MethodVisitor mv = new MethodVisitor(Opcodes.ASM4) { @Override public AnnotationVisitor visitAnnotation(String desc, boolean b) { onType(desc); return av; } @Override public AnnotationVisitor visitParameterAnnotation(int i, String desc, boolean b) { onType(desc); return av; } @Override public AnnotationVisitor visitAnnotationDefault() { return av; } @Override public void visitFieldInsn(int i, String owner, String name, String desc) { onType(owner); onType(desc); } @Override public void visitFrame(int i, int i2, Object[] locTypes, int i3, Object[] stackTypes) { for (Object o : locTypes) { if (o instanceof String) onType((String) o); } for (Object o : stackTypes) { if (o instanceof String) onType((String) o); } } @Override public void visitLocalVariable(String name, String desc, String signature, Label lb, Label lb2, int i) { onType(desc); } @Override public void visitMethodInsn(int i, String owner, String name, String desc) { onType(owner); } @Override public void visitMultiANewArrayInsn(String desc, int dim) { onType(desc); } @Override public void visitTryCatchBlock(Label lb, Label lb2, Label lb3, String e) { onType(e); } }; void onClass(String depCls) { assert validateClassName(depCls) : depCls; if (depCls.startsWith("java.")) // Filter out platform classes. return; if (visited.contains(depCls)) return; Boolean res = cache.get(depCls); if (res == Boolean.TRUE || (res == null && hasExternalDependencies(depCls, visited))) hasDeps.set(true); } void onType(String type) { if (type == null) return; int off = 0; while (type.charAt(off) == '[') off++; // Handle arrays. if (off != 0) type = type.substring(off); if (type.length() == 1) return; // Get rid of primitives. if (type.charAt(type.length() - 1) == ';') { assert type.charAt(0) == 'L' : type; type = type.substring(1, type.length() - 1); } type = type.replace('/', '.'); onClass(type); } @Override public void visit(int i, int i2, String name, String signature, String superName, String[] ifaces) { onType(superName); if (ifaces != null) { for (String iface : ifaces) onType(iface); } } @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { onType(desc); return av; } @Override public void visitInnerClass(String name, String outerName, String innerName, int i) { onType(name); } @Override public FieldVisitor visitField(int i, String name, String desc, String signature, Object val) { onType(desc); return fv; } @Override public MethodVisitor visitMethod(int i, String name, String desc, String signature, String[] exceptions) { if (exceptions != null) { for (String e : exceptions) onType(e); } return mv; } }, 0); if (hasDeps.get()) // We already know that we have dependencies, no need to check parent. return true; // Here we are known to not have any dependencies but possibly we have a parent which have them. int idx = clsName.lastIndexOf('$'); if (idx == -1) // No parent class. return false; String parentCls = clsName.substring(0, idx); if (visited.contains(parentCls)) return false; Boolean res = cache.get(parentCls); if (res == null) res = hasExternalDependencies(parentCls, visited); return res; }
From source file:org.apache.ignite.internal.processors.hadoop.HadoopHelperImpl.java
License:Apache License
/** {@inheritDoc} */ @Override// w ww . j a va 2s . c o m public byte[] loadReplace(InputStream in, final String originalName, final String replaceName) { ClassReader rdr; try { rdr = new ClassReader(in); } catch (IOException e) { throw new RuntimeException(e); } ClassWriter w = new ClassWriter(Opcodes.ASM4); rdr.accept(new RemappingClassAdapter(w, new Remapper() { /** */ String replaceType = replaceName.replace('.', '/'); /** */ String nameType = originalName.replace('.', '/'); @Override public String map(String type) { if (type.equals(replaceType)) return nameType; return type; } }), ClassReader.EXPAND_FRAMES); return w.toByteArray(); }
From source file:org.apache.lucene.validation.ForbiddenApisCheckTask.java
License:Apache License
/** Parses a class given as Resource and checks for valid method invocations */ private int checkClass(final ClassReader reader) { final int[] violations = new int[1]; reader.accept(new ClassVisitor(Opcodes.ASM4) { final String className = Type.getObjectType(reader.getClassName()).getClassName(); String source = null;/*from w w w . jav a2 s . c o m*/ @Override public void visitSource(String source, String debug) { this.source = source; } @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { return new MethodVisitor(Opcodes.ASM4) { private int lineNo = -1; private ClassSignatureLookup lookupRelatedClass(String internalName) { ClassSignatureLookup c = classesToCheck.get(internalName); if (c == null) try { c = getClassFromClassLoader(internalName); } catch (BuildException be) { // we ignore lookup errors and simply ignore this related class c = null; } return c; } private boolean checkClassUse(String owner) { final String printout = forbiddenClasses.get(owner); if (printout != null) { log("Forbidden class use: " + printout, Project.MSG_ERR); return true; } return false; } private boolean checkMethodAccess(String owner, Method method) { if (checkClassUse(owner)) { return true; } final String printout = forbiddenMethods.get(owner + '\000' + method); if (printout != null) { log("Forbidden method invocation: " + printout, Project.MSG_ERR); return true; } final ClassSignatureLookup c = lookupRelatedClass(owner); if (c != null && !c.methods.contains(method)) { final String superName = c.reader.getSuperName(); if (superName != null && checkMethodAccess(superName, method)) { return true; } final String[] interfaces = c.reader.getInterfaces(); if (interfaces != null) { for (String intf : interfaces) { if (intf != null && checkMethodAccess(intf, method)) { return true; } } } } return false; } private boolean checkFieldAccess(String owner, String field) { if (checkClassUse(owner)) { return true; } final String printout = forbiddenFields.get(owner + '\000' + field); if (printout != null) { log("Forbidden field access: " + printout, Project.MSG_ERR); return true; } final ClassSignatureLookup c = lookupRelatedClass(owner); if (c != null && !c.fields.contains(field)) { final String superName = c.reader.getSuperName(); if (superName != null && checkFieldAccess(superName, field)) { return true; } final String[] interfaces = c.reader.getInterfaces(); if (interfaces != null) { for (String intf : interfaces) { if (intf != null && checkFieldAccess(intf, field)) { return true; } } } } return false; } @Override public void visitMethodInsn(int opcode, String owner, String name, String desc) { if (checkMethodAccess(owner, new Method(name, desc))) { violations[0]++; reportSourceAndLine(); } } @Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { if (checkFieldAccess(owner, name)) { violations[0]++; reportSourceAndLine(); } } private void reportSourceAndLine() { final StringBuilder sb = new StringBuilder(" in ").append(className); if (source != null && lineNo >= 0) { new Formatter(sb, Locale.ROOT).format(" (%s:%d)", source, lineNo).flush(); } log(sb.toString(), Project.MSG_ERR); } @Override public void visitLineNumber(int lineNo, Label start) { this.lineNo = lineNo; } }; } }, ClassReader.SKIP_FRAMES); return violations[0]; }
From source file:org.apache.maven.plugins.shade.DefaultShaderTest.java
License:Apache License
public void testShaderWithRelocatedClassname() throws Exception { DefaultShader s = newShader();/*ww w . j a va2 s. com*/ Set<File> set = new LinkedHashSet<File>(); set.add(new File("src/test/jars/test-project-1.0-SNAPSHOT.jar")); set.add(new File("src/test/jars/plexus-utils-1.4.1.jar")); List<Relocator> relocators = new ArrayList<Relocator>(); relocators.add( new SimpleRelocator("org/codehaus/plexus/util/", "_plexus/util/__", null, Arrays.<String>asList())); List<ResourceTransformer> resourceTransformers = new ArrayList<ResourceTransformer>(); resourceTransformers.add(new ComponentsXmlResourceTransformer()); List<Filter> filters = new ArrayList<Filter>(); File file = new File("target/foo-relocate-class.jar"); ShadeRequest shadeRequest = new ShadeRequest(); shadeRequest.setJars(set); shadeRequest.setUberJar(file); shadeRequest.setFilters(filters); shadeRequest.setRelocators(relocators); shadeRequest.setResourceTransformers(resourceTransformers); shadeRequest.setListShadedInJar(true); s.shade(shadeRequest); URLClassLoader cl = new URLClassLoader(new URL[] { file.toURI().toURL() }); Class<?> c = cl.loadClass("_plexus.util.__StringUtils"); // first, ensure it works: Object o = c.newInstance(); assertEquals("", c.getMethod("clean", String.class).invoke(o, (String) null)); // now, check that its source file was rewritten: final String[] source = { null }; final ClassReader classReader = new ClassReader(cl.getResourceAsStream("_plexus/util/__StringUtils.class")); classReader.accept(new ClassVisitor(Opcodes.ASM4) { @Override public void visitSource(String arg0, String arg1) { super.visitSource(arg0, arg1); source[0] = arg0; } }, ClassReader.SKIP_CODE); assertEquals("__StringUtils.java", source[0]); testNumberOfShadedDeps(2, file); // Now we re-use the uber jar we just made so we can test nested shading // NOTE: there should be 4 list entrys 3 for the jar we just made // shaded stuff + it's name, and 1 for the new jar we are adding. set = new LinkedHashSet<File>(); set.add(new File("src/test/jars/test-artifact-1.0-SNAPSHOT.jar")); set.add(file); File newUber = new File("target/foo-relocate-class-nested.jar"); shadeRequest = new ShadeRequest(); shadeRequest.setJars(set); shadeRequest.setUberJar(newUber); shadeRequest.setFilters(filters); shadeRequest.setRelocators(relocators); shadeRequest.setResourceTransformers(resourceTransformers); shadeRequest.setListShadedInJar(true); s = newShader(); s.shade(shadeRequest); testNumberOfShadedDeps(4, newUber); // Now we test that we aren't doubling any entries, if we include the same jar twice it should // only be present in the list once set = new LinkedHashSet<File>(); set.add(newUber); newUber = new File("target/foo-relocate-class-nested-rep.jar"); shadeRequest = new ShadeRequest(); shadeRequest.setJars(set); shadeRequest.setUberJar(newUber); shadeRequest.setFilters(filters); shadeRequest.setRelocators(relocators); shadeRequest.setResourceTransformers(resourceTransformers); shadeRequest.setListShadedInJar(true); s = newShader(); s.shade(shadeRequest); // only an increase of one due to previous jar added testNumberOfShadedDeps(5, newUber); }