List of usage examples for org.objectweb.asm.tree ClassNode ClassNode
public ClassNode()
From source file:nova.core.wrapper.mc.forge.v17.asm.lib.ASMHelper.java
License:Open Source License
public static ClassNode createClassNode(byte[] bytes, int flags) { ClassNode cnode = new ClassNode(); ClassReader reader = new ClassReader(bytes); reader.accept(cnode, flags);//from w w w. j a v a2s. com return cnode; }
From source file:nova.core.wrapper.mc.forge.v17.asm.lib.ComponentInjector.java
License:Open Source License
private Class<? extends T> construct(List<Class<? extends Component>> components) { // Map components to specified wrapped interfaces Map<Class<?>, Class<? extends Component>> intfComponentMap = new HashMap<>(); for (Class<? extends Component> component : components) { for (Passthrough pt : component.getAnnotationsByType(Passthrough.class)) { Class<?> intf;//from w w w. j av a 2 s . com try { intf = Class.forName(pt.value()); } catch (ClassNotFoundException exec) { throw new ClassLoaderUtil.ClassLoaderException( "Invalid passthrough \"%s\" on component %s, the specified interface doesn't exist.", pt.value(), component); } if (!intf.isAssignableFrom(component)) { throw new ClassLoaderUtil.ClassLoaderException( "Invalid passthrough \"%s\" on component %s, the specified interface isn't implemented.", pt.value(), component); } if (intfComponentMap.containsKey(intf)) { throw new ClassLoaderUtil.ClassLoaderException( "Duplicate Passthrough interface found: %s (%s, %s)", pt.value(), component, intfComponentMap.get(intf)); } intfComponentMap.put(intf, component); } } // Create new ClassNode from cached bytes ClassNode clazzNode = new ClassNode(); String name = Type.getInternalName(baseClazz); String classname = name + "_$$_NOVA_" + cache.size(); // Inject block field clazzNode.visit(V1_8, ACC_PUBLIC | ACC_SUPER, classname, null, name, intfComponentMap.keySet().stream().map(Type::getInternalName).toArray(s -> new String[s])); clazzNode.visitField(ACC_PRIVATE | ACC_FINAL, "$$_provider", Type.getDescriptor(ComponentProvider.class), null, null).visitEnd(); // Add constructors for (Constructor<?> constructor : baseClazz.getConstructors()) { int mod = constructor.getModifiers(); String descr = Type.getConstructorDescriptor(constructor); if (Modifier.isFinal(mod) || Modifier.isPrivate(mod)) { continue; } MethodVisitor mv = clazzNode.visitMethod(mod, "<init>", descr, null, ASMHelper.getExceptionTypes(constructor)); // Call super constructor mv.visitCode(); // load this mv.visitVarInsn(ALOAD, 0); Class<?>[] parameters = constructor.getParameterTypes(); for (int i = 0; i < constructor.getParameterCount(); i++) { // variables mv.visitVarInsn(Type.getType(parameters[i]).getOpcode(ILOAD), i + 1); } mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(baseClazz), "<init>", descr, false); // return mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } // Add methods for (Class<?> intf : intfComponentMap.keySet()) { // Create class constant Type clazzConst = Type.getType(intf.getClass()); for (Method m : intf.getMethods()) { boolean isVoid = m.getReturnType() == null; String descr = Type.getMethodDescriptor(m); MethodVisitor mv = clazzNode.visitMethod(ACC_PUBLIC, m.getName(), descr, null, ASMHelper.getExceptionTypes(m)); mv.visitCode(); // load block instance mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, classname, "$$_provider", Type.getDescriptor(ComponentProvider.class)); mv.visitLdcInsn(clazzConst); // load component instance mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(ComponentProvider.class), "get", Type.getMethodDescriptor(Type.getType(Component.class), Type.getType(Class.class)), false); // add parameters Class<?>[] parameters = m.getParameterTypes(); for (int i = 0; i < m.getParameterCount(); i++) { mv.visitVarInsn(Type.getType(parameters[i]).getOpcode(ILOAD), i + 1); } // invoke mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(intf), m.getName(), descr, true); mv.visitInsn(isVoid ? RETURN : Type.getType(m.getReturnType()).getOpcode(IRETURN)); mv.visitMaxs(0, 0); mv.visitEnd(); } } clazzNode.visitEnd(); return ASMHelper.defineClass(clazzNode, ClassWriter.COMPUTE_MAXS, baseClazz.getProtectionDomain()); }
From source file:org.adjective.stout.tools.ClassVerifier.java
License:Apache License
@SuppressWarnings("unchecked") private static void execute(File classFile, String methodName) throws FileNotFoundException, IOException { InputStream input = new FileInputStream(classFile); ClassReader reader = new ClassReader(input); ClassNode node = new ClassNode(); reader.accept(node, 0);//from www.j av a 2 s .c o m List<MethodNode> methods = node.methods; for (MethodNode method : methods) { if (method.name.equals(methodName) || "*".equals(methodName)) { verify(node.name, method); } } }
From source file:org.anon.smart.base.stt.asm.ASMSTTReader.java
License:Open Source License
public void initialize(InputStream istr) throws CtxException { try {/* w ww . ja v a 2s . c o m*/ ClassReader reader = new ClassReader(istr); _clazz = new ClassNode(); reader.accept(_clazz, ClassReader.EXPAND_FRAMES); } catch (Exception e) { except().rt(e, new CtxException.Context("Error initializing: ", "ASMSTTReader")); } }
From source file:org.apache.cassandra.continuations.ContinuationClassTransformer.java
License:Apache License
public byte[] transform(ClassLoader classLoader, String className, Class redefiningClass, ProtectionDomain domain, byte[] bytes) throws IllegalClassFormatException { /*//from w ww . j av a 2s. c o m * Use the ASM class reader to see which classes support * the Suspendable annotation. If they do then those * classes need to have their bytecodes transformed for * Continuation support. */ ClassReader classReader = new ClassReader(bytes); ClassNode classNode = new ClassNode(); classReader.accept(classNode, true); List<AnnotationNode> annotationNodes = classNode.visibleAnnotations; for (AnnotationNode annotationNode : annotationNodes) { if (annotationNode.desc.contains(ContinuationClassTransformer.targetAnnotation_)) { bytes = transformer_.transform(bytes); } } return bytes; }
From source file:org.apache.drill.exec.compile.ClassTransformer.java
License:Apache License
private static ClassNode getClassNodeFromByteCode(byte[] bytes) { ClassReader iReader = new ClassReader(bytes); ClassNode impl = new ClassNode(); iReader.accept(impl, ClassReader.EXPAND_FRAMES); return impl;/* w w w . ja v a 2 s.c o m*/ }
From source file:org.apache.drill.exec.compile.TestClassTransformation.java
License:Apache License
@Test // DRILL-6524 public void testScalarReplacementInCondition() throws Exception { ClassTransformer.ClassNames classNames = new ClassTransformer.ClassNames( "org.apache.drill.CompileClassWithIfs"); String entireClass = DrillFileUtils .getResourceAsString(DrillFileUtils.SEPARATOR + classNames.slash + ".java"); sessionOptions.setLocalOption(ClassCompilerSelector.JAVA_COMPILER_DEBUG_OPTION, false); List<String> compilers = Arrays.asList(ClassCompilerSelector.CompilerPolicy.JANINO.name(), ClassCompilerSelector.CompilerPolicy.JDK.name()); for (String compilerName : compilers) { sessionOptions.setLocalOption(ClassCompilerSelector.JAVA_COMPILER_OPTION, compilerName); QueryClassLoader queryClassLoader = new QueryClassLoader(config, sessionOptions); byte[][] implementationClasses = queryClassLoader.getClassByteCode(classNames, entireClass); ClassNode originalClass = AsmUtil.classFromBytes(implementationClasses[0], ClassReader.EXPAND_FRAMES); ClassNode transformedClass = new ClassNode(); DrillCheckClassAdapter mergeGenerator = new DrillCheckClassAdapter(CompilationConfig.ASM_API_VERSION, new CheckClassVisitorFsm(CompilationConfig.ASM_API_VERSION, transformedClass), true); originalClass.accept(new ValueHolderReplacementVisitor(mergeGenerator, true)); if (!AsmUtil.isClassOk(logger, classNames.dot, transformedClass)) { throw new IllegalStateException( String.format("Problem found after transforming %s", classNames.dot)); }// ww w . j av a 2 s. c o m ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES); transformedClass.accept(writer); byte[] outputClass = writer.toByteArray(); queryClassLoader.injectByteCode(classNames.dot, outputClass); Class<?> transformedClazz = queryClassLoader.findClass(classNames.dot); transformedClazz.getMethod("doSomething").invoke(null); } }
From source file:org.apache.felix.ipojo.manipulator.metadata.annotation.ClassMetadataCollector.java
License:Apache License
public ClassMetadataCollector(BindingRegistry registry, Reporter reporter) { super(Opcodes.ASM5); this.registry = registry; this.reporter = reporter; node = new ClassNode(); }
From source file:org.apache.felix.ipojo.manipulator.metadata.annotation.ComponentWorkbenchTestCase.java
License:Apache License
public static ClassNode node() { ClassNode node = new ClassNode(); node.name = "my/Component"; return node; }
From source file:org.apache.felix.ipojo.manipulator.metadata.annotation.registry.SelectionTestCase.java
License:Apache License
private ClassNode classNode() { ClassNode node = new ClassNode(); node.visit(0, 0, "my/Component", null, "java/lang/Object", null); return node;/*from w ww.java2 s .c om*/ }