use of org.objectweb.asm.FieldVisitor in project atlas by alibaba.
the class AsmTest method test.
@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();
}
use of org.objectweb.asm.FieldVisitor in project groovy by apache.
the class AsmClassGenerator method visitField.
@Override
public void visitField(final FieldNode fieldNode) {
onLineNumber(fieldNode, "visitField: " + fieldNode.getName());
ClassNode t = fieldNode.getType();
String signature = BytecodeHelper.getGenericsBounds(t);
Expression initialValueExpression = fieldNode.getInitialValueExpression();
ConstantExpression cexp = initialValueExpression instanceof ConstantExpression ? (ConstantExpression) initialValueExpression : null;
if (cexp != null) {
cexp = Verifier.transformToPrimitiveConstantIfPossible(cexp);
}
Object value = cexp != null && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(t) && fieldNode.isStatic() && fieldNode.isFinal() ? cexp.getValue() : // GROOVY-5150
null;
if (value != null) {
// byte, char and short require an extra cast
if (isPrimitiveByte(t) || isPrimitiveShort(t)) {
value = ((Number) value).intValue();
} else if (isPrimitiveChar(t)) {
value = Integer.valueOf((Character) value);
}
}
FieldVisitor fv = classVisitor.visitField(fieldNode.getModifiers(), fieldNode.getName(), BytecodeHelper.getTypeDescription(t), signature, value);
visitAnnotations(fieldNode, fv);
visitType(fieldNode.getType(), fv, newTypeReference(FIELD), "", true);
fv.visitEnd();
}
use of org.objectweb.asm.FieldVisitor in project aries by apache.
the class AbstractWovenProxyAdapter method generateField.
/**
* Generate an instance field that should be "invisible" to normal code
*
* @param fieldName
* @param fieldDescriptor
*/
private final void generateField(String fieldName, String fieldDescriptor) {
FieldVisitor fv = cv.visitField(ACC_PROTECTED | ACC_TRANSIENT | ACC_SYNTHETIC | ACC_FINAL, fieldName, fieldDescriptor, null, null);
for (String s : annotationTypeDescriptors) fv.visitAnnotation(s, true).visitEnd();
fv.visitEnd();
}
use of org.objectweb.asm.FieldVisitor in project spring-loaded by spring-projects.
the class SystemClassReflectionGenerator method generateJLCMethod.
public static void generateJLCMethod(ClassWriter cw, String classname, String membername, String methodname) {
FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, membername, "Ljava/lang/reflect/Method;", null, null);
fv.visitEnd();
MethodVisitor mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC + ACC_VARARGS, membername, "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", null, new String[] { "java/lang/NoSuchMethodException" });
mv.visitCode();
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
mv.visitTryCatchBlock(l0, l1, l2, "java/lang/reflect/InvocationTargetException");
Label l3 = new Label();
mv.visitTryCatchBlock(l0, l1, l3, "java/lang/Exception");
Label l4 = new Label();
mv.visitLabel(l4);
mv.visitFieldInsn(GETSTATIC, classname, membername, "Ljava/lang/reflect/Method;");
mv.visitJumpInsn(IFNONNULL, l0);
Label l5 = new Label();
mv.visitLabel(l5);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 2);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", methodname, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false);
mv.visitInsn(ARETURN);
mv.visitLabel(l0);
mv.visitFieldInsn(GETSTATIC, classname, membername, "Ljava/lang/reflect/Method;");
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ICONST_3);
mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
mv.visitInsn(DUP);
mv.visitInsn(ICONST_0);
mv.visitVarInsn(ALOAD, 0);
mv.visitInsn(AASTORE);
mv.visitInsn(DUP);
mv.visitInsn(ICONST_1);
mv.visitVarInsn(ALOAD, 1);
mv.visitInsn(AASTORE);
mv.visitInsn(DUP);
mv.visitInsn(ICONST_2);
mv.visitVarInsn(ALOAD, 2);
mv.visitInsn(AASTORE);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false);
mv.visitTypeInsn(CHECKCAST, "java/lang/reflect/Method");
mv.visitLabel(l1);
mv.visitInsn(ARETURN);
mv.visitLabel(l2);
mv.visitVarInsn(ASTORE, 3);
Label l6 = new Label();
mv.visitLabel(l6);
// Don't print the exception if just unwrapping it
// mv.visitVarInsn(ALOAD, 3);
// mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "printStackTrace", "()V");
Label l7 = new Label();
mv.visitLabel(l7);
mv.visitVarInsn(ALOAD, 3);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
mv.visitTypeInsn(INSTANCEOF, "java/lang/NoSuchMethodException");
Label l8 = new Label();
mv.visitJumpInsn(IFEQ, l8);
Label l9 = new Label();
mv.visitLabel(l9);
mv.visitVarInsn(ALOAD, 3);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
mv.visitTypeInsn(CHECKCAST, "java/lang/NoSuchMethodException");
mv.visitInsn(ATHROW);
mv.visitLabel(l3);
mv.visitVarInsn(ASTORE, 3);
Label l10 = new Label();
mv.visitLabel(l10);
mv.visitVarInsn(ALOAD, 3);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace", "()V", false);
mv.visitLabel(l8);
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
Label l11 = new Label();
mv.visitLabel(l11);
// mv.visitLocalVariable("clazz", "Ljava/lang/Class;", "Ljava/lang/Class<*>;", l4, l11, 0);
// mv.visitLocalVariable("methodname", "Ljava/lang/String;", null, l4, l11, 1);
// mv.visitLocalVariable("parameterTypes", "[Ljava/lang/Class;", null, l4, l11, 2);
// mv.visitLocalVariable("ite", "Ljava/lang/reflect/InvocationTargetException;", null, l6, l3, 3);
// mv.visitLocalVariable("e", "Ljava/lang/Exception;", null, l10, l8, 3);
mv.visitMaxs(6, 4);
mv.visitEnd();
}
use of org.objectweb.asm.FieldVisitor in project spring-loaded by spring-projects.
the class SystemClassReflectionGenerator method generateJLRF_Get.
public static void generateJLRF_Get(ClassWriter cw, String classname) {
FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, jlrfGetMember, "Ljava/lang/reflect/Method;", null, null);
fv.visitEnd();
MethodVisitor mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, jlrfGetMember, jlrfGetDescriptor, null, new String[] { "java/lang/IllegalAccessException", "java/lang/IllegalArgumentException" });
mv.visitCode();
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
Label l3 = new Label();
mv.visitLabel(l3);
mv.visitFieldInsn(GETSTATIC, classname, jlrfGetMember, "Ljava/lang/reflect/Method;");
mv.visitJumpInsn(IFNONNULL, l0);
Label l4 = new Label();
mv.visitLabel(l4);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Field", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", false);
mv.visitInsn(ARETURN);
mv.visitLabel(l0);
mv.visitFieldInsn(GETSTATIC, classname, jlrfGetMember, "Ljava/lang/reflect/Method;");
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ICONST_2);
mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
mv.visitInsn(DUP);
mv.visitInsn(ICONST_0);
// target field
mv.visitVarInsn(ALOAD, 0);
mv.visitInsn(AASTORE);
mv.visitInsn(DUP);
mv.visitInsn(ICONST_1);
// instance on which to get the field
mv.visitVarInsn(ALOAD, 1);
mv.visitInsn(AASTORE);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false);
mv.visitLabel(l1);
mv.visitInsn(ARETURN);
mv.visitLabel(l2);
mv.visitVarInsn(ASTORE, 2);
Label l5 = new Label();
mv.visitLabel(l5);
Label l6 = new Label();
mv.visitLabel(l6);
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
Label l7 = new Label();
mv.visitLabel(l7);
mv.visitMaxs(8, 4);
mv.visitEnd();
}
Aggregations