List of usage examples for org.objectweb.asm.commons Method Method
public Method(final String name, final String descriptor)
From source file:org.apache.commons.weaver.privilizer.PrivilizingVisitor.java
License:Apache License
@Override public void visitEnd() { annotate();//from ww w. j a v a2 s.co m if (privilizer().policy == Policy.ON_INIT) { final String fieldName = privilizer().generateName("hasSecurityManager"); visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, fieldName, Type.BOOLEAN_TYPE.getDescriptor(), null, null).visitEnd(); final GeneratorAdapter mgen = new GeneratorAdapter(Opcodes.ACC_STATIC, new Method("<clinit>", "()V"), null, Privilizer.EMPTY_TYPE_ARRAY, this); checkSecurityManager(mgen); mgen.putStatic(target, fieldName, Type.BOOLEAN_TYPE); mgen.returnValue(); mgen.endMethod(); } super.visitEnd(); }
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;//w w w . j a v a 2s . com @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.ehcache.impl.internal.store.offheap.AssertingOffHeapValueHolder.java
License:Apache License
private static boolean isLockedInFrame(StackTraceElement ste) { try {/*from w ww .ja v a2 s . com*/ ClassReader reader = new ClassReader(ste.getClassName()); NavigableMap<Integer, Integer> lockLevels = new TreeMap<>(); reader.accept(new ClassVisitor(ASM6) { @Override public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { if (ste.getMethodName().equals(name)) { return new InstructionAdapter(ASM6, new MethodVisitor(ASM6) { }) { private final Map<Label, Integer> levels = new HashMap<>(); private int lockLevel; @Override public void invokeinterface(String owner, String name, String descriptor) { if (LOCK_CLASS.equals(getObjectType(owner))) { if (LOCK_METHOD.equals(new Method(name, descriptor))) { lockLevel++; } else if (UNLOCK_METHOD.equals(new Method(name, descriptor))) { lockLevel--; } } } @Override public void visitJumpInsn(int opcode, Label label) { levels.merge(label, lockLevel, Integer::max); } @Override public void visitLabel(Label label) { lockLevel = levels.merge(label, lockLevel, Integer::max); } @Override public void visitLineNumber(int line, Label start) { lockLevels.merge(line, levels.get(start), Integer::max); } }; } else { return null; } } }, 0); Map.Entry<Integer, Integer> entry = lockLevels.floorEntry(ste.getLineNumber()); return entry.getValue() > 0; } catch (IOException e) { throw new AssertionError(e); } }
From source file:org.elasticsearch.painless.LambdaBootstrap.java
License:Apache License
/** * Generates a constructor that will take in captured * arguments if any and store them in their respective * member fields.//w w w. java 2 s. c o m */ private static void generateLambdaConstructor(ClassWriter cw, Type lambdaClassType, MethodType factoryMethodType, Capture[] captures) { String conDesc = factoryMethodType.changeReturnType(void.class).toMethodDescriptorString(); Method conMeth = new Method(CTOR_METHOD_NAME, conDesc); Type baseConType = Type.getType(Object.class); Method baseConMeth = new Method(CTOR_METHOD_NAME, MethodType.methodType(void.class).toMethodDescriptorString()); int modifiers = (captures.length > 0) ? ACC_PRIVATE : ACC_PUBLIC; GeneratorAdapter constructor = new GeneratorAdapter(modifiers, conMeth, cw.visitMethod(modifiers, CTOR_METHOD_NAME, conDesc, null, null)); constructor.visitCode(); constructor.loadThis(); constructor.invokeConstructor(baseConType, baseConMeth); for (int captureCount = 0; captureCount < captures.length; ++captureCount) { constructor.loadThis(); constructor.loadArg(captureCount); constructor.putField(lambdaClassType, captures[captureCount].name, captures[captureCount].type); } constructor.returnValue(); constructor.endMethod(); // Add a factory method, if lambda takes captures. // @uschindler says: I talked with Rmi Forax about this. Technically, a plain ctor // and a MethodHandle to the ctor would be enough - BUT: Hotspot is unable to // do escape analysis through a MethodHandles.findConstructor generated handle. // Because of this we create a factory method. With this factory method, the // escape analysis can figure out that everything is final and we don't need // an instance, so it can omit object creation on heap! if (captures.length > 0) { generateStaticCtorDelegator(cw, ACC_PUBLIC, LAMBDA_FACTORY_METHOD_NAME, lambdaClassType, factoryMethodType); } }
From source file:org.elasticsearch.painless.LambdaBootstrap.java
License:Apache License
/** * Generates a factory method to delegate to constructors. *//* w ww . ja va 2s .c o m*/ private static void generateStaticCtorDelegator(ClassWriter cw, int access, String delegatorMethodName, Type delegateClassType, MethodType delegateMethodType) { Method wrapperMethod = new Method(delegatorMethodName, delegateMethodType.toMethodDescriptorString()); Method constructorMethod = new Method(CTOR_METHOD_NAME, delegateMethodType.changeReturnType(void.class).toMethodDescriptorString()); int modifiers = access | ACC_STATIC; GeneratorAdapter factory = new GeneratorAdapter(modifiers, wrapperMethod, cw.visitMethod(modifiers, delegatorMethodName, delegateMethodType.toMethodDescriptorString(), null, null)); factory.visitCode(); factory.newInstance(delegateClassType); factory.dup(); factory.loadArgs(); factory.invokeConstructor(delegateClassType, constructorMethod); factory.returnValue(); factory.endMethod(); }
From source file:org.elasticsearch.painless.LambdaBootstrap.java
License:Apache License
/** * Generates the interface method that will delegate (call) to the delegate method * with {@code INVOKEDYNAMIC} using the {@link #delegateBootstrap} type converter. */// www. j a v a 2s . c o m private static void generateInterfaceMethod(ClassWriter cw, MethodType factoryMethodType, Type lambdaClassType, String interfaceMethodName, MethodType interfaceMethodType, Type delegateClassType, int delegateInvokeType, String delegateMethodName, MethodType delegateMethodType, Capture[] captures) throws LambdaConversionException { String lamDesc = interfaceMethodType.toMethodDescriptorString(); Method lamMeth = new Method(lambdaClassType.getInternalName(), lamDesc); int modifiers = ACC_PUBLIC; GeneratorAdapter iface = new GeneratorAdapter(modifiers, lamMeth, cw.visitMethod(modifiers, interfaceMethodName, lamDesc, null, null)); iface.visitCode(); // Loads any captured variables onto the stack. for (int captureCount = 0; captureCount < captures.length; ++captureCount) { iface.loadThis(); iface.getField(lambdaClassType, captures[captureCount].name, captures[captureCount].type); } // Loads any passed in arguments onto the stack. iface.loadArgs(); // Handles the case for a lambda function or a static reference method. // interfaceMethodType and delegateMethodType both have the captured types // inserted into their type signatures. This later allows the delegate // method to be invoked dynamically and have the interface method types // appropriately converted to the delegate method types. // Example: Integer::parseInt // Example: something.each(x -> x + 1) if (delegateInvokeType == H_INVOKESTATIC) { interfaceMethodType = interfaceMethodType.insertParameterTypes(0, factoryMethodType.parameterArray()); delegateMethodType = delegateMethodType.insertParameterTypes(0, factoryMethodType.parameterArray()); } else if (delegateInvokeType == H_INVOKEVIRTUAL || delegateInvokeType == H_INVOKEINTERFACE) { // Handles the case for a virtual or interface reference method with no captures. // delegateMethodType drops the 'this' parameter because it will be re-inserted // when the method handle for the dynamically invoked delegate method is created. // Example: Object::toString if (captures.length == 0) { Class<?> clazz = delegateMethodType.parameterType(0); delegateClassType = Type.getType(clazz); delegateMethodType = delegateMethodType.dropParameterTypes(0, 1); // Handles the case for a virtual or interface reference method with 'this' // captured. interfaceMethodType inserts the 'this' type into its // method signature. This later allows the delegate // method to be invoked dynamically and have the interface method types // appropriately converted to the delegate method types. // Example: something::toString } else if (captures.length == 1) { Class<?> clazz = factoryMethodType.parameterType(0); delegateClassType = Type.getType(clazz); interfaceMethodType = interfaceMethodType.insertParameterTypes(0, clazz); } else { throw new LambdaConversionException("unexpected number of captures [ " + captures.length + "]"); } } else { throw new IllegalStateException("unexpected invocation type [" + delegateInvokeType + "]"); } Handle delegateHandle = new Handle(delegateInvokeType, delegateClassType.getInternalName(), delegateMethodName, delegateMethodType.toMethodDescriptorString(), delegateInvokeType == H_INVOKEINTERFACE); iface.invokeDynamic(delegateMethodName, Type.getMethodType(interfaceMethodType.toMethodDescriptorString()).getDescriptor(), DELEGATE_BOOTSTRAP_HANDLE, delegateHandle); iface.returnValue(); iface.endMethod(); }
From source file:org.elasticsearch.painless.node.ECallLocal.java
License:Apache License
@Override void write(MethodWriter writer, Globals globals) { writer.writeDebugInfo(location);/*from www . j a v a 2 s. co m*/ for (AExpression argument : arguments) { argument.write(writer, globals); } writer.invokeStatic(CLASS_TYPE, new Method(method.name, method.methodType.toMethodDescriptorString())); }
From source file:org.elasticsearch.painless.WriterConstants.java
License:Apache License
private static Method getAsmMethod(final Class<?> rtype, final String name, final Class<?>... ptypes) { return new Method(name, MethodType.methodType(rtype, ptypes).toMethodDescriptorString()); }
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 . j a v a2 s .c o 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.evosuite.stubs.StubClassVisitor.java
License:Open Source License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { Method m = new Method(name, desc); GeneratorAdapter mg = new GeneratorAdapter(access, m, signature, new Type[0], this.cv); mg.visitCode();/*from w ww. j a v a 2 s . co m*/ if (name.equals("<init>")) createConstructor(mg, m); else createMethod(mg, m); return null; }