本文整理汇总了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);
}
}
示例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();
}
示例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();
}
示例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!");
}
示例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();
}
}
示例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();
}
}
示例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();
}