List of usage examples for org.objectweb.asm.tree ClassNode ClassNode
public ClassNode()
From source file:org.jvnet.jax_ws_commons.beans_generator.ambassador.impl.asm.ASMResponseBeanGenerator.java
License:Open Source License
public byte[] generate() { ClassNode tree = new ClassNode(); tree.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, methodInfo.getResponseBeanClassName().replace(".", "/"), null, Type.getInternalName(Object.class), new String[0]); generateNoArgsConstructor();//from www. j a v a 2 s .c o m generateAnnotations(); generateFields(); tree.visitEnd(); tree.accept(writer); return writer.toByteArray(); }
From source file:org.kantega.revoc.demo.ControlFlowAnalysis.java
License:Apache License
public static void main(String[] args) throws IOException, AnalyzerException { InputStream inputStream = HelloWorld.class.getResourceAsStream("ClassWithLogic.class"); ClassReader cr = new ClassReader(inputStream); ClassNode classNode = new ClassNode(); cr.accept(classNode, 0);//from w w w. j a v a 2 s .co m MethodNode mn = (MethodNode) classNode.methods.get(1); System.out.println("Analyzing method " + mn.name + ":"); Analyzer a = new Analyzer(new BasicInterpreter()) { protected Frame newFrame(int nLocals, int nStack) { return new Node(nLocals, nStack); } protected Frame newFrame(Frame src) { return new Node(src); } @Override protected void newControlFlowEdge(int src, int dst) { Node s = (Node) getFrames()[src]; Node d = (Node) getFrames()[dst]; s.successors.add(d); } protected void newControlFlowEdge(Frame src, Frame dst) { Node s = (Node) src; s.successors.add(new Node(dst)); } }; a.analyze(classNode.name, mn); Frame[] frames = a.getFrames(); int edges = 0; int nodes = 0; for (int i = 0; i < frames.length; ++i) { if (frames[i] != null) { int numSuccessors = ((Node) frames[i]).successors.size(); edges += numSuccessors; nodes += 1; TraceMethodVisitor v = new TraceMethodVisitor(new Textifier()); mn.instructions.get(i).accept(v); System.out.println("Frame " + i + " (" + numSuccessors + ") " + v.toString()); } } System.out.println("CC: " + (edges - nodes + 2)); }
From source file:org.lenskit.data.entities.AbstractBeanEntity.java
License:Open Source License
private static BeanAttributeGetter generateGetter(DynamicClassLoader dlc, Class<? extends AbstractBeanEntity> type, Method getter) { ClassNode node = new ClassNode(); node.name = String.format("%s$$AttrGet$%s", Type.getInternalName(type), getter.getName()); node.access = ACC_PUBLIC;/*w w w .ja v a2 s .c o m*/ node.version = V1_8; node.superName = Type.getInternalName(BeanAttributeGetter.class); node.methods.add(generateGetterConstructor()); node.methods.add(generateGetterMethod(type, getter)); if (Type.getReturnType(getter).equals(Type.LONG_TYPE)) { node.methods.add(generateLongGetterMethod(type, getter)); } else if (Type.getReturnType(getter).equals(Type.DOUBLE_TYPE)) { node.methods.add(generateDoubleGetterMethod(type, getter)); } Class<? extends BeanAttributeGetter> cls = dlc.defineClass(node).asSubclass(BeanAttributeGetter.class); try { return cls.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException("Cannot instantiate " + cls, e); } }
From source file:org.lenskit.data.entities.AbstractBeanEntityBuilder.java
License:Open Source License
private static AttrMethod[] lookupAttrs(Class<? extends AbstractBeanEntityBuilder> type) { AttrMethod[] res = cache.get(type);/*from w w w . j a va2 s .co m*/ if (res != null) { return res; } DynamicClassLoader dlc = new DynamicClassLoader(type.getClassLoader()); Map<TypedName<?>, Method> setters = new HashMap<>(); Map<String, Method> clearers = new HashMap<>(); for (Method m : type.getMethods()) { EntityAttributeSetter annot = m.getAnnotation(EntityAttributeSetter.class); if (annot != null) { Type[] params = m.getParameterTypes(); if (params.length != 1) { throw new IllegalArgumentException( "method " + m + " has " + params.length + " parameters, expected 1"); } TypeToken atype = TypeToken.of(params[0]); TypedName<?> name = TypedName.create(annot.value(), atype); setters.put(name, m); } EntityAttributeClearer clearAnnot = m.getAnnotation(EntityAttributeClearer.class); if (clearAnnot != null) { clearers.put(clearAnnot.value(), m); } } AttrMethod[] ael = new AttrMethod[setters.size()]; int attrIdx = 0; for (Map.Entry<TypedName<?>, Method> ce : setters.entrySet()) { TypedName<?> attr = ce.getKey(); Method smethod = ce.getValue(); Class smVtype = smethod.getParameterTypes()[0]; Method cmethod = clearers.get(ce.getKey().getName()); ClassNode cn = new ClassNode(); cn.name = String.format("org/lenskit/generated/entities/AttrSet$$%s$$%s", type.getName().replace('.', '$'), ce.getValue().getName()); cn.access = ACC_PUBLIC; cn.version = V1_8; Class<? extends AttrMethod> sc; if (attr.getRawType().equals(Long.class) && smVtype.equals(long.class)) { sc = LongAttrMethod.class; } else if (attr.getRawType().equals(Double.class) && smVtype.equals(double.class)) { sc = DoubleAttrMethod.class; } else { sc = AttrMethod.class; } cn.superName = getInternalName(sc); MethodNode ctor = generateBeanConstructor(sc); cn.methods.add(ctor); MethodNode setter = generateSetter(type, smethod); cn.methods.add(setter); if (attr.getRawType().equals(Long.class) && smVtype.equals(long.class)) { cn.methods.add(generateLongSetter(type, smethod)); } if (attr.getRawType().equals(Double.class) && smVtype.equals(double.class)) { cn.methods.add(generateDoubleSetter(type, smethod)); } MethodNode clearer = generateClearer(type, smethod, cmethod); cn.methods.add(clearer); Class<? extends AttrMethod> cls = dlc.defineClass(cn).asSubclass(AttrMethod.class); try { ael[attrIdx++] = ConstructorUtils.invokeConstructor(cls, attr); } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new RuntimeException("cannot instantiate " + cls, e); } } cache.put(type, ael); return ael; }
From source file:org.mutabilitydetector.checkers.settermethod.AbstractSetterMethodChecker.java
License:Apache License
public AbstractSetterMethodChecker() { candidatesInitialisersMapping = CandidatesInitialisersMapping.newInstance(); classNode = new ClassNode(); enhancedClassNode = null; }
From source file:org.neptunepowered.vanilla.launch.transformer.AccessTransformer.java
License:MIT License
@Override public byte[] transform(String name, String transformedName, byte[] bytes) { if (bytes == null || !this.modifiers.containsKey(transformedName)) { return bytes; }/*from w w w .j a va 2 s . c o m*/ ClassNode classNode = new ClassNode(); ClassReader reader = new ClassReader(bytes); reader.accept(classNode, 0); for (Modifier m : this.modifiers.get(transformedName)) { if (m.isClass) { // Class classNode.access = m.transform(classNode.access); } else if (m.desc == null) { // Field for (FieldNode fieldNode : classNode.fields) { if (m.wildcard || fieldNode.name.equals(m.name)) { fieldNode.access = m.transform(fieldNode.access); if (!m.wildcard) { break; } } } } else { List<MethodNode> overridable = null; for (MethodNode methodNode : classNode.methods) { if (m.wildcard || (methodNode.name.equals(m.name) && methodNode.desc.equals(m.desc))) { boolean wasPrivate = (methodNode.access & ACC_PRIVATE) != 0; methodNode.access = m.transform(methodNode.access); // Constructors always use INVOKESPECIAL // if we changed from private to something else we need to replace all INVOKESPECIAL calls to // this method with INVOKEVIRTUAL // so that overridden methods will be called. Only need to scan this class, because obviously // the method was private. if (!methodNode.name.equals("<init>") && wasPrivate && (methodNode.access & ACC_PRIVATE) == 0) { if (overridable == null) { overridable = Lists.newArrayListWithExpectedSize(3); } overridable.add(methodNode); } if (!m.wildcard) { break; } } } if (overridable != null) { for (MethodNode methodNode : classNode.methods) { for (Iterator<AbstractInsnNode> itr = methodNode.instructions.iterator(); itr.hasNext();) { AbstractInsnNode insn = itr.next(); if (insn.getOpcode() == INVOKESPECIAL) { MethodInsnNode mInsn = (MethodInsnNode) insn; for (MethodNode replace : overridable) { if (replace.name.equals(mInsn.name) && replace.desc.equals(mInsn.desc)) { mInsn.setOpcode(INVOKEVIRTUAL); break; } } } } } } } } ClassWriter writer = new ClassWriter(0); classNode.accept(writer); return writer.toByteArray(); }
From source file:org.neptunepowered.vanilla.launch.transformer.DeobfuscationTransformer.java
License:MIT License
private String getFieldType(String owner, String name) { Map<String, String> fieldDescriptions = this.fieldDescriptions.get(owner); if (fieldDescriptions != null) { return fieldDescriptions.get(name); }/* w w w . j av a 2s . co m*/ byte[] bytes = getBytes(owner); if (bytes == null) { return null; } ClassReader reader = new ClassReader(bytes); ClassNode classNode = new ClassNode(); reader.accept(classNode, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); String result = null; fieldDescriptions = Maps.newHashMapWithExpectedSize(classNode.fields.size()); for (FieldNode fieldNode : classNode.fields) { fieldDescriptions.put(fieldNode.name, fieldNode.desc); if (fieldNode.name.equals(name)) { result = fieldNode.desc; } } this.fieldDescriptions.put(owner, fieldDescriptions); return result; }
From source file:org.qkit.core.asm.adapters.AddCustomGetterAdapter.java
License:Open Source License
public AddCustomGetterAdapter(final ClassVisitor cv, final String fieldName, final String fieldDescriptor, final String getterDesc, final String getterName, final int retInsn) { super(ASM4, new ClassNode()); next = cv;//from w ww . j a v a 2s.co m this.fieldName = fieldName; this.getterName = getterName; this.retInsn = retInsn; this.fieldDescriptor = fieldDescriptor; this.getterDesc = getterDesc; }
From source file:org.qkit.core.asm.adapters.AddCustomGraphicsAdapter.java
License:Open Source License
public AddCustomGraphicsAdapter(final ClassVisitor cv, String targetClazzName) { super(ASM4, new ClassNode()); next = cv;/* w ww. j a va 2 s .c o m*/ this.targetClazzName = targetClazzName; }
From source file:org.qkit.core.asm.adapters.AddGetterAdapter.java
License:Open Source License
public AddGetterAdapter(final ClassVisitor cv, final String fieldName, final String fieldDescriptor, final String getterName, final int retInsn) { super(ASM4, new ClassNode()); next = cv;/*from w ww. ja v a 2 s .c o m*/ this.fieldName = fieldName; this.getterName = getterName; this.retInsn = retInsn; this.fieldDescriptor = fieldDescriptor; }