当前位置: 首页>>代码示例>>Java>>正文


Java ClassWriter.visitField方法代码示例

本文整理汇总了Java中org.objectweb.asm.ClassWriter.visitField方法的典型用法代码示例。如果您正苦于以下问题:Java ClassWriter.visitField方法的具体用法?Java ClassWriter.visitField怎么用?Java ClassWriter.visitField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.objectweb.asm.ClassWriter的用法示例。


在下文中一共展示了ClassWriter.visitField方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: proceed

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@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);
    }
}
 
开发者ID:yrom,项目名称:shrinker,代码行数:26,代码来源:WriteStyleablesProcessor.java

示例2: generate

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public byte[] generate(){
    String className = "eu.mikroskeem.test.shuriken.common.classtools.gen.GeneratedClass";
    String classNameInternal = className.replaceAll("\\.", "/");

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    cw.visit(
            V1_8,
            ACC_PUBLIC + ACC_SUPER,
            classNameInternal,
            null,
            Type.getInternalName(Object.class),
            null);
    FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "test", Type.getDescriptor(String.class), null, "hey");
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    return cw.toByteArray();
}
 
开发者ID:mikroskeem,项目名称:Shuriken,代码行数:24,代码来源:ValidatorTester.java

示例3: generateConfigClass

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * 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();
}
 
开发者ID:rakutentech,项目名称:android-perftracking,代码行数:17,代码来源:ClassGenerator.java

示例4: testClassLoading

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Test
public void testClassLoading() throws Exception {
    SimpleClassLoader simpleClassLoader = new SimpleClassLoader(ClassLoader.getSystemClassLoader());

    String className = getClass().getPackage().getName()+".GeneratedTestClass";
    String classNameInternal = className.replaceAll("\\.", "/");

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, classNameInternal, null, Type.getInternalName(Object.class), null);
    {
        FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "test", Type.getDescriptor(String.class), null, "hey");
        fv.visitEnd();
    }
    {
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
        cw.visitEnd();
    }
    cw.visitEnd();

    /* Test class loading */
    Class<?> generated = ClassLoaderTools.defineClass(simpleClassLoader, className, cw.toByteArray());
    Assertions.assertNotNull(generated, "Class didn't load!");
    Assertions.assertTrue(Reflect.getClass(className, simpleClassLoader).isPresent(),
            "Class isn't present in classloader!");
    Assertions.assertFalse(Reflect.getClass(className).isPresent()
            , "Class is present in system class loader!");

    /* Test field */
    ClassWrapper<?> c = Reflect.construct(Reflect.wrapClass(generated));
    Optional<FieldWrapper<String>> testFieldOptional = c.getField("test", String.class);
    Assertions.assertTrue(testFieldOptional.isPresent(), "Test field is not present!");
    Assertions.assertEquals("hey", testFieldOptional.get().read(), "Test field content didn't match!");
}
 
开发者ID:mikroskeem,项目名称:Shuriken,代码行数:39,代码来源:ClassLoadingTester.java

示例5: generateFields

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private static void generateFields(Method method, ClassWriter cw, Parameter[] parameters) {
    FieldVisitor fv;
    cw.visitSource(method.getName() + "[email protected]@aspecio", null);

    fv = cw.visitField(ACC_PUBLIC + ACC_FINAL, "parameters", "Ljava/util/List;", "Ljava/util/List<Ljava/lang/reflect/Parameter;>;",
            null);
    fv.visitEnd();

    for (Parameter p : parameters) {
        fv = cw.visitField(ACC_PUBLIC, p.getName(), Type.getDescriptor(p.getType()), null, null);
        fv.visitEnd();
    }
}
 
开发者ID:primeval-io,项目名称:primeval-reflex,代码行数:14,代码来源:MethodArgumentssUpdaterGenerator.java

示例6: generateFields

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private static void generateFields(Method method, ClassWriter cw, Parameter[] parameters) {
    FieldVisitor fv;
    cw.visitSource(method.getName() + "[email protected]", null);

    fv = cw.visitField(ACC_PUBLIC + ACC_FINAL, "parameters", "Ljava/util/List;", "Ljava/util/List<Ljava/lang/reflect/Parameter;>;",
            null);
    fv.visitEnd();

    for (Parameter p : parameters) {
        fv = cw.visitField(ACC_PUBLIC + ACC_FINAL, p.getName(), Type.getDescriptor(p.getType()), null, null);
        fv.visitEnd();
    }
}
 
开发者ID:primeval-io,项目名称:primeval-reflex,代码行数:14,代码来源:MethodArgumentsGenerator.java

示例7: test

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@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();

}
 
开发者ID:alibaba,项目名称:atlas,代码行数:73,代码来源:AsmTest.java


注:本文中的org.objectweb.asm.ClassWriter.visitField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。