List of usage examples for org.objectweb.asm.commons GeneratorAdapter putStatic
public void putStatic(final Type owner, final String name, final Type type)
From source file:co.cask.cdap.internal.app.runtime.service.http.HttpHandlerGenerator.java
License:Apache License
/** * Generates a static Logger field for logging and a static initialization block to initialize the logger. *///w w w . j a va 2s . c om private void generateLogger(Type classType, ClassWriter classWriter) { // private static final Logger LOG = LoggerFactory.getLogger(classType); classWriter.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, "LOG", Type.getType(Logger.class).getDescriptor(), null, null); Method init = Methods.getMethod(void.class, "<clinit>"); GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_STATIC, init, null, null, classWriter); mg.visitLdcInsn(classType); mg.invokeStatic(Type.getType(LoggerFactory.class), Methods.getMethod(Logger.class, "getLogger", Class.class)); mg.putStatic(classType, "LOG", Type.getType(Logger.class)); mg.returnValue(); mg.endMethod(); }
From source file:com.xruby.compiler.codegen.RubyIDClassGenerator.java
License:BSD License
private static byte[] visitEnd() { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, RubyIDClassName, null, "java/lang/Object", null); Method staticBlock = Method.getMethod("void <clinit> ()V"); GeneratorAdapter staticBlockMg = new GeneratorAdapter(Opcodes.ACC_STATIC, staticBlock, null, null, cw); for (Map.Entry<String, String> e : idMap.entrySet()) { cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, e.getValue(), Types.RUBY_ID_TYPE.getDescriptor(), null, null);/*w w w .j a va2 s .com*/ staticBlockMg.push(e.getKey()); staticBlockMg.invokeStatic(Type.getType(RubyID.class), Method.getMethod("com.xruby.runtime.lang.RubyID intern(String)")); staticBlockMg.putStatic(Type.getType("L" + RubyIDClassName + ";"), e.getValue(), Types.RUBY_ID_TYPE); } staticBlockMg.returnValue(); staticBlockMg.endMethod(); cw.visitEnd(); clear(); return cw.toByteArray(); }
From source file:de.enough.polish.postcompile.java5.Java5ClassVisitor.java
License:Open Source License
public void visitEnd() { if (this.isEnumClass) { if (this.name_values == null) { throw new BuildException("This is not an enum class: " + this.classDesc); }//from ww w . ja v a 2s . c o m // Generate new <clinit> method. int numValues = EnumManager.getInstance().getNumEnumValues(this.classDesc); Method m = Method.getMethod("void <clinit> ()"); MethodVisitor mv = super.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); GeneratorAdapter mg = new GeneratorAdapter(ACC_STATIC, m, mv); mg.push(numValues); mg.newArray(Type.INT_TYPE); if (numValues <= 3) { for (int i = 1; i < numValues; i++) { mg.dup(); mg.push(i); mg.push(i); mg.arrayStore(Type.INT_TYPE); } } else { Label labelInitializeField = new Label(); Label labelCheck = new Label(); Label labelDone = new Label(); mg.push(1); mg.storeLocal(0, Type.INT_TYPE); mg.goTo(labelCheck); mg.mark(labelInitializeField); mg.dup(); mg.loadLocal(0, Type.INT_TYPE); mg.dup(); mg.arrayStore(Type.INT_TYPE); mg.iinc(0, 1); mg.mark(labelCheck); mg.loadLocal(0, Type.INT_TYPE); mg.push(numValues); mg.ifICmp(GeneratorAdapter.LT, labelInitializeField); mg.mark(labelDone); } mg.putStatic(Type.getType(this.classDesc), this.name_values, Type.getType(int[].class)); mg.returnValue(); mg.endMethod(); } // Called super implementation of this method to really close this class. super.visitEnd(); }
From source file:io.datakernel.codegen.VarField.java
License:Apache License
public static void setField(Context ctx, Type ownerType, String field, Type valueType, boolean isStatic) { GeneratorAdapter g = ctx.getGeneratorAdapter(); Class<?> valueClass = getJavaType(ctx.getClassLoader(), valueType); if (ctx.getThisType().equals(ownerType)) { Map<String, Class<?>> fieldNameToClass = (isStatic) ? ctx.getStaticFields() : ctx.getThisFields(); Class<?> fieldClass = fieldNameToClass.get(field); if (fieldClass == null) { throw new RuntimeException(format("No field \"%s\" in generated class %s. %s", field, ctx.getThisType().getClassName(), exceptionInGeneratedClass(ctx))); }//from w ww. ja v a 2 s. com Type fieldType = getType(fieldClass); cast(ctx, valueType, fieldType); if (isStatic) g.putStatic(ownerType, field, fieldType); else g.putField(ownerType, field, fieldType); return; } Class<?> argumentClass = getJavaType(ctx.getClassLoader(), ownerType); try { java.lang.reflect.Field javaField = argumentClass.getField(field); if (Modifier.isPublic(javaField.getModifiers()) && Modifier.isStatic(javaField.getModifiers()) == isStatic) { Type fieldType = getType(javaField.getType()); cast(ctx, valueType, fieldType); if (Modifier.isStatic(javaField.getModifiers())) g.putStatic(ownerType, field, fieldType); else g.putField(ownerType, field, fieldType); return; } } catch (NoSuchFieldException ignored) { } java.lang.reflect.Method javaSetter = tryFindSetter(argumentClass, field, valueClass); if (javaSetter == null && Primitives.isWrapperType(valueClass)) { javaSetter = tryFindSetter(argumentClass, field, Primitives.unwrap(valueClass)); } if (javaSetter == null && valueClass.isPrimitive()) { javaSetter = tryFindSetter(argumentClass, field, Primitives.wrap(valueClass)); } if (javaSetter == null) { javaSetter = tryFindSetter(argumentClass, field); } if (javaSetter != null) { Type fieldType = getType(javaSetter.getParameterTypes()[0]); cast(ctx, valueType, fieldType); invokeVirtualOrInterface(g, argumentClass, getMethod(javaSetter)); Type returnType = getType(javaSetter.getReturnType()); if (returnType.getSize() == 1) { g.pop(); } else if (returnType.getSize() == 2) { g.pop2(); } return; } throw new RuntimeException(format("No public field or setter for class %s for field \"%s\". %s ", ownerType.getClassName(), field, exceptionInGeneratedClass(ctx))); }
From source file:org.apache.aries.proxy.impl.common.AbstractWovenProxyAdapter.java
License:Apache License
/** * Create fields and an initialiser for {@link java.lang.reflect.Method} * objects in our class/*from w w w .j av a2 s. c om*/ */ private final void writeStaticInitMethod() { // we create a static field for each method we encounter with a *unique* // random name // since each method needs to be stored individually for (String methodStaticFieldName : transformedMethods.keySet()) { // add a private static field for the method cv.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC, methodStaticFieldName, METHOD_TYPE.getDescriptor(), null, null).visitEnd(); } GeneratorAdapter staticAdapter = new GeneratorAdapter(staticInitMethodFlags, staticInitMethod, null, null, cv); for (Entry<String, TypeMethod> entry : transformedMethods.entrySet()) { // Add some more code to the static initializer TypeMethod m = entry.getValue(); Type[] targetMethodParameters = m.method.getArgumentTypes(); String methodStaticFieldName = entry.getKey(); Label beginPopulate = staticAdapter.newLabel(); Label endPopulate = staticAdapter.newLabel(); Label catchHandler = staticAdapter.newLabel(); staticAdapter.visitTryCatchBlock(beginPopulate, endPopulate, catchHandler, THROWABLE_INAME); staticAdapter.mark(beginPopulate); staticAdapter.push(m.declaringClass); // push the method name string arg onto the stack staticAdapter.push(m.method.getName()); // create an array of the method parm class[] arg staticAdapter.push(targetMethodParameters.length); staticAdapter.newArray(CLASS_TYPE); int index = 0; for (Type t : targetMethodParameters) { staticAdapter.dup(); staticAdapter.push(index); staticAdapter.push(t); staticAdapter.arrayStore(CLASS_TYPE); index++; } // invoke the getMethod staticAdapter.invokeVirtual(CLASS_TYPE, new Method("getDeclaredMethod", METHOD_TYPE, new Type[] { STRING_TYPE, CLASS_ARRAY_TYPE })); // store the reflected method in the static field staticAdapter.putStatic(typeBeingWoven, methodStaticFieldName, METHOD_TYPE); Label afterCatch = staticAdapter.newLabel(); staticAdapter.mark(endPopulate); staticAdapter.goTo(afterCatch); staticAdapter.mark(catchHandler); // We don't care about the exception, so pop it off staticAdapter.pop(); // store the reflected method in the static field staticAdapter.visitInsn(ACONST_NULL); staticAdapter.putStatic(typeBeingWoven, methodStaticFieldName, METHOD_TYPE); staticAdapter.mark(afterCatch); } staticAdapter.returnValue(); staticAdapter.endMethod(); }
From source file:org.apache.commons.weaver.privilizer.PrivilizingVisitor.java
License:Apache License
@Override public void visitEnd() { annotate();//from w w w . ja v a 2 s . com 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.elasticsearch.painless.node.LField.java
License:Apache License
@Override void store(final CompilerSettings settings, final Definition definition, final GeneratorAdapter adapter) { if (java.lang.reflect.Modifier.isStatic(field.reflect.getModifiers())) { adapter.putStatic(field.owner.type, field.reflect.getName(), field.type.type); } else {// w w w .ja v a 2 s .c om adapter.putField(field.owner.type, field.reflect.getName(), field.type.type); } }
From source file:org.evosuite.testcase.FieldReference.java
License:Open Source License
@Override public void storeBytecode(GeneratorAdapter mg, Map<Integer, Integer> locals) { if (!isStatic()) { source.loadBytecode(mg, locals); mg.swap();/*from w ww . j a va 2s . c o m*/ mg.putField(org.objectweb.asm.Type.getType(source.getVariableClass()), field.getName(), org.objectweb.asm.Type.getType(getVariableClass())); } else { mg.putStatic(org.objectweb.asm.Type.getType(source.getVariableClass()), field.getName(), org.objectweb.asm.Type.getType(getVariableClass())); } }
From source file:org.jacoco.core.runtime.OfflineInstrumentationAccessGeneratorTest.java
License:Open Source License
/** * Creates a new class with the given id, loads this class and instantiates * it. The constructor of the generated class will request the probe array * from the access generator under test. *//*from www.ja va 2 s .c om*/ private ITarget generateAndInstantiateClass(int classid) throws InstantiationException, IllegalAccessException { final String className = "org/jacoco/test/targets/RuntimeTestTarget_" + classid; Type classType = Type.getObjectType(className); final ClassWriter writer = new ClassWriter(0); writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", new String[] { Type.getInternalName(ITarget.class) }); writer.visitField(InstrSupport.DATAFIELD_ACC, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC, null, null); // Constructor GeneratorAdapter gen = new GeneratorAdapter( writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "<init>", "()V"); gen.visitCode(); gen.loadThis(); gen.invokeConstructor(Type.getType(Object.class), new Method("<init>", "()V")); gen.loadThis(); final int size = generator.generateDataAccessor(classid, className, 2, gen); gen.putStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC)); gen.returnValue(); gen.visitMaxs(size + 1, 0); gen.visitEnd(); // get() gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "get", "()[Z", null, new String[0]), Opcodes.ACC_PUBLIC, "get", "()[Z"); gen.visitCode(); gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC)); gen.returnValue(); gen.visitMaxs(1, 0); gen.visitEnd(); writer.visitEnd(); final TargetLoader loader = new TargetLoader(className.replace('/', '.'), writer.toByteArray()); return (ITarget) loader.newTargetInstance(); }
From source file:org.jacoco.core.runtime.RuntimeTestBase.java
License:Open Source License
/** * Creates a new class with the given id, loads this class and instantiates * it. The constructor of the generated class will request the probe array * from the runtime under test.// w ww. ja va 2 s . c o m */ private ITarget generateAndInstantiateClass(int classid) throws InstantiationException, IllegalAccessException { final String className = "org/jacoco/test/targets/RuntimeTestTarget_" + classid; Type classType = Type.getObjectType(className); final ClassWriter writer = new ClassWriter(0); writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", new String[] { Type.getInternalName(ITarget.class) }); writer.visitField(InstrSupport.DATAFIELD_ACC, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC, null, null); // Constructor GeneratorAdapter gen = new GeneratorAdapter( writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "<init>", "()V"); gen.visitCode(); gen.loadThis(); gen.invokeConstructor(Type.getType(Object.class), new Method("<init>", "()V")); gen.loadThis(); final int size = runtime.generateDataAccessor(classid, className, 2, gen); gen.putStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC)); gen.returnValue(); gen.visitMaxs(size + 1, 0); gen.visitEnd(); // get() gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "get", "()[Z", null, new String[0]), Opcodes.ACC_PUBLIC, "get", "()[Z"); gen.visitCode(); gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC)); gen.returnValue(); gen.visitMaxs(1, 0); gen.visitEnd(); // a() gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "a", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "a", "()V"); gen.visitCode(); gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC)); gen.push(0); gen.push(1); gen.arrayStore(Type.BOOLEAN_TYPE); gen.returnValue(); gen.visitMaxs(3, 0); gen.visitEnd(); // b() gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "b", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "b", "()V"); gen.visitCode(); gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC)); gen.push(1); gen.push(1); gen.arrayStore(Type.BOOLEAN_TYPE); gen.returnValue(); gen.visitMaxs(3, 0); gen.visitEnd(); writer.visitEnd(); final TargetLoader loader = new TargetLoader(className.replace('/', '.'), writer.toByteArray()); return (ITarget) loader.newTargetInstance(); }