List of usage examples for org.objectweb.asm.commons Method getName
public String getName()
From source file:org.apache.deltaspike.partialbean.impl.proxy.AsmProxyClassGenerator.java
License:Apache License
private static void defineMethod(ClassWriter cw, java.lang.reflect.Method method, Type proxyType, Type invocationHandlerType, Type superType, boolean callInvocationHandler) { Type methodType = Type.getType(method); Type[] exceptionTypes = getTypes(method.getExceptionTypes()); // push the method definition int modifiers = (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED) & method.getModifiers(); Method asmMethod = Method.getMethod(method); GeneratorAdapter mg = new GeneratorAdapter(modifiers, asmMethod, null, exceptionTypes, cw); // copy annotations for (Annotation annotation : method.getDeclaredAnnotations()) { mg.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true).visitEnd(); }/* w w w.ja v a 2s . c o m*/ mg.visitCode(); Label tryBlockStart = mg.mark(); mg.loadThis(); loadCurrentMethod(mg, method, methodType); loadArguments(mg, method, methodType); // invoke our ProxyInvocationHandler mg.invokeStatic(Type.getType(ManualInvocationHandler.class), Method.getMethod("Object staticInvoke(Object, java.lang.reflect.Method, Object[])")); // cast the result mg.unbox(methodType.getReturnType()); Label tryBlockEnd = mg.mark(); // push return mg.returnValue(); boolean throwableCatched = false; // catch ProceedOriginalRuntimeException Label proceedOriginal = mg.mark(); if (callInvocationHandler) { // call stored InvocationHandler mg.loadThis(); mg.getField(proxyType, FIELDNAME_HANDLER, invocationHandlerType); mg.loadThis(); loadCurrentMethod(mg, method, methodType); loadArguments(mg, method, methodType); mg.invokeVirtual(invocationHandlerType, Method.getMethod("Object invoke(Object, java.lang.reflect.Method, Object[])")); mg.unbox(methodType.getReturnType()); mg.returnValue(); } else { // call super method mg.loadThis(); mg.loadArgs(); mg.visitMethodInsn(Opcodes.INVOKESPECIAL, superType.getInternalName(), method.getName(), Type.getMethodDescriptor(method), false); mg.returnValue(); } mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, proceedOriginal, Type.getInternalName(ProceedOriginalMethodException.class)); // catch declared exceptions if (exceptionTypes.length > 0) { Label rethrow = mg.mark(); mg.visitVarInsn(Opcodes.ASTORE, 1); mg.visitVarInsn(Opcodes.ALOAD, 1); mg.throwException(); // catch declared exceptions and rethrow it... for (Type exceptionType : exceptionTypes) { if (exceptionType.getClassName().equals(Throwable.class.getName())) { throwableCatched = true; } mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, rethrow, exceptionType.getInternalName()); } } if (!throwableCatched) { // catch Throwable and wrap it with a UndeclaredThrowableException Type uteType = Type.getType(UndeclaredThrowableException.class); Label wrapAndRethrow = mg.mark(); mg.visitVarInsn(Opcodes.ASTORE, 1); mg.newInstance(uteType); mg.dup(); mg.visitVarInsn(Opcodes.ALOAD, 1); mg.invokeConstructor(uteType, Method.getMethod("void <init>(java.lang.Throwable)")); mg.throwException(); mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, wrapAndRethrow, Type.getInternalName(Throwable.class)); } // finish the method mg.endMethod(); mg.visitMaxs(10, 10); mg.visitEnd(); }
From source file:org.apache.deltaspike.partialbean.impl.proxy.AsmProxyClassGenerator.java
License:Apache License
/** * Generates:// ww w . j a v a 2s. c om * <pre> * Method method = * method.getDeclaringClass().getMethod("methodName", new Class[] { args... }); * </pre> * @param mg * @param method * @param methodType */ private static void loadCurrentMethod(GeneratorAdapter mg, java.lang.reflect.Method method, Type methodType) { mg.push(Type.getType(method.getDeclaringClass())); mg.push(method.getName()); // create the Class[] mg.push(methodType.getArgumentTypes().length); mg.newArray(TYPE_CLASS); // push parameters into array for (int i = 0; i < methodType.getArgumentTypes().length; i++) { // keep copy of array on stack mg.dup(); // push index onto stack mg.push(i); mg.push(methodType.getArgumentTypes()[i]); mg.arrayStore(TYPE_CLASS); } // invoke getMethod() with the method name and the array of types mg.invokeVirtual(TYPE_CLASS, Method.getMethod("java.lang.reflect.Method getDeclaredMethod(String, Class[])")); }
From source file:org.apache.deltaspike.proxy.impl.AsmDeltaSpikeProxyClassGenerator.java
License:Apache License
private static void defineSuperAccessorMethod(ClassWriter cw, java.lang.reflect.Method method, Type superType, String superAccessorMethodSuffix) { Method originalAsmMethod = Method.getMethod(method); Method newAsmMethod = new Method(method.getName() + superAccessorMethodSuffix, originalAsmMethod.getReturnType(), originalAsmMethod.getArgumentTypes()); GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, newAsmMethod, null, null, cw); mg.visitCode();/*from w w w . j a v a 2 s.c o m*/ // call super method mg.loadThis(); mg.loadArgs(); mg.visitMethodInsn(Opcodes.INVOKESPECIAL, superType.getInternalName(), method.getName(), Type.getMethodDescriptor(method), false); mg.returnValue(); // finish the method mg.endMethod(); mg.visitMaxs(10, 10); mg.visitEnd(); }
From source file:org.apache.lucene.validation.ForbiddenApisCheckTask.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 signature) throws BuildException { final String clazz, field; final Method method; int 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 BuildException("Invalid method signature (method name missing): " + signature); }/*from w w w . j a va 2 s. co m*/ // 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 BuildException("Invalid method signature: " + signature); } field = null; } else { field = s; method = null; } } else { clazz = signature; method = null; field = null; } // check class & method/field signature, if it is really existent (in classpath), but we don't really load the class into JVM: final ClassSignatureLookup c = getClassFromClassLoader(clazz); 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.reader.getClassName() + '\000' + m, signature); // don't break when found, as there may be more covariant overrides! } } if (!found) { throw new BuildException("No method found with following signature: " + signature); } } else if (field != null) { assert method == null; if (!c.fields.contains(field)) { throw new BuildException("No field found with following name: " + signature); } forbiddenFields.put(c.reader.getClassName() + '\000' + field, signature); } else { assert field == null && method == null; // only add the signature as class name forbiddenClasses.put(c.reader.getClassName(), signature); } }
From source file:org.elasticsearch.painless.MethodWriter.java
License:Apache License
public MethodWriter(int access, Method method, ClassVisitor cw, BitSet statements, CompilerSettings settings) { super(Opcodes.ASM5, cw.visitMethod(access, method.getName(), method.getDescriptor(), null, null), access, method.getName(), method.getDescriptor()); this.statements = statements; this.settings = settings; }
From source file:org.elasticsearch.painless.ScriptInterface.java
License:Apache License
public ScriptInterface(Class<?> iface) { this.iface = iface; // Find the main method and the uses$argName methods java.lang.reflect.Method executeMethod = null; List<org.objectweb.asm.commons.Method> usesMethods = new ArrayList<>(); for (java.lang.reflect.Method m : iface.getMethods()) { if (m.isDefault()) { continue; }//from w w w.java 2 s. c om if (m.getName().equals("execute")) { if (executeMethod == null) { executeMethod = m; } else { throw new IllegalArgumentException( "Painless can only implement interfaces that have a single method named [execute] but [" + iface.getName() + "] has more than one."); } continue; } if (m.getName().startsWith("uses$")) { if (false == m.getReturnType().equals(boolean.class)) { throw new IllegalArgumentException( "Painless can only implement uses$ methods that return boolean but [" + iface.getName() + "#" + m.getName() + "] returns [" + m.getReturnType().getName() + "]."); } if (m.getParameterTypes().length > 0) { throw new IllegalArgumentException( "Painless can only implement uses$ methods that do not take parameters but [" + iface.getName() + "#" + m.getName() + "] does."); } usesMethods.add(new org.objectweb.asm.commons.Method(m.getName(), USES_PARAMETER_METHOD_TYPE.toMethodDescriptorString())); continue; } throw new IllegalArgumentException( "Painless can only implement methods named [execute] and [uses$argName] but [" + iface.getName() + "] contains a method named [" + m.getName() + "]"); } MethodType methodType = MethodType.methodType(executeMethod.getReturnType(), executeMethod.getParameterTypes()); this.executeMethod = new org.objectweb.asm.commons.Method(executeMethod.getName(), methodType.toMethodDescriptorString()); executeMethodReturnType = definitionTypeForClass(executeMethod.getReturnType(), componentType -> "Painless can only implement execute methods returning a whitelisted type but [" + iface.getName() + "#execute] returns [" + componentType.getName() + "] which isn't whitelisted."); // Look up the argument names Set<String> argumentNames = new LinkedHashSet<>(); List<MethodArgument> arguments = new ArrayList<>(); String[] argumentNamesConstant = readArgumentNamesConstant(iface); Class<?>[] types = executeMethod.getParameterTypes(); if (argumentNamesConstant.length != types.length) { throw new IllegalArgumentException("[" + iface.getName() + "#ARGUMENTS] has length [2] but [" + iface.getName() + "#execute] takes [1] argument."); } for (int arg = 0; arg < types.length; arg++) { arguments.add(methodArgument(types[arg], argumentNamesConstant[arg])); argumentNames.add(argumentNamesConstant[arg]); } this.executeArguments = unmodifiableList(arguments); // Validate that the uses$argName methods reference argument names for (org.objectweb.asm.commons.Method usesMethod : usesMethods) { if (false == argumentNames.contains(usesMethod.getName().substring("uses$".length()))) { throw new IllegalArgumentException( "Painless can only implement uses$ methods that match a parameter name but [" + iface.getName() + "#" + usesMethod.getName() + "] doesn't match any of " + argumentNames + "."); } } this.usesMethods = unmodifiableList(usesMethods); }
From source file:org.evosuite.stubs.StubClassVisitor.java
License:Open Source License
/** * Stubbed methods forward the query to the central Stubs class * and return whatever that class tells it to return * //from w ww .ja v a 2s. co m * @param mg * @param m */ private void createMethod(GeneratorAdapter mg, Method m) { String methodName = "getReturnValue" + getTypeName(m.getReturnType()); String desc = "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)" + getReturnTypeDesc(m.getReturnType()); mg.push(className); mg.push(m.getName() + m.getDescriptor()); mg.loadArgArray(); Type owner = Type.getType(PackageInfo.getNameWithSlash(Stubs.class)); Method method = new Method(methodName, desc); mg.invokeStatic(owner, method); insertReturnCast(mg, m); mg.returnValue(); mg.endMethod(); }
From source file:org.glowroot.agent.weaving.PluginClassRenamer.java
License:Apache License
private @Nullable Method hack(@Nullable Method method) { if (method == null) { return null; }// ww w.ja va 2 s. c om Type[] argumentTypes = method.getArgumentTypes(); Type[] hackedArgumentTypes = new Type[argumentTypes.length]; for (int i = 0; i < argumentTypes.length; i++) { hackedArgumentTypes[i] = hack(argumentTypes[i]); } return new Method(method.getName(), hack(method.getReturnType()), hackedArgumentTypes); }
From source file:org.glowroot.agent.weaving.WeavingClassVisitor.java
License:Apache License
@RequiresNonNull("type") private void addShim(ShimType shimType) { for (java.lang.reflect.Method reflectMethod : shimType.shimMethods()) { Method method = Method.getMethod(reflectMethod); Shim shim = reflectMethod.getAnnotation(Shim.class); checkNotNull(shim);// w w w .j a v a2 s . c o m Method targetMethod = Method.getMethod(shim.value()); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, method.getName(), method.getDescriptor(), null, null); mv.visitCode(); int i = 0; mv.visitVarInsn(ALOAD, i++); for (Type argumentType : method.getArgumentTypes()) { mv.visitVarInsn(argumentType.getOpcode(ILOAD), i++); } mv.visitMethodInsn(INVOKEVIRTUAL, type.getInternalName(), targetMethod.getName(), targetMethod.getDescriptor(), false); mv.visitInsn(method.getReturnType().getOpcode(IRETURN)); mv.visitMaxs(0, 0); mv.visitEnd(); } }
From source file:org.glowroot.agent.weaving.WeavingMethodVisitor.java
License:Apache License
private void defineAndEvaluateEnabledLocalVar(Advice advice) { Integer enabledLocal = null;// w w w. ja v a2s . c o m Method isEnabledAdvice = advice.isEnabledAdvice(); if (isEnabledAdvice != null) { loadMethodParameters(advice.isEnabledParameters(), 0, -1, advice.adviceType(), IsEnabled.class, false); visitMethodInsn(INVOKESTATIC, advice.adviceType().getInternalName(), isEnabledAdvice.getName(), isEnabledAdvice.getDescriptor(), false); enabledLocal = newLocal(Type.BOOLEAN_TYPE); enabledLocals.put(advice, enabledLocal); storeLocal(enabledLocal); } String nestingGroup = advice.pointcut().nestingGroup(); if (!nestingGroup.isEmpty() || advice.hasBindThreadContext() || advice.hasBindOptionalThreadContext()) { if (threadContextHolderLocal == null) { // need to define thread context local var outside of any branches, // but also don't want to load ThreadContext if enabledLocal exists and is false threadContextHolderLocal = newLocal(fastThreadLocalHolderType); visitInsn(ACONST_NULL); storeLocal(threadContextHolderLocal); threadContextLocal = newLocal(threadContextPlusType); visitInsn(ACONST_NULL); storeLocal(threadContextLocal); } } Integer prevNestingGroupIdLocal = null; if (!nestingGroup.isEmpty()) { // need to define thread context local var outside of any branches // but also don't want to load ThreadContext if enabledLocal exists and is false prevNestingGroupIdLocal = newLocal(Type.INT_TYPE); prevNestingGroupIdLocals.put(advice, prevNestingGroupIdLocal); visitIntInsn(BIPUSH, -1); storeLocal(prevNestingGroupIdLocal); } // futher calculations whether this @Pointcut is enabled.. if (!nestingGroup.isEmpty() || (advice.hasBindThreadContext() && !advice.hasBindOptionalThreadContext())) { Label disabledLabel = new Label(); if (enabledLocal != null) { loadLocal(enabledLocal); visitJumpInsn(IFEQ, disabledLabel); } else { enabledLocal = newLocal(Type.BOOLEAN_TYPE); enabledLocals.put(advice, enabledLocal); // temporary initial value to help with Java 7 stack frames visitInsn(ICONST_0); storeLocal(enabledLocal); } loadThreadContextHolder(); dup(); checkNotNull(threadContextHolderLocal); storeLocal(threadContextHolderLocal); visitMethodInsn(INVOKEVIRTUAL, fastThreadLocalHolderType.getInternalName(), "get", "()" + objectType.getDescriptor(), false); dup(); checkNotNull(threadContextLocal); storeLocal(threadContextLocal); if (advice.hasBindThreadContext() && !advice.hasBindOptionalThreadContext()) { visitJumpInsn(IFNULL, disabledLabel); if (!nestingGroup.isEmpty()) { checkNotNull(prevNestingGroupIdLocal); checkNestingGroupId(prevNestingGroupIdLocal, nestingGroup, disabledLabel); } } else { // this conditional covers !nestingGroup.isEmpty() Label enabledLabel = new Label(); // if thread context == null, then not in nesting group visitJumpInsn(IFNULL, enabledLabel); checkNotNull(prevNestingGroupIdLocal); checkNestingGroupId(prevNestingGroupIdLocal, nestingGroup, disabledLabel); visitLabel(enabledLabel); } visitInsn(ICONST_1); Label endLabel = new Label(); goTo(endLabel); visitLabel(disabledLabel); visitInsn(ICONST_0); visitLabel(endLabel); storeLocal(enabledLocal); } }