use of org.objectweb.asm.FieldVisitor in project baseio by generallycloud.
the class HelloGeneratorClass method generatorHelloClass.
/**
* 使用构造Hello类class字节码<br/>
* package中的Hello.java只是代码原型,本例中只供对比用,没有实际使用到这个类。<br/>
* ASM代码生成工具使用 bytecode
*
* @return
* @throws Exception
* @author SHANHY
* @create 2016年2月3日
*/
public static byte[] generatorHelloClass() throws Exception {
ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "com/generallycloud/test/others/asm/Hello", null, "java/lang/Object", null);
cw.visitSource("Hello.java", null);
{
fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "FLAG", "Ljava/lang/String;", null, "\u6211\u662f\u5e38\u91cf");
fv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "display", "()V", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitInsn(ICONST_0);
mv.visitVarInsn(ISTORE, 1);
Label l1 = new Label();
mv.visitLabel(l1);
Label l2 = new Label();
mv.visitJumpInsn(GOTO, l2);
Label l3 = new Label();
mv.visitLabel(l3);
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { Opcodes.INTEGER }, 0, null);
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn(">>>>>>>>>>\u6211\u662f\u5e38\u91cf");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
Label l4 = new Label();
mv.visitLabel(l4);
mv.visitIincInsn(1, 1);
mv.visitLabel(l2);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitVarInsn(ILOAD, 1);
mv.visitIntInsn(BIPUSH, 8);
mv.visitJumpInsn(IF_ICMPLT, l3);
Label l5 = new Label();
mv.visitLabel(l5);
mv.visitInsn(RETURN);
Label l6 = new Label();
mv.visitLabel(l6);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "testList", "()Ljava/util/List;", "()Ljava/util/List<Ljava/lang/String;>;", null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitTypeInsn(NEW, "java/util/ArrayList");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
mv.visitVarInsn(ASTORE, 1);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitVarInsn(ALOAD, 1);
mv.visitLdcInsn("Tome");
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
mv.visitInsn(POP);
Label l2 = new Label();
mv.visitLabel(l2);
mv.visitVarInsn(ALOAD, 1);
mv.visitLdcInsn("Jack");
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
mv.visitInsn(POP);
Label l3 = new Label();
mv.visitLabel(l3);
mv.visitVarInsn(ALOAD, 1);
mv.visitLdcInsn("Lily");
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
mv.visitInsn(POP);
Label l4 = new Label();
mv.visitLabel(l4);
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
mv.visitInsn(DUP);
mv.visitLdcInsn(">>>>>>>>>>testList > list.size = ");
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "size", "()I", true);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
Label l5 = new Label();
mv.visitLabel(l5);
mv.visitVarInsn(ALOAD, 1);
mv.visitInsn(ARETURN);
Label l6 = new Label();
mv.visitLabel(l6);
mv.visitMaxs(4, 2);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC + ACC_VARARGS, "testMapList", "(Z[Ljava/util/Map;)Ljava/util/List;", "(Z[Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;)Ljava/util/List<Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;>;", null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitTypeInsn(NEW, "java/util/ArrayList");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
mv.visitVarInsn(ASTORE, 3);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitVarInsn(ILOAD, 1);
Label l2 = new Label();
mv.visitJumpInsn(IFEQ, l2);
Label l3 = new Label();
mv.visitLabel(l3);
mv.visitVarInsn(ALOAD, 2);
mv.visitInsn(DUP);
mv.visitVarInsn(ASTORE, 7);
mv.visitInsn(ARRAYLENGTH);
mv.visitVarInsn(ISTORE, 6);
mv.visitInsn(ICONST_0);
mv.visitVarInsn(ISTORE, 5);
Label l4 = new Label();
mv.visitJumpInsn(GOTO, l4);
Label l5 = new Label();
mv.visitLabel(l5);
mv.visitFrame(Opcodes.F_FULL, 8, new Object[] { "com/generallycloud/test/others/asm/Hello", Opcodes.INTEGER, "[Ljava/util/Map;", "java/util/List", Opcodes.TOP, Opcodes.INTEGER, Opcodes.INTEGER, "[Ljava/util/Map;" }, 0, new Object[] {});
mv.visitVarInsn(ALOAD, 7);
mv.visitVarInsn(ILOAD, 5);
mv.visitInsn(AALOAD);
mv.visitVarInsn(ASTORE, 4);
Label l6 = new Label();
mv.visitLabel(l6);
mv.visitVarInsn(ALOAD, 3);
mv.visitVarInsn(ALOAD, 4);
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
mv.visitInsn(POP);
Label l7 = new Label();
mv.visitLabel(l7);
mv.visitIincInsn(5, 1);
mv.visitLabel(l4);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitVarInsn(ILOAD, 5);
mv.visitVarInsn(ILOAD, 6);
mv.visitJumpInsn(IF_ICMPLT, l5);
mv.visitLabel(l2);
mv.visitFrame(Opcodes.F_FULL, 4, new Object[] { "com/generallycloud/test/others/asm/Hello", Opcodes.INTEGER, "[Ljava/util/Map;", "java/util/List" }, 0, new Object[] {});
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
mv.visitInsn(DUP);
mv.visitLdcInsn(">>>>>>>>>>testMapList > list.size = ");
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false);
mv.visitVarInsn(ALOAD, 3);
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "size", "()I", true);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
Label l8 = new Label();
mv.visitLabel(l8);
mv.visitVarInsn(ALOAD, 3);
mv.visitInsn(ARETURN);
Label l9 = new Label();
mv.visitLabel(l9);
mv.visitMaxs(4, 8);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
use of org.objectweb.asm.FieldVisitor in project felix by apache.
the class ClassManipulator method addFlagsForInnerClassMethods.
/**
* Add the boolean flag fields for methods from inner classes.
*/
private void addFlagsForInnerClassMethods() {
for (Map.Entry<String, List<MethodDescriptor>> entry : m_manipulator.getInnerClassesAndMethods().entrySet()) {
for (MethodDescriptor descriptor : entry.getValue()) {
String id = generateMethodFlagForMethodFromInnerClass(descriptor.getName(), descriptor.getDescriptor(), entry.getKey());
if (!m_methodFlags.contains(id)) {
FieldVisitor flagField = cv.visitField(0, id, "Z", null, null);
flagField.visitEnd();
m_methodFlags.add(id);
}
}
}
}
use of org.objectweb.asm.FieldVisitor in project felix by apache.
the class ClassManipulator method visitMethod.
/**
* A method is visited.
* This method does not manipulate clinit and class$ methods.
* In the case of a constructor, this method will generate a constructor with the instance manager
* and will adapt the current constructor to call this constructor.
* For standard method, this method will create method header, rename the current method and adapt it.
* @param access : access flag.
* @param name : name of the method
* @param desc : method descriptor
* @param signature : signature
* @param exceptions : declared exceptions.
* @return the MethodVisitor wich will visit the method code.
* @see org.objectweb.asm.ClassVisitor#visitMethod(int, java.lang.String, java.lang.String, java.lang.String,
* java.lang.String[])
*/
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
// Avoid manipulating special methods
if (name.equals("<clinit>") || name.equals("class$")) {
return super.visitMethod(access, name, desc, signature, exceptions);
}
// The constructor is manipulated separately
if (name.equals("<init>")) {
MethodDescriptor md = getMethodDescriptor("$init", desc);
// 1) change the constructor descriptor (add a component manager arg as first argument)
String newDesc = desc.substring(1);
newDesc = "(Lorg/apache/felix/ipojo/InstanceManager;" + newDesc;
Type[] args = Type.getArgumentTypes(desc);
// TODO HERE ! => All constructor matches, no distinction between the different constructors.
generateConstructor(access, desc, signature, exceptions, md.getAnnotations(), md.getParameterAnnotations(), md.getLocals());
if (args.length == 0) {
m_foundSuitableConstructor = true;
} else if (args.length == 1 && args[0].getClassName().equals("org.osgi.framework.BundleContext")) {
m_foundSuitableConstructor = true;
}
// Insert the new constructor
MethodVisitor mv = super.visitMethod(ACC_PRIVATE, "<init>", newDesc, signature, exceptions);
return new ConstructorCodeAdapter(mv, m_owner, m_fields, ACC_PRIVATE, name, newDesc, m_superclass);
}
if ((access & ACC_SYNTHETIC) == ACC_SYNTHETIC && name.startsWith("access$")) {
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
return new MethodCodeAdapter(mv, m_owner, access, name, desc, m_fields);
}
// Do nothing on static methods
if ((access & ACC_STATIC) == ACC_STATIC) {
return super.visitMethod(access, name, desc, signature, exceptions);
}
// Do nothing on native methods
if ((access & ACC_NATIVE) == ACC_NATIVE) {
return super.visitMethod(access, name, desc, signature, exceptions);
}
MethodDescriptor md = getMethodDescriptor(name, desc);
if (md == null) {
generateMethodHeader(access, name, desc, signature, exceptions, null, null, null);
} else {
generateMethodHeader(access, name, desc, signature, exceptions, md.getArgumentLocalVariables(), md.getAnnotations(), md.getParameterAnnotations());
}
// TODO Also add the method flags for inner class methods.
String id = generateMethodFlag(name, desc);
if (!m_methodFlags.contains(id)) {
FieldVisitor flagField = cv.visitField(0, id, "Z", null, null);
flagField.visitEnd();
m_methodFlags.add(id);
}
MethodVisitor mv = super.visitMethod(ACC_PRIVATE, PREFIX + name, desc, signature, exceptions);
return new MethodCodeAdapter(mv, m_owner, ACC_PRIVATE, PREFIX + name, desc, m_fields);
}
use of org.objectweb.asm.FieldVisitor in project Lucee by lucee.
the class ASMUtil method createPojo.
public static byte[] createPojo(String className, ASMProperty[] properties, Class parent, Class[] interfaces, String srcName) throws PageException {
className = className.replace('.', '/');
className = className.replace('\\', '/');
className = ListUtil.trim(className, "/");
String[] inter = null;
if (interfaces != null) {
inter = new String[interfaces.length];
for (int i = 0; i < inter.length; i++) {
inter[i] = interfaces[i].getName().replace('.', '/');
}
}
// CREATE CLASS
ClassWriter cw = ASMUtil.getClassWriter();
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, className, null, parent.getName().replace('.', '/'), inter);
String md5;
try {
md5 = createMD5(properties);
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
md5 = "";
}
FieldVisitor fv = cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, "_md5_", "Ljava/lang/String;", null, md5);
fv.visitEnd();
// Constructor
GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR_OBJECT, null, null, cw);
adapter.loadThis();
adapter.invokeConstructor(toType(parent, true), CONSTRUCTOR_OBJECT);
adapter.returnValue();
adapter.endMethod();
// properties
for (int i = 0; i < properties.length; i++) {
createProperty(cw, className, properties[i]);
}
// complexType src
if (!StringUtil.isEmpty(srcName)) {
GeneratorAdapter _adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, _SRC_NAME, null, null, cw);
_adapter.push(srcName);
_adapter.returnValue();
_adapter.endMethod();
}
cw.visitEnd();
return cw.toByteArray();
}
use of org.objectweb.asm.FieldVisitor in project Lucee by lucee.
the class JavaProxyFactory method createProxy.
/*
public static Object to(Object obj, Class clazz) {
return obj;
}*/
/*public static Object createProxy(Config config,Component cfc, String className) throws PageException, IOException {
return createProxy(cfc, null, ClassUtil.loadClass(config.getClassLoader(), className));
}*/
public static Object createProxy(PageContext pc, Component cfc, Class extendz, Class... interfaces) throws PageException, IOException {
PageContextImpl pci = (PageContextImpl) pc;
// ((ConfigImpl)pci.getConfig()).getClassLoaderEnv()
ClassLoader[] parents = extractClassLoaders(null, interfaces);
if (extendz == null)
extendz = Object.class;
if (interfaces == null)
interfaces = new Class[0];
else {
for (int i = 0; i < interfaces.length; i++) {
if (!interfaces[i].isInterface())
throw new IOException("definition [" + interfaces[i].getName() + "] is a class and not a interface");
}
}
Type typeExtends = Type.getType(extendz);
Type[] typeInterfaces = ASMUtil.toTypes(interfaces);
String[] strInterfaces = new String[typeInterfaces.length];
for (int i = 0; i < strInterfaces.length; i++) {
strInterfaces[i] = typeInterfaces[i].getInternalName();
}
String className = createClassName(extendz, interfaces);
// Mapping mapping = cfc.getPageSource().getMapping();
// get ClassLoader
PhysicalClassLoader pcl = null;
try {
// mapping.getConfig().getRPCClassLoader(false)
pcl = (PhysicalClassLoader) pci.getRPCClassLoader(false, parents);
} catch (IOException e) {
throw Caster.toPageException(e);
}
Resource classFile = pcl.getDirectory().getRealResource(className.concat(".class"));
// check if already exists, if yes return
if (classFile.exists()) {
try {
Object obj = newInstance(pcl, className, pc.getConfig(), cfc);
if (obj != null)
return obj;
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
}
}
/*
String classNameOriginal=component.getPageSource().getFullClassName();
String realOriginal=classNameOriginal.replace('.','/');
Resource classFileOriginal = mapping.getClassRootDirectory().getRealResource(realOriginal.concat(".class"));
*/
ClassWriter cw = ASMUtil.getClassWriter();
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, className, null, typeExtends.getInternalName(), strInterfaces);
// BytecodeContext statConstr = null;//new BytecodeContext(null,null,null,cw,real,ga,Page.STATIC_CONSTRUCTOR);
// BytecodeContext constr = null;//new BytecodeContext(null,null,null,cw,real,ga,Page.CONSTRUCTOR);
// field Component
FieldVisitor _fv = cw.visitField(Opcodes.ACC_PRIVATE, "cfc", COMPONENT_NAME, null, null);
_fv.visitEnd();
_fv = cw.visitField(Opcodes.ACC_PRIVATE, "config", CONFIG_WEB_NAME, null, null);
_fv.visitEnd();
// Constructor
GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR, null, null, cw);
Label begin = new Label();
adapter.visitLabel(begin);
adapter.loadThis();
adapter.invokeConstructor(Types.OBJECT, SUPER_CONSTRUCTOR);
// adapter.putField(JAVA_PROXY, arg1, arg2)
adapter.visitVarInsn(Opcodes.ALOAD, 0);
adapter.visitVarInsn(Opcodes.ALOAD, 1);
adapter.visitFieldInsn(Opcodes.PUTFIELD, className, "config", CONFIG_WEB_NAME);
adapter.visitVarInsn(Opcodes.ALOAD, 0);
adapter.visitVarInsn(Opcodes.ALOAD, 2);
adapter.visitFieldInsn(Opcodes.PUTFIELD, className, "cfc", COMPONENT_NAME);
adapter.visitInsn(Opcodes.RETURN);
Label end = new Label();
adapter.visitLabel(end);
adapter.visitLocalVariable("config", CONFIG_WEB_NAME, null, begin, end, 1);
adapter.visitLocalVariable("cfc", COMPONENT_NAME, null, begin, end, 2);
// adapter.returnValue();
adapter.endMethod();
// create methods
Set<Class> cDone = new HashSet<Class>();
Map<String, Class> mDone = new HashMap<String, Class>();
for (int i = 0; i < interfaces.length; i++) {
_createProxy(cw, cDone, mDone, cfc, interfaces[i], className);
}
cw.visitEnd();
// create class file
byte[] barr = cw.toByteArray();
try {
ResourceUtil.touch(classFile);
IOUtil.copy(new ByteArrayInputStream(barr), classFile, true);
pcl = (PhysicalClassLoader) pci.getRPCClassLoader(true, parents);
Class<?> clazz = pcl.loadClass(className, barr);
return newInstance(clazz, pc.getConfig(), cfc);
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
throw Caster.toPageException(t);
}
}
Aggregations