Java Code Examples for org.objectweb.asm.ClassWriter#visitField()
The following examples show how to use
org.objectweb.asm.ClassWriter#visitField() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DynamicEntityGenerator.java From we-cmdb with Apache License 2.0 | 6 votes |
private static void writeField(ClassWriter classWriter, FieldNode field) { FieldVisitor fieldVisitor; if (field.isJoinNode()) { if (EntityRelationship.ManyToOne.equals(field.getEntityRelationship())) { fieldVisitor = classWriter.visitField(ACC_PRIVATE, field.getName(), field.getTypeDesc(), null, null); } else { // OneToMany or ManyToMany fieldVisitor = classWriter.visitField(ACC_PRIVATE, field.getName(), "Ljava/util/Set;", getTypeSiganitureForOneToMany(field.getTypeDesc()), null); } // ignore join field for json AnnotationVisitor annotationVisitor0 = fieldVisitor.visitAnnotation("Lcom/fasterxml/jackson/annotation/JsonIgnore;", true); annotationVisitor0.visitEnd(); } else { fieldVisitor = classWriter.visitField(ACC_PRIVATE, field.getName(), typeMap.get(field.getType()), null, null); } fieldVisitor.visitEnd(); }
Example 2
Source File: IntArrayFieldInitializer.java From bazel with Apache License 2.0 | 6 votes |
@Override public boolean writeFieldDefinition( ClassWriter cw, boolean isFinal, boolean annotateTransitiveFields) { int accessLevel = Opcodes.ACC_STATIC; if (visibility != Visibility.PRIVATE) { accessLevel |= Opcodes.ACC_PUBLIC; } if (isFinal) { accessLevel |= Opcodes.ACC_FINAL; } FieldVisitor fv = cw.visitField(accessLevel, fieldName, DESC, null, null); if (annotateTransitiveFields && dependencyInfo.dependencyType() == DependencyInfo.DependencyType.TRANSITIVE) { AnnotationVisitor av = fv.visitAnnotation( RClassGenerator.PROVENANCE_ANNOTATION_CLASS_DESCRIPTOR, /*visible=*/ true); av.visit(RClassGenerator.PROVENANCE_ANNOTATION_LABEL_KEY, dependencyInfo.label()); av.visitEnd(); } fv.visitEnd(); return true; }
Example 3
Source File: CallSiteGenerator.java From groovy with Apache License 2.0 | 6 votes |
public static byte[] genStaticMetaMethodSite(CachedMethod cachedMethod, ClassWriter cw, String name) { String internalName = name.replace('.', '/'); classHeader(cw, internalName, "org/codehaus/groovy/runtime/callsite/StaticMetaMethodSite"); cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "__constructor__", "Ljava/lang/reflect/Constructor;", null, null); genConstructor(cw, "org/codehaus/groovy/runtime/callsite/StaticMetaMethodSite", internalName); genCallXxxWithArray(cw, "", "org/codehaus/groovy/runtime/callsite/StaticMetaMethodSite", cachedMethod, "java/lang/Object"); genCallXxxWithArray(cw, "Static", "org/codehaus/groovy/runtime/callsite/StaticMetaMethodSite", cachedMethod, "java/lang/Class"); genCallWithFixedParams(cw, "", "org/codehaus/groovy/runtime/callsite/StaticMetaMethodSite", cachedMethod, "java/lang/Object"); genCallWithFixedParams(cw, "Static", "org/codehaus/groovy/runtime/callsite/StaticMetaMethodSite", cachedMethod, "java/lang/Class"); cw.visitEnd(); return cw.toByteArray(); }
Example 4
Source File: InterfaceFieldClassGeneratorTest.java From AVM with MIT License | 6 votes |
public static byte[] getNestedInterfaceCalledFIELDSLevelOne() { ClassWriter classWriter = new ClassWriter(0); FieldVisitor fieldVisitor; classWriter.visit(V10, ACC_ABSTRACT | ACC_INTERFACE, "NestedInterfaces$FIELDS", null, "java/lang/Object", null); classWriter.visitSource("NestedInterfaces.java", null); classWriter.visitInnerClass("NestedInterfaces$FIELDS", "NestedInterfaces", "FIELDS", ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE); classWriter.visitInnerClass("NestedInterfaces$FIELDS$FIELDS", "NestedInterfaces$FIELDS", "FIELDS", ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE); { fieldVisitor = classWriter.visitField(ACC_PUBLIC | ACC_FINAL | ACC_STATIC, "a", "I", null, Integer.valueOf(101)); fieldVisitor.visitEnd(); } classWriter.visitEnd(); return classWriter.toByteArray(); }
Example 5
Source File: WriteStyleablesProcessor.java From shrinker with Apache License 2.0 | 6 votes |
@Override public void proceed() { ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_SUPER, RSymbols.R_STYLEABLES_CLASS_NAME, null, "java/lang/Object", null); for (String name : symbols.getStyleables().keySet()) { writer.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, name, "[I", null, null); } writeClinit(writer); writer.visitEnd(); byte[] bytes = writer.toByteArray(); try { if (!dir.isDirectory() && !dir.mkdirs()) { throw new RuntimeException("Cannot mkdir " + dir); } Files.write(dir.toPath().resolve(RSymbols.R_STYLEABLES_CLASS_NAME + ".class"), bytes); } catch (IOException e) { throw new UncheckedIOException(e); } }
Example 6
Source File: CallSiteGenerator.java From groovy with Apache License 2.0 | 5 votes |
public static byte[] genPojoMetaMethodSite(CachedMethod cachedMethod, ClassWriter cw, String name) { String internalName = name.replace('.', '/'); classHeader(cw, internalName, "org/codehaus/groovy/runtime/callsite/PojoMetaMethodSite"); cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "__constructor__", "Ljava/lang/reflect/Constructor;", null, null); genConstructor(cw, "org/codehaus/groovy/runtime/callsite/PojoMetaMethodSite", internalName); genCallXxxWithArray(cw, "", "org/codehaus/groovy/runtime/callsite/PojoMetaMethodSite", cachedMethod, "java/lang/Object"); genCallWithFixedParams(cw, "", "org/codehaus/groovy/runtime/callsite/PojoMetaMethodSite", cachedMethod, "java/lang/Object"); cw.visitEnd(); return cw.toByteArray(); }
Example 7
Source File: GroupClassGenerator.java From grappa with Apache License 2.0 | 5 votes |
private static void generateFields(final InstructionGroup group, final ClassWriter cw) { // TODO: fix the below comment; those "two members" should be split // CAUTION: the FieldNode has illegal access flags and an illegal // value field since these two members are reused for other // purposes, so we need to write out the field "manually" here // rather than just call "field.accept(cw)" for (final FieldNode field: group.getFields()) cw.visitField(ACC_PUBLIC + ACC_SYNTHETIC, field.name, field.desc, null, null); }
Example 8
Source File: DynamicClassFactory.java From caravan with Apache License 2.0 | 5 votes |
private Class<?> createClass(ClassPair classPair) { String className = generateClassName(classPair); String classStr = className.replaceAll("\\.", "/"); String keyClassType = Type.getDescriptor(classPair.keyClass); String valueClassType = Type.getDescriptor(classPair.valueClass); ClassWriter cw = new ClassWriter(0); cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", new String[]{interfaceName}); AnnotationVisitor anno = cw.visitAnnotation(Type.getDescriptor(JsonPropertyOrder.class), true); AnnotationVisitor aa = anno.visitArray("value"); aa.visit("", "key"); aa.visit("", "value"); aa.visitEnd(); anno.visitEnd(); FieldVisitor keyField = cw.visitField(ACC_PRIVATE, "key", keyClassType, null, null); keyField.visitEnd(); FieldVisitor valueField = cw.visitField(ACC_PRIVATE, "value", valueClassType, null, null); valueField.visitEnd(); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitMaxs(2, 1); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false); // call the constructor of super class mv.visitInsn(RETURN); mv.visitEnd(); addGetterSetter(classPair, className, classStr, keyClassType, valueClassType, cw); addKVPairMethods(classPair, className, classStr, keyClassType, valueClassType, cw); cw.visitEnd(); return defineClass(className, cw.toByteArray()); }
Example 9
Source File: ClassGenerator.java From android-perftracking with MIT License | 5 votes |
/** * Creates AppPerformanceConfig.class file with a static boolean field AppPerformanceConfig#enabled in it. * * @param value Initializes the AppPerformanceConfig#enabled with value. * @return byte array of the class. */ public static byte[] generateConfigClass(boolean value) { ClassWriter cw = new ClassWriter(0); cw.visit(V1_6, ACC_FINAL + ACC_SUPER, "com/rakuten/tech/mobile/perf/runtime/internal/AppPerformanceConfig", null, "java/lang/Object", null); cw.visitField(ACC_PUBLIC + ACC_STATIC, "enabled", "Z", null, value); cw.visitEnd(); return cw.toByteArray(); }
Example 10
Source File: InterfaceFieldClassGeneratorTest.java From AVM with MIT License | 5 votes |
public static byte[] getNestedInterfaceCalledFIELDSLevelTwo() { ClassWriter classWriter = new ClassWriter(0); FieldVisitor fieldVisitor; MethodVisitor methodVisitor; classWriter.visit(V10, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, "NestedInterfaces$FIELDS$FIELDS", null, "java/lang/Object", null); classWriter.visitSource("NestedInterfaces.java", null); classWriter.visitInnerClass("NestedInterfaces$FIELDS", "NestedInterfaces", "FIELDS", ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE); classWriter.visitInnerClass("NestedInterfaces$FIELDS$FIELDS", "NestedInterfaces$FIELDS", "FIELDS", ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE); { fieldVisitor = classWriter.visitField(ACC_PUBLIC | ACC_FINAL | ACC_STATIC, "b", "I", null, null); fieldVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); methodVisitor.visitCode(); Label label0 = new Label(); methodVisitor.visitLabel(label0); methodVisitor.visitLineNumber(7, label0); methodVisitor.visitTypeInsn(NEW, "java/lang/Object"); methodVisitor.visitInsn(DUP); methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "hashCode", "()I", false); methodVisitor.visitFieldInsn(PUTSTATIC, "NestedInterfaces$FIELDS$FIELDS", "b", "I"); methodVisitor.visitInsn(RETURN); methodVisitor.visitMaxs(2, 0); methodVisitor.visitEnd(); } classWriter.visitEnd(); return classWriter.toByteArray(); }
Example 11
Source File: CrossCheckingSizeOfIT.java From sizeof with Apache License 2.0 | 5 votes |
private static Class<?> generateClassHierarchy(List<List<Class<?>>> classes) { TestClassLoader loader = new TestClassLoader(); String superClassDesc = "java/lang/Object"; int classIndex = 0; for (List<Class<?>> fields : classes) { String classDesc = "A" + classIndex++; ClassWriter cw = new ClassWriter(COMPUTE_MAXS | COMPUTE_FRAMES); cw.visit(V1_6, ACC_PUBLIC, classDesc, null, superClassDesc, null); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, superClassDesc, "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); int fieldIndex = 0; for (Class<?> field : fields) { cw.visitField(ACC_PUBLIC, "a" + fieldIndex++, Type.getDescriptor(field), null, null); } cw.visitEnd(); loader.defineClass(classDesc, cw.toByteArray()); superClassDesc = classDesc; } try { return loader.loadClass("A" + (classIndex - 1)); } catch (ClassNotFoundException | NoClassDefFoundError e) { throw new AssertionError(e); } }
Example 12
Source File: TestEnumForEclipseCompilerDump.java From AVM with MIT License | 4 votes |
public static byte[] generateBytecode() { ClassWriter classWriter = new ClassWriter(0); FieldVisitor fieldVisitor; MethodVisitor methodVisitor; classWriter.visit(V10, ACC_PUBLIC | ACC_FINAL | ACC_SUPER | ACC_ENUM, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "Ljava/lang/Enum<L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;>;", "java/lang/Enum", null); { fieldVisitor = classWriter.visitField(ACC_PUBLIC | ACC_FINAL | ACC_STATIC | ACC_ENUM, "TEST", "L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;", null, null); fieldVisitor.visitEnd(); } { fieldVisitor = classWriter.visitField(ACC_PRIVATE | ACC_FINAL | ACC_STATIC | ACC_SYNTHETIC, "ENUM$VALUES", "[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;", null, null); fieldVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); methodVisitor.visitCode(); methodVisitor.visitTypeInsn(NEW, PACKAGE_NAME_INTERNAL + "/TestEnumForValues"); methodVisitor.visitInsn(DUP); methodVisitor.visitLdcInsn("TEST"); methodVisitor.visitInsn(ICONST_0); methodVisitor.visitMethodInsn(INVOKESPECIAL, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "<init>", "(Ljava/lang/String;I)V", false); methodVisitor.visitFieldInsn(PUTSTATIC, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "TEST", "L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;"); methodVisitor.visitInsn(ICONST_1); methodVisitor.visitTypeInsn(ANEWARRAY, PACKAGE_NAME_INTERNAL + "/TestEnumForValues"); methodVisitor.visitInsn(DUP); methodVisitor.visitInsn(ICONST_0); methodVisitor.visitFieldInsn(GETSTATIC, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "TEST", "L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;"); methodVisitor.visitInsn(AASTORE); methodVisitor.visitFieldInsn(PUTSTATIC, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "ENUM$VALUES", "[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;"); methodVisitor.visitInsn(RETURN); methodVisitor.visitMaxs(4, 0); methodVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(ACC_PRIVATE, "<init>", "(Ljava/lang/String;I)V", null, null); methodVisitor.visitCode(); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitVarInsn(ILOAD, 2); methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Enum", "<init>", "(Ljava/lang/String;I)V", false); methodVisitor.visitInsn(RETURN); methodVisitor.visitMaxs(3, 3); methodVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "values", "()[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;", null, null); methodVisitor.visitCode(); methodVisitor.visitFieldInsn(GETSTATIC, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "ENUM$VALUES", "[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;"); methodVisitor.visitInsn(DUP); methodVisitor.visitVarInsn(ASTORE, 0); methodVisitor.visitInsn(ICONST_0); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitInsn(ARRAYLENGTH); methodVisitor.visitInsn(DUP); methodVisitor.visitVarInsn(ISTORE, 1); methodVisitor.visitTypeInsn(ANEWARRAY, PACKAGE_NAME_INTERNAL + "/TestEnumForValues"); methodVisitor.visitInsn(DUP); methodVisitor.visitVarInsn(ASTORE, 2); methodVisitor.visitInsn(ICONST_0); methodVisitor.visitVarInsn(ILOAD, 1); methodVisitor.visitMethodInsn(INVOKESTATIC, "java/lang/System", "arraycopy", "(Ljava/lang/Object;ILjava/lang/Object;II)V", false); methodVisitor.visitVarInsn(ALOAD, 2); methodVisitor.visitInsn(ARETURN); methodVisitor.visitMaxs(5, 3); methodVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "valueOf", "(Ljava/lang/String;)L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;", null, null); methodVisitor.visitCode(); methodVisitor.visitLdcInsn(Type.getType("L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;")); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitMethodInsn(INVOKESTATIC, "java/lang/Enum", "valueOf", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;", false); methodVisitor.visitTypeInsn(CHECKCAST, PACKAGE_NAME_INTERNAL + "/TestEnumForValues"); methodVisitor.visitInsn(ARETURN); methodVisitor.visitMaxs(2, 1); methodVisitor.visitEnd(); } classWriter.visitEnd(); return classWriter.toByteArray(); }
Example 13
Source File: AsmTest.java From atlas with Apache License 2.0 | 4 votes |
@Test public void test() throws Throwable { ClassReader cr = new ClassReader(Config.class.getName()); ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS); ClassVisitor cv = new MethodChangeClassAdapter(cw); cr.accept(cv, Opcodes.ASM5); //Add a new method MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "add", "([Ljava/lang/String;)V", null, null); // pushes the 'out' field (of type PrintStream) of the System class mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); // pushes the "Hello World!" String constant mw.visitLdcInsn("this is add method print!"); // invokes the 'println' method (defined in the PrintStream class) mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); mw.visitInsn(RETURN); // this code uses a maximum of two stack elements and two local // variables mw.visitMaxs(0, 0); mw.visitEnd(); //Type.getDescriptor(AdviceFlowOuterHolder.class) FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, "age", Type.INT_TYPE.toString(), null, 1); fv.visitEnd(); FieldVisitor fv2 = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, "name2", Type.getDescriptor(String.class), null, "name2"); fv2.visitEnd(); ModifyClassVisiter cv2 = new ModifyClassVisiter(Opcodes.ASM5); cv2.addRemoveField("name"); cr.accept(cv2, Opcodes.ASM5); FieldVisitor fv3 = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, "name", Type.getDescriptor(String.class), null, "name"); fv3.visitEnd(); byte[] code = cw.toByteArray(); File file = new File("Config.class"); System.out.println(file.getAbsolutePath()); FileOutputStream fos = new FileOutputStream(file); fos.write(code); fos.close(); }
Example 14
Source File: TestEnumForJavacDump.java From AVM with MIT License | 4 votes |
public static byte[] generateBytecode() { ClassWriter classWriter = new ClassWriter(0); FieldVisitor fieldVisitor; MethodVisitor methodVisitor; classWriter.visit(V10, ACC_PUBLIC | ACC_FINAL | ACC_SUPER | ACC_ENUM, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "Ljava/lang/Enum<L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;>;", "java/lang/Enum", null); { fieldVisitor = classWriter.visitField(ACC_PUBLIC | ACC_FINAL | ACC_STATIC | ACC_ENUM, "TEST", "L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;", null, null); fieldVisitor.visitEnd(); } { fieldVisitor = classWriter.visitField(ACC_PRIVATE | ACC_FINAL | ACC_STATIC | ACC_SYNTHETIC, "$VALUES", "[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;", null, null); fieldVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "values", "()[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;", null, null); methodVisitor.visitCode(); methodVisitor.visitFieldInsn(GETSTATIC, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "$VALUES", "[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;"); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;", "clone", "()Ljava/lang/Object;", false); methodVisitor.visitTypeInsn(CHECKCAST, "[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;"); methodVisitor.visitInsn(ARETURN); methodVisitor.visitMaxs(1, 0); methodVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "valueOf", "(Ljava/lang/String;)L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;", null, null); methodVisitor.visitCode(); methodVisitor.visitLdcInsn(Type.getType("L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;")); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitMethodInsn(INVOKESTATIC, "java/lang/Enum", "valueOf", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;", false); methodVisitor.visitTypeInsn(CHECKCAST, PACKAGE_NAME_INTERNAL + "/TestEnumForValues"); methodVisitor.visitInsn(ARETURN); methodVisitor.visitMaxs(2, 1); methodVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(ACC_PRIVATE, "<init>", "(Ljava/lang/String;I)V", "()V", null); methodVisitor.visitCode(); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitVarInsn(ILOAD, 2); methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Enum", "<init>", "(Ljava/lang/String;I)V", false); methodVisitor.visitInsn(RETURN); methodVisitor.visitMaxs(3, 3); methodVisitor.visitEnd(); } { methodVisitor = classWriter.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); methodVisitor.visitCode(); methodVisitor.visitTypeInsn(NEW, PACKAGE_NAME_INTERNAL + "/TestEnumForValues"); methodVisitor.visitInsn(DUP); methodVisitor.visitLdcInsn("TEST"); methodVisitor.visitInsn(ICONST_0); methodVisitor.visitMethodInsn(INVOKESPECIAL, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "<init>", "(Ljava/lang/String;I)V", false); methodVisitor.visitFieldInsn(PUTSTATIC, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "TEST", "L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;"); methodVisitor.visitInsn(ICONST_1); methodVisitor.visitTypeInsn(ANEWARRAY, PACKAGE_NAME_INTERNAL + "/TestEnumForValues"); methodVisitor.visitInsn(DUP); methodVisitor.visitInsn(ICONST_0); methodVisitor.visitFieldInsn(GETSTATIC, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "TEST", "L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;"); methodVisitor.visitInsn(AASTORE); methodVisitor.visitFieldInsn(PUTSTATIC, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "$VALUES", "[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;"); methodVisitor.visitInsn(RETURN); methodVisitor.visitMaxs(4, 0); methodVisitor.visitEnd(); } classWriter.visitEnd(); return classWriter.toByteArray(); }
Example 15
Source File: OnChangeAwaitBCGennerator.java From Concurnas with MIT License | 4 votes |
private void createStateObjectClass(String fileName, String fullname, FuncParams spliceInArgs, HashSet<String> stuffToNull, boolean isEvery, boolean isOnlyClosed){ //YUCK! this.bytecodeVisitor.localvarStackSize.push(0); this.bytecodeVisitor.localvarStack.push(new HashMap<String, Pair<Type, Integer>>()); ClassWriter cw = new ConcClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS, bytecodeVisitor.typeDirectory); cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER + ACC_SYNTHETIC, fullname, null, "java/lang/Object", null); {//Fields for(FuncParam o : spliceInArgs.params){ Type fieldType = Utils.copyRemoveGenericUpperBound(o.getTaggedType()); FieldVisitor fv = cw.visitField(ACC_PUBLIC, o.name, fieldType.getBytecodeType(), fieldType instanceof PrimativeType?null:fieldType.getGenericBytecodeType(), null); fv.visitEnd(); } cw.visitField(ACC_PUBLIC, "$regSet", Const_RegSetType.getBytecodeType(), null, null).visitEnd(); cw.visitField(ACC_PUBLIC, "refStateTracker$", Const_RefStateTracker.getBytecodeType(), null, null).visitEnd(); } BytecodeOutputter boCasterHelpder = new BytecodeOutputter(cw, false); {//"<init>" ArrayList<Sixple<Type, String, Annotations, Boolean, Boolean, Boolean>> splicedInAsTypeAndStr = spliceInArgs.getAsTypesAndNames(); ArrayList<Sixple<Type, String, Annotations, Boolean, Boolean, Boolean>> forMeth = Utils.filterOutNameFromtuples(splicedInAsTypeAndStr, stuffToNull); MethodVisitor mv = boCasterHelpder.enterMethod("<init>", forMeth, Const_PRIM_VOID, false, false, false, AccessModifier.PUBLIC, 0, fullname, null, false, null); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); //mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); int cnt = 1; for(Sixple<Type, String, Annotations, Boolean, Boolean, Boolean> tAndName : splicedInAsTypeAndStr){ mv.visitVarInsn(ALOAD, 0);//this Type tt = tAndName.getA(); if(stuffToNull.contains(tAndName.getB())){//just null ref it mv.visitInsn(Opcodes.ACONST_NULL); } else{ Utils.applyLoad(boCasterHelpder, tt, cnt++); if (tt instanceof PrimativeType) { PrimativeTypeEnum tta = ((PrimativeType) tt).type; if (tta == PrimativeTypeEnum.DOUBLE || tta == PrimativeTypeEnum.LONG) {// double, long cnt++; } } } mv.visitFieldInsn(PUTFIELD, fullname, tAndName.getB(), tt.getBytecodeType()); } { mv.visitVarInsn(ALOAD, 0); mv.visitTypeInsn(NEW, "com/concurnas/runtime/cps/IsoRegistrationSet"); mv.visitInsn(DUP); mv.visitInsn(isEvery?ICONST_1:ICONST_0); mv.visitInsn(isOnlyClosed?ICONST_1:ICONST_0); mv.visitMethodInsn(INVOKESPECIAL, "com/concurnas/runtime/cps/IsoRegistrationSet", "<init>", "(ZZ)V", false); mv.visitFieldInsn(PUTFIELD, fullname, "$regSet", "Lcom/concurnas/runtime/cps/IsoRegistrationSet;"); } { mv.visitVarInsn(ALOAD, 0); mv.visitTypeInsn(NEW, "com/concurnas/runtime/cps/RefStateTracker"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "com/concurnas/runtime/cps/RefStateTracker", "<init>", "()V", false); mv.visitFieldInsn(PUTFIELD, fullname, "refStateTracker$", "Lcom/concurnas/runtime/cps/RefStateTracker;"); } Label l2 = new Label(); mv.visitLabel(l2); mv.visitInsn(RETURN); mv.visitMaxs(2, 2); mv.visitEnd(); } cw.visitEnd(); this.bytecodeVisitor.localvarStackSize.pop(); this.bytecodeVisitor.localvarStack.pop(); this.bytecodeVisitor.addNameBtyecode(fileName, cw.toByteArray()); }
Example 16
Source File: LowerTest.java From turbine with Apache License 2.0 | 4 votes |
@Test public void invalidConstants() throws Exception { Path lib = temporaryFolder.newFile("lib.jar").toPath(); try (OutputStream os = Files.newOutputStream(lib); JarOutputStream jos = new JarOutputStream(os)) { jos.putNextEntry(new JarEntry("Lib.class")); ClassWriter cw = new ClassWriter(0); cw.visit(52, Opcodes.ACC_SUPER, "Lib", null, "java/lang/Object", null); cw.visitField(Opcodes.ACC_FINAL | Opcodes.ACC_STATIC, "ZCONST", "Z", null, Integer.MAX_VALUE); cw.visitField(Opcodes.ACC_FINAL | Opcodes.ACC_STATIC, "SCONST", "S", null, Integer.MAX_VALUE); jos.write(cw.toByteArray()); } ImmutableMap<String, String> input = ImmutableMap.of( "Test.java", Joiner.on('\n') .join( "class Test {", " static final short SCONST = Lib.SCONST + 0;", " static final boolean ZCONST = Lib.ZCONST || false;", "}")); Map<String, byte[]> actual = IntegrationTestSupport.runTurbine(input, ImmutableList.of(lib)); Map<String, Object> values = new LinkedHashMap<>(); new ClassReader(actual.get("Test")) .accept( new ClassVisitor(Opcodes.ASM7) { @Override public FieldVisitor visitField( int access, String name, String desc, String signature, Object value) { values.put(name, value); return super.visitField(access, name, desc, signature, value); } }, 0); assertThat(values).containsEntry("SCONST", -1); assertThat(values).containsEntry("ZCONST", 1); }
Example 17
Source File: ClassReaderTest.java From turbine with Apache License 2.0 | 4 votes |
@Test public void fields() { ClassWriter cw = new ClassWriter(0); cw.visit( 52, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, "test/Hello", "<X:Ljava/lang/Object;>Ljava/lang/Object;", "java/lang/Object", null); FieldVisitor fv = cw.visitField(Opcodes.ACC_PUBLIC, "x", "I", null, null); fv.visitAnnotation("Ljava/lang/Deprecated;", true); cw.visitField( Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, "y", "I", null, Integer.valueOf(42)); cw.visitField(Opcodes.ACC_PUBLIC, "z", "Ljava/util/List;", "Ljava/util/List<TX;>;", null); cw.visitEnd(); byte[] bytes = cw.toByteArray(); ClassFile classFile = com.google.turbine.bytecode.ClassReader.read(null, bytes); assertThat(classFile.fields()).hasSize(3); ClassFile.FieldInfo x = classFile.fields().get(0); assertThat(x.access()).isEqualTo(TurbineFlag.ACC_PUBLIC); assertThat(x.name()).isEqualTo("x"); assertThat(x.descriptor()).isEqualTo("I"); assertThat(x.signature()).isNull(); assertThat(x.value()).isNull(); assertThat(x.annotations()).hasSize(1); ClassFile.AnnotationInfo annotation = Iterables.getOnlyElement(x.annotations()); assertThat(annotation.typeName()).isEqualTo("Ljava/lang/Deprecated;"); ClassFile.FieldInfo y = classFile.fields().get(1); assertThat(y.access()) .isEqualTo(TurbineFlag.ACC_PUBLIC | TurbineFlag.ACC_STATIC | TurbineFlag.ACC_FINAL); assertThat(y.name()).isEqualTo("y"); assertThat(y.descriptor()).isEqualTo("I"); assertThat(y.value().constantTypeKind()).isEqualTo(TurbineConstantTypeKind.INT); assertThat(((Const.IntValue) y.value()).value()).isEqualTo(42); assertThat(y.annotations()).isEmpty(); ClassFile.FieldInfo z = classFile.fields().get(2); assertThat(z.name()).isEqualTo("z"); assertThat(z.descriptor()).isEqualTo("Ljava/util/List;"); assertThat(z.signature()).isEqualTo("Ljava/util/List<TX;>;"); assertThat(z.annotations()).isEmpty(); }
Example 18
Source File: MethodHandleFieldAssembler.java From spring-boot-netty with MIT License | 4 votes |
public static void assembleMethodHandleField(final ClassWriter cw, final Method targetMethod, final String invokerName) { final String desc = Type.getDescriptor(MethodHandle.class); cw.visitField(ACC_PRIVATE | ACC_FINAL + ACC_STATIC, "HANDLE", desc, null, null); cw.visitEnd(); final MethodVisitor ctor = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); final Label cs = createLabel(); final Label ce = createLabel(); ctor.visitCode(); ctor.visitLabel(cs); final String clName = targetMethod.getDeclaringClass().getCanonicalName(); final String targetMethodName = targetMethod.getName(); ctor.visitLdcInsn(clName); ctor.visitLdcInsn(targetMethodName); final Parameter[] parameters = targetMethod.getParameters(); ctor.visitLdcInsn(parameters.length); final Type classType = Type.getType(Class.class); final String descriptor = classType.getInternalName(); ctor.visitTypeInsn(ANEWARRAY, descriptor); for (int i = 0; i < parameters.length; i++) { ctor.visitInsn(DUP); ctor.visitLdcInsn(i); final Class<?> paramClass = parameters[i].getType(); if (paramClass.isPrimitive()) { final Class<?> wrapper = Primitives.wrap(paramClass); final String holderName = Type.getType(wrapper).getInternalName(); ctor.visitFieldInsn(GETSTATIC, holderName, "TYPE", CL_DESCRIPTOR); } else { final Type parameterType = Type.getType(paramClass); ctor.visitLdcInsn(parameterType); } ctor.visitInsn(AASTORE); } ctor.visitMethodInsn(INVOKESTATIC, MHC_INTERNAL_NAME, "createUniversal", '(' + STR_DESCRIPTOR + STR_DESCRIPTOR + CLA_DESCRIPTOR + ')' + MH_DESCRIPTOR, false); ctor.visitFieldInsn(PUTSTATIC, invokerName, "HANDLE", MH_DESCRIPTOR); ctor.visitInsn(RETURN); ctor.visitLabel(ce); ctor.visitEnd(); ctor.visitMaxs(0, 0); }
Example 19
Source File: ClassWithCredentialsDumper.java From openpojo with Apache License 2.0 | 4 votes |
static byte[] dump() { ClassWriter cw = new ClassWriter(0); FieldVisitor fv; MethodVisitor mv; AnnotationVisitor av0; cw.visit(52, ACC_FINAL + ACC_SUPER, CLASS_PACKAGE, null, "java/lang/Object", null); { fv = cw.visitField(ACC_PRIVATE, "credentials", "L" + SUN_SECURITY_KRB_5_CREDENTIALS + ";", null, null); fv.visitEnd(); } { mv = cw.visitMethod(0, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "getCredentials", "()L" + SUN_SECURITY_KRB_5_CREDENTIALS + ";", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, CLASS_PACKAGE, "credentials", "L" + SUN_SECURITY_KRB_5_CREDENTIALS + ";"); mv.visitInsn(ARETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "setCredentials", "(L" + SUN_SECURITY_KRB_5_CREDENTIALS + ";)V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(PUTFIELD, CLASS_PACKAGE, "credentials", "L" + SUN_SECURITY_KRB_5_CREDENTIALS + ";"); mv.visitInsn(RETURN); mv.visitMaxs(2, 2); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
Example 20
Source File: CallSiteGenerator.java From groovy with Apache License 2.0 | 4 votes |
public static byte[] genPogoMetaMethodSite(CachedMethod cachedMethod, ClassWriter cw, String name) { String internalName = name.replace('.', '/'); classHeader(cw, internalName, "org/codehaus/groovy/runtime/callsite/PogoMetaMethodSite"); cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "__constructor__", "Ljava/lang/reflect/Constructor;", null, null); genConstructor(cw, "org/codehaus/groovy/runtime/callsite/PogoMetaMethodSite", internalName); genCallXxxWithArray(cw, "Current", "org/codehaus/groovy/runtime/callsite/PogoMetaMethodSite", cachedMethod, "groovy/lang/GroovyObject"); genCallXxxWithArray(cw, "", "org/codehaus/groovy/runtime/callsite/PogoMetaMethodSite", cachedMethod, "java/lang/Object"); genCallWithFixedParams(cw, "Current", "org/codehaus/groovy/runtime/callsite/PogoMetaMethodSite", cachedMethod, "groovy/lang/GroovyObject"); genCallWithFixedParams(cw, "", "org/codehaus/groovy/runtime/callsite/PogoMetaMethodSite", cachedMethod, "java/lang/Object"); cw.visitEnd(); return cw.toByteArray(); }