List of usage examples for org.objectweb.asm.commons Method getName
public String getName()
From source file:com.mogujie.instantrun.IncrementalChangeVisitor.java
License:Apache License
public void addSupportMethod() { int access = Opcodes.ACC_PUBLIC; Method m = new Method("isSupport", "(I)Z"); MethodVisitor mv = super.visitMethod(access, m.getName(), m.getDescriptor(), null, null); mv.visitCode();/*from w w w . j a va 2s. c o m*/ mv.visitVarInsn(Opcodes.ALOAD, 1); // mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false); int[] hashArray = new int[fixMtds.size()]; Label[] labelArray = new Label[fixMtds.size()]; Label l0 = new Label(); Label l1 = new Label(); for (int i = 0; i < fixMtds.size(); i++) { hashArray[i] = AcesoProguardMap.instance().getClassData(visitedClassName).getMtdIndex(fixMtds.get(i)); labelArray[i] = l0; } mv.visitLookupSwitchInsn(l1, hashArray, labelArray); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IRETURN); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(Opcodes.ICONST_0); mv.visitInsn(Opcodes.IRETURN); mv.visitMaxs(1, 2); mv.visitEnd(); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodVariables.java
License:Apache License
void invokeConstructor(final InsnList instructions, final Type type, final Method method) { String owner = type.getSort() == Type.ARRAY ? type.getDescriptor() : type.getInternalName(); instructions.add(//from w w w . ja va2s.c o m new MethodInsnNode(Opcodes.INVOKESPECIAL, owner, method.getName(), method.getDescriptor(), false)); }
From source file:com.xruby.runtime.lang.util.RubyTypeFactory.java
License:BSD License
private CgMethodItem createMethodItem(RubyLevelMethod annotation, java.lang.reflect.Method method) { CgMethodItem item = makeMethodItemPros(method); item.javaName = method.getName(); makeGenneralItem(annotation, item);/*from ww w . j a va 2 s. c o m*/ return item; }
From source file:com.xruby.runtime.lang.util.RubyTypeFactory.java
License:BSD License
private CgMethodItem createAllocItem(RubyAllocMethod annotation, java.lang.reflect.Method method) { CgMethodItem item = makeMethodItemPros(method); item.name = null;//from ww w . j av a 2 s . co m item.javaName = method.getName(); item.alias = null; return item; }
From source file:de.bodden.tamiflex.playout.ReflectionMonitor.java
License:Open Source License
public ReflectionMonitor(String instruments, boolean verbose) { List<String> split = new ArrayList<>(Arrays.asList(instruments.split("[ ]+"))); Collections.sort(split);// w ww .jav a2 s. co m if (verbose) { System.out.println("\nActive instruments:"); } for (String className : split) { className = className.trim(); if (className.isEmpty()) { continue; } try { @SuppressWarnings("unchecked") Class<AbstractTransformation> c = (Class<AbstractTransformation>) Class.forName(className); AbstractTransformation transform = c.newInstance(); transformations.add(transform); if (verbose) { System.out.print(className); System.out.println(": "); for (Method m : transform.getAffectedMethods()) { System.out.print(" "); System.out.print(transform.getAffectedClass().getName() + "." + m.getName() + m.getDescriptor() + "\n"); } } } catch (Exception e) { throw new RuntimeException("There was an error instantiating the instrument " + className, e); } } }
From source file:de.thetaphi.forbiddenapis.Checker.java
License:Apache License
/** Adds the method signature to the list of disallowed methods. The Signature is checked against the given ClassLoader. */ private void addSignature(final String line, final String defaultMessage, final boolean failOnUnresolvableSignatures) throws ParseException { final String clazz, field, signature, message; final Method method; int p = line.indexOf('@'); if (p >= 0) { signature = line.substring(0, p).trim(); message = line.substring(p + 1).trim(); } else {//from w w w.ja va 2 s. co m signature = line; message = defaultMessage; } p = signature.indexOf('#'); if (p >= 0) { clazz = signature.substring(0, p); final String s = signature.substring(p + 1); p = s.indexOf('('); if (p >= 0) { if (p == 0) { throw new ParseException("Invalid method signature (method name missing): " + signature); } // we ignore the return type, its just to match easier (so return type is void): try { method = Method.getMethod("void " + s, true); } catch (IllegalArgumentException iae) { throw new ParseException("Invalid method signature: " + signature); } field = null; } else { field = s; method = null; } } else { clazz = signature; method = null; field = null; } // create printout message: final String printout = (message != null && message.length() > 0) ? (signature + " [" + message + "]") : signature; // check class & method/field signature, if it is really existent (in classpath), but we don't really load the class into JVM: if (AsmUtils.isGlob(clazz)) { if (method != null || field != null) { throw new ParseException(String.format(Locale.ENGLISH, "Class level glob pattern cannot be combined with methods/fields: %s", signature)); } forbiddenClassPatterns.add(new ClassPatternRule(clazz, printout)); } else { final ClassSignature c; try { c = getClassFromClassLoader(clazz); } catch (ClassNotFoundException cnfe) { reportParseFailed(failOnUnresolvableSignatures, cnfe.getMessage(), signature); return; } if (method != null) { assert field == null; // list all methods with this signature: boolean found = false; for (final Method m : c.methods) { if (m.getName().equals(method.getName()) && Arrays.equals(m.getArgumentTypes(), method.getArgumentTypes())) { found = true; forbiddenMethods.put(c.className + '\000' + m, printout); // don't break when found, as there may be more covariant overrides! } } if (!found) { reportParseFailed(failOnUnresolvableSignatures, "Method not found", signature); return; } } else if (field != null) { assert method == null; if (!c.fields.contains(field)) { reportParseFailed(failOnUnresolvableSignatures, "Field not found", signature); return; } forbiddenFields.put(c.className + '\000' + field, printout); } else { assert field == null && method == null; // only add the signature as class name forbiddenClasses.put(c.className, printout); } } }
From source file:dodola.anole.lib.IncrementalChangeVisitor.java
License:Apache License
/** * To each class, add the dispatch method called by the original code that acts as a trampoline to * invoke the changed methods./*from w w w. j a v a2s . c om*/ * <p/> * Pseudo code: * <code> * Object access$dispatch(String name, object[] args) { * if (name.equals( * "firstMethod.(L$type;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;")) { * return firstMethod(($type)arg[0], (String)arg[1], arg[2]); * } * if (name.equals("secondMethod.(L$type;Ljava/lang/String;I;)V")) { * secondMethod(($type)arg[0], (String)arg[1], (int)arg[2]); * return; * } * ... * StringBuilder $local1 = new StringBuilder(); * $local1.append("Method not found "); * $local1.append(name); * $local1.append(" in " + visitedClassName + * "$dispatch implementation, restart the application"); * throw new $package/InstantReloadException($local1.toString()); * } * </code> */ private void addDispatchMethod() { int access = Opcodes.ACC_PUBLIC | Opcodes.ACC_VARARGS; Method m = new Method("access$dispatch", "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;"); MethodVisitor visitor = super.visitMethod(access, m.getName(), m.getDescriptor(), null, null); final GeneratorAdapter mv = new GeneratorAdapter(access, m, visitor); if (TRACING_ENABLED) { mv.push("Redirecting "); mv.loadArg(0); trace(mv, 2); } List<MethodNode> allMethods = new ArrayList<MethodNode>(); // if we are disabled, do not generate any dispatch, the method will throw an exception // if invoked which should never happen. if (!instantRunDisabled) { //noinspection unchecked allMethods.addAll(classNode.methods); allMethods.addAll(addedMethods); } final Map<String, MethodNode> methods = new HashMap<String, MethodNode>(); for (MethodNode methodNode : allMethods) { if (methodNode.name.equals("<clinit>") || methodNode.name.equals("<init>")) { continue; } if (!isAccessCompatibleWithInstantRun(methodNode.access)) { continue; } methods.put(methodNode.name + "." + methodNode.desc, methodNode); } new StringSwitch() { @Override void visitString() { mv.visitVarInsn(Opcodes.ALOAD, 1); } @Override void visitCase(String methodName) { MethodNode methodNode = methods.get(methodName); String name = methodNode.name; boolean isStatic = (methodNode.access & Opcodes.ACC_STATIC) != 0; String newDesc = computeOverrideMethodDesc(methodNode.desc, isStatic); if (TRACING_ENABLED) { trace(mv, "M: " + name + " P:" + newDesc); } Type[] args = Type.getArgumentTypes(newDesc); int argc = 0; for (Type t : args) { mv.visitVarInsn(Opcodes.ALOAD, 2); mv.push(argc); mv.visitInsn(Opcodes.AALOAD); ByteCodeUtils.unbox(mv, t); argc++; } mv.visitMethodInsn(Opcodes.INVOKESTATIC, visitedClassName + "$override", isStatic ? computeOverrideMethodName(name, methodNode.desc) : name, newDesc, false); Type ret = Type.getReturnType(methodNode.desc); if (ret.getSort() == Type.VOID) { mv.visitInsn(Opcodes.ACONST_NULL); } else { mv.box(ret); } mv.visitInsn(Opcodes.ARETURN); } @Override void visitDefault() { writeMissingMessageWithHash(mv, visitedClassName); } }.visit(mv, methods.keySet()); mv.visitMaxs(0, 0); mv.visitEnd(); super.visitEnd(); }
From source file:dodola.anole.lib.IncrementalSupportVisitor.java
License:Apache License
/*** * Inserts a trampoline to this class so that the updated methods can make calls to super * class methods.//from w ww . j a va2 s. c o m * <p/> * Pseudo code for this trampoline: * <code> * Object access$super($classType instance, String name, object[] args) { * switch(name) { * case "firstMethod.(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;": * return super~instance.firstMethod((String)arg[0], arg[1]); * case "secondMethod.(Ljava/lang/String;I)V": * return super~instance.firstMethod((String)arg[0], arg[1]); * <p> * default: * StringBuilder $local1 = new StringBuilder(); * $local1.append("Method not found "); * $local1.append(name); * $local1.append(" in " $classType $super implementation"); * throw new $package/InstantReloadException($local1.toString()); * } * </code> */ private void createAccessSuper() { int access = Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_VARARGS; Method m = new Method("access$super", "(L" + visitedClassName + ";Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;"); MethodVisitor visitor = super.visitMethod(access, m.getName(), m.getDescriptor(), null, null); final GeneratorAdapter mv = new GeneratorAdapter(access, m, visitor); // Gather all methods from itself and its superclasses to generate a giant access$super // implementation. // This will work fine as long as we don't support adding methods to a class. final Map<String, MethodReference> uniqueMethods = new HashMap<String, MethodReference>(); if (parentNodes.isEmpty()) { // if we cannot determine the parents for this class, let's blindly add all the // method of the current class as a gateway to a possible parent version. addAllNewMethods(uniqueMethods, classNode); } else { // otherwise, use the parent list. for (ClassNode parentNode : parentNodes) { addAllNewMethods(uniqueMethods, parentNode); } } new StringSwitch() { @Override void visitString() { mv.visitVarInsn(Opcodes.ALOAD, 1); } @Override void visitCase(String methodName) { MethodReference methodRef = uniqueMethods.get(methodName); mv.visitVarInsn(Opcodes.ALOAD, 0); Type[] args = Type.getArgumentTypes(methodRef.method.desc); int argc = 0; for (Type t : args) { mv.visitVarInsn(Opcodes.ALOAD, 2); mv.push(argc); mv.visitInsn(Opcodes.AALOAD); ByteCodeUtils.unbox(mv, t); argc++; } if (TRACING_ENABLED) { trace(mv, "super selected ", methodRef.owner.name, methodRef.method.name, methodRef.method.desc); } // Call super on the other object, yup this works cos we are on the right place to // call from. mv.visitMethodInsn(Opcodes.INVOKESPECIAL, methodRef.owner.name, methodRef.method.name, methodRef.method.desc, false); Type ret = Type.getReturnType(methodRef.method.desc); if (ret.getSort() == Type.VOID) { mv.visitInsn(Opcodes.ACONST_NULL); } else { mv.box(ret); } mv.visitInsn(Opcodes.ARETURN); } @Override void visitDefault() { writeMissingMessageWithHash(mv, visitedClassName); } }.visit(mv, uniqueMethods.keySet()); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:dodola.anole.lib.IncrementalSupportVisitor.java
License:Apache License
/*** * Inserts a trampoline to this class so that the updated methods can make calls to * constructors./*from w ww.ja va2 s . c om*/ * <p> * <p/> * Pseudo code for this trampoline: * <code> * ClassName(Object[] args, Marker unused) { * String name = (String) args[0]; * if (name.equals( * "java/lang/ClassName.(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;")) { * this((String)arg[1], arg[2]); * return * } * if (name.equals("SuperClassName.(Ljava/lang/String;I)V")) { * super((String)arg[1], (int)arg[2]); * return; * } * ... * StringBuilder $local1 = new StringBuilder(); * $local1.append("Method not found "); * $local1.append(name); * $local1.append(" in " $classType $super implementation"); * throw new $package/InstantReloadException($local1.toString()); * } * </code> */ private void createDispatchingThis() { // Gather all methods from itself and its superclasses to generate a giant constructor // implementation. // This will work fine as long as we don't support adding constructors to classes. final Map<String, MethodNode> uniqueMethods = new HashMap<String, MethodNode>(); addAllNewConstructors(uniqueMethods, classNode, true /*keepPrivateConstructors*/); for (ClassNode parentNode : parentNodes) { addAllNewConstructors(uniqueMethods, parentNode, false /*keepPrivateConstructors*/); } int access = Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC; Method m = new Method(AsmUtils.CONSTRUCTOR, ConstructorArgsRedirection.DISPATCHING_THIS_SIGNATURE); MethodVisitor visitor = super.visitMethod(0, m.getName(), m.getDescriptor(), null, null); final GeneratorAdapter mv = new GeneratorAdapter(access, m, visitor); mv.visitCode(); // Mark this code as redirection code Label label = new Label(); mv.visitLineNumber(0, label); // Get and store the constructor canonical name. mv.visitVarInsn(Opcodes.ALOAD, 1); mv.push(0); mv.visitInsn(Opcodes.AALOAD); mv.unbox(Type.getType("Ljava/lang/String;")); final int constructorCanonicalName = mv.newLocal(Type.getType("Ljava/lang/String;")); mv.storeLocal(constructorCanonicalName); new StringSwitch() { @Override void visitString() { mv.loadLocal(constructorCanonicalName); } @Override void visitCase(String canonicalName) { MethodNode methodNode = uniqueMethods.get(canonicalName); String owner = canonicalName.split("\\.")[0]; // Parse method arguments and mv.visitVarInsn(Opcodes.ALOAD, 0); Type[] args = Type.getArgumentTypes(methodNode.desc); int argc = 0; for (Type t : args) { mv.visitVarInsn(Opcodes.ALOAD, 1); mv.push(argc + 1); mv.visitInsn(Opcodes.AALOAD); ByteCodeUtils.unbox(mv, t); argc++; } mv.visitMethodInsn(Opcodes.INVOKESPECIAL, owner, AsmUtils.CONSTRUCTOR, methodNode.desc, false); mv.visitInsn(Opcodes.RETURN); } @Override void visitDefault() { writeMissingMessageWithHash(mv, visitedClassName); } }.visit(mv, uniqueMethods.keySet()); mv.visitMaxs(1, 3); mv.visitEnd(); }
From source file:dyco4j.instrumentation.LoggingHelper.java
License:BSD License
private static void emitInvokeLog(final MethodVisitor mv, final Method method) { mv.visitMethodInsn(Opcodes.INVOKESTATIC, LOGGER, method.getName(), method.getDescriptor(), false); }