List of usage examples for org.objectweb.asm Opcodes ACC_PUBLIC
int ACC_PUBLIC
To view the source code for org.objectweb.asm Opcodes ACC_PUBLIC.
Click Source Link
From source file:com.nginious.http.serialize.QueryDeserializerCreator.java
License:Apache License
/** * Creates bytecode which implements the {@link QueryDeserializer#deserialize(se.netdigital.http.HttpRequest)} method for * the deserializer class being created. * /*from www . j a v a2s . co m*/ * @param writer class byte code writer * @param intBeanClazzName name of deserializer class being generated * @return a method visitor for writing bytecode inside the generated method */ private MethodVisitor createDeserializeMethod(ClassWriter writer, String intBeanClazzName) { String[] exceptions = { "com/nginious/serialize/SerializerException" }; MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "deserialize", "(Lcom/nginious/http/HttpRequest;)Ljava/lang/Object;", null, exceptions); visitor.visitCode(); visitor.visitTypeInsn(Opcodes.NEW, intBeanClazzName); visitor.visitInsn(Opcodes.DUP); visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, intBeanClazzName, "<init>", "()V"); visitor.visitVarInsn(Opcodes.ASTORE, 3); return visitor; }
From source file:com.nginious.http.serialize.Serialization.java
License:Apache License
/** * Creates bytecode for a no argument constructor. The created bytecode also calls the no argument constructor * in the superclass with the specified name. * /*from w ww . ja v a2 s. c o m*/ * @param writer bytecode writer * @param superclassName binary name of superclass */ static void createConstructor(ClassWriter writer, String superclassName) { MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); visitor.visitCode(); visitor.visitVarInsn(Opcodes.ALOAD, 0); visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassName, "<init>", "()V"); visitor.visitInsn(Opcodes.RETURN); visitor.visitMaxs(1, 1); visitor.visitEnd(); }
From source file:com.nginious.http.serialize.XmlDeserializerCreator.java
License:Apache License
/** * Creates a XML deserializer for the specified bean class unless a deserializer has already * been created. Created deserializers are cached and returned on subsequent calls to this method. * //from ww w .j ava2 s . c om * @param <T> class type for bean * @param beanClazz bean class for which a deserializer should be created * @return the created deserializer * @throws SerializerFactoryException if unable to create deserializer or class is not a bean */ @SuppressWarnings("unchecked") protected <T> XmlDeserializer<T> create(Class<T> beanClazz) throws SerializerFactoryException { XmlDeserializer<T> deserializer = (XmlDeserializer<T>) deserializers.get(beanClazz); if (deserializer != null) { return deserializer; } try { synchronized (this) { deserializer = (XmlDeserializer<T>) deserializers.get(beanClazz); if (deserializer != null) { return deserializer; } checkDeserializability(beanClazz, "xml"); String intBeanClazzName = Serialization.createInternalClassName(beanClazz); Method[] methods = beanClazz.getMethods(); String intDeserializerClazzName = new StringBuffer(intBeanClazzName).append("XmlDeserializer") .toString(); // Create class ClassWriter writer = new ClassWriter(0); String signature = Serialization.createClassSignature("com/nginious/http/serialize/XmlDeserializer", intBeanClazzName); writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, intDeserializerClazzName, signature, "com/nginious/http/serialize/XmlDeserializer", null); // Create constructor Serialization.createConstructor(writer, "com/nginious/http/serialize/XmlDeserializer"); // Create deserialize method MethodVisitor visitor = createDeserializeMethod(writer, intBeanClazzName); for (Method method : methods) { Serializable info = method.getAnnotation(Serializable.class); boolean canDeserialize = info == null || (info != null && info.deserialize() && info.types().indexOf("xml") > -1); if (canDeserialize && method.getName().startsWith("set") && method.getReturnType().equals(void.class) && method.getParameterTypes().length == 1) { Class<?>[] parameterTypes = method.getParameterTypes(); Class<?> parameterType = parameterTypes[0]; if (parameterType.isArray()) { Class<?> arrayType = parameterType.getComponentType(); if (arrayType.equals(boolean.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeBooleanArray", "[Z", "[Z", intBeanClazzName, method.getName()); } else if (arrayType.equals(double.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeDoubleArray", "[D", "[D", intBeanClazzName, method.getName()); } else if (arrayType.equals(float.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeFloatArray", "[F", "[F", intBeanClazzName, method.getName()); } else if (arrayType.equals(int.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeIntArray", "[I", "[I", intBeanClazzName, method.getName()); } else if (arrayType.equals(long.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeLongArray", "[J", "[J", intBeanClazzName, method.getName()); } else if (arrayType.equals(short.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeShortArray", "[S", "[S", intBeanClazzName, method.getName()); } else if (arrayType.equals(String.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeStringArray", "[Ljava/lang/String;", "[Ljava/lang/String;", intBeanClazzName, method.getName()); } } else if (parameterType.isPrimitive()) { if (parameterType.equals(boolean.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeBoolean", "Z", "Z", intBeanClazzName, method.getName()); } else if (parameterType.equals(double.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeDouble", "D", "D", intBeanClazzName, method.getName()); } else if (parameterType.equals(float.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeFloat", "F", "F", intBeanClazzName, method.getName()); } else if (parameterType.equals(int.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeInt", "I", "I", intBeanClazzName, method.getName()); } else if (parameterType.equals(long.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeLong", "J", "J", intBeanClazzName, method.getName()); } else if (parameterType.equals(short.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeShort", "S", "S", intBeanClazzName, method.getName()); } } else if (parameterType.equals(Calendar.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeCalendar", "Ljava/util/Calendar;", "Ljava/util/Calendar;", intBeanClazzName, method.getName()); } else if (parameterType.equals(Date.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeDate", "Ljava/util/Date;", "Ljava/util/Date;", intBeanClazzName, method.getName()); } else if (parameterType.equals(String.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeString", "Ljava/lang/String;", "Ljava/lang/String;", intBeanClazzName, method.getName()); } } } visitor.visitVarInsn(Opcodes.ALOAD, 3); visitor.visitInsn(Opcodes.ARETURN); visitor.visitMaxs(5, 4); visitor.visitEnd(); writer.visitEnd(); byte[] clazzBytes = writer.toByteArray(); ClassLoader controllerLoader = null; if (classLoader.hasLoaded(beanClazz)) { controllerLoader = beanClazz.getClassLoader(); } else { controllerLoader = this.classLoader; } Class<?> clazz = Serialization.loadClass(controllerLoader, intDeserializerClazzName.replace('/', '.'), clazzBytes); deserializer = (XmlDeserializer<T>) clazz.newInstance(); deserializers.put(beanClazz, deserializer); return deserializer; } } catch (IllegalAccessException e) { throw new SerializerFactoryException(e); } catch (InstantiationException e) { throw new SerializerFactoryException(e); } }
From source file:com.nginious.http.serialize.XmlDeserializerCreator.java
License:Apache License
/** * Creates bytecode which implements the {@link XmlDeserializer#deserialize(java.util.HashMap)} method for * the deserializer class being created. * // w ww . j a v a 2s . c o m * @param writer class byte code writer * @param intBeanClazzName name of deserializer class being generated * @return a method visitor for writing bytecode inside the generated method */ private MethodVisitor createDeserializeMethod(ClassWriter writer, String intBeanClazzName) { String[] exceptions = { "com/nginious/http/serialize/SerializerException" }; MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "deserialize", "(Ljava/util/HashMap;)Ljava/lang/Object;", null, exceptions); visitor.visitCode(); visitor.visitTypeInsn(Opcodes.NEW, intBeanClazzName); visitor.visitInsn(Opcodes.DUP); visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, intBeanClazzName, "<init>", "()V"); visitor.visitVarInsn(Opcodes.ASTORE, 3); return visitor; }
From source file:com.nginious.http.serialize.XmlSerializerCreator.java
License:Apache License
/** * Creates a XML serializer for the specified bean class unless a serializer has already been * created. Created serializers are cached and returned on subsequent calls to this method. * //from w ww . j av a 2 s . c o m * @param factory serializer factory * @param <T> class type for bean * @param beanClazz bean class for which a serializer should be created * @return the created serializer * @throws SerializerFactoryException if unable to create serializer or class is not a bean */ @SuppressWarnings("unchecked") <T> XmlSerializer<T> create(SerializerFactoryImpl factory, Class<T> beanClazz) throws SerializerFactoryException { XmlSerializer<T> serializer = (XmlSerializer<T>) serializers.get(beanClazz); if (serializer != null) { return serializer; } try { synchronized (this) { checkSerializability(beanClazz, "xml"); String intBeanClazzName = Serialization.createInternalClassName(beanClazz); Method[] methods = beanClazz.getMethods(); String intSerializerClazzName = new StringBuffer(intBeanClazzName).append("XmlSerializer") .toString(); // Create class ClassWriter writer = new ClassWriter(0); String signature = Serialization.createClassSignature("com/nginious/http/serialize/XmlSerializer", intBeanClazzName); writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, intSerializerClazzName, signature, "com/nginious/http/serialize/XmlSerializer", null); // Create constructor Serialization.createConstructor(writer, "com/nginious/http/serialize/XmlSerializer"); // Create serialize method MethodVisitor visitor = createSerializeMethod(writer, intBeanClazzName); for (Method method : methods) { Serializable info = method.getAnnotation(Serializable.class); boolean canSerialize = info == null || (info != null && info.serialize() && info.types().indexOf("xml") > -1); if (canSerialize && method.getName().startsWith("get") && !method.getName().equals("getClass") && method.getReturnType() != null && method.getParameterTypes().length == 0) { Class<?> returnType = method.getReturnType(); String propertyName = getPropertyName(method); if (returnType.isPrimitive()) { if (returnType.equals(boolean.class)) { createPropertySerializationCode(visitor, intSerializerClazzName, "serializeBoolean", "Z", "Z", intBeanClazzName, method.getName(), propertyName); } else if (returnType.equals(double.class)) { createPropertySerializationCode(visitor, intSerializerClazzName, "serializeDouble", "D", "D", intBeanClazzName, method.getName(), propertyName); } else if (returnType.equals(float.class)) { createPropertySerializationCode(visitor, intSerializerClazzName, "serializeFloat", "F", "F", intBeanClazzName, method.getName(), propertyName); } else if (returnType.equals(int.class)) { createPropertySerializationCode(visitor, intSerializerClazzName, "serializeInt", "I", "I", intBeanClazzName, method.getName(), propertyName); } else if (returnType.equals(long.class)) { createPropertySerializationCode(visitor, intSerializerClazzName, "serializeLong", "J", "J", intBeanClazzName, method.getName(), propertyName); } else if (returnType.equals(short.class)) { createPropertySerializationCode(visitor, intSerializerClazzName, "serializeShort", "S", "S", intBeanClazzName, method.getName(), propertyName); } } else if (Collection.class.isAssignableFrom(returnType)) { Class<?> collectionType = canSerializeGenericCollectionType(method, "json"); if (collectionType != null) { createBeanCollectionSerializationCode(visitor, intBeanClazzName, method.getName(), propertyName, returnType, collectionType); } else { createObjectCollectionSerializationCode(visitor, intBeanClazzName, method.getName(), propertyName, returnType); } } else if (returnType.equals(Calendar.class)) { createPropertySerializationCode(visitor, intSerializerClazzName, "serializeCalendar", "Ljava/util/Calendar;", "Ljava/util/Calendar;", intBeanClazzName, method.getName(), propertyName); } else if (returnType.equals(Date.class)) { createPropertySerializationCode(visitor, intSerializerClazzName, "serializeDate", "Ljava/util/Date;", "Ljava/util/Date;", intBeanClazzName, method.getName(), propertyName); } else if (returnType.equals(String.class)) { createPropertySerializationCode(visitor, intSerializerClazzName, "serializeString", "Ljava/lang/String;", "Ljava/lang/String;", intBeanClazzName, method.getName(), propertyName); } else { info = returnType.getAnnotation(Serializable.class); canSerialize = info != null && info.serialize() && info.types().indexOf("json") > -1; if (canSerialize) { createBeanSerializationCode(visitor, method.getName(), propertyName, returnType, intBeanClazzName); } else { createPropertySerializationCode(visitor, intSerializerClazzName, "serializeObject", "Ljava/lang/Object;", "L" + returnType.getName().replace('.', '/') + ";", intBeanClazzName, method.getName(), propertyName); } } } } visitor.visitInsn(Opcodes.RETURN); visitor.visitMaxs(7, 6); visitor.visitEnd(); writer.visitEnd(); byte[] clazzBytes = writer.toByteArray(); ClassLoader controllerLoader = null; if (classLoader.hasLoaded(beanClazz)) { controllerLoader = beanClazz.getClassLoader(); } else { controllerLoader = this.classLoader; } Class<?> clazz = Serialization.loadClass(controllerLoader, intSerializerClazzName.replace('/', '.'), clazzBytes); serializer = (XmlSerializer<T>) clazz.newInstance(); serializer.setName(Serialization.createPropertyNameFromClass(beanClazz)); serializer.setType(beanClazz); serializer.setSerializerFactory(factory); serializers.put(beanClazz, serializer); return serializer; } } catch (IllegalAccessException e) { throw new SerializerFactoryException("Can't create XML serializer for " + beanClazz.getName(), e); } catch (InstantiationException e) { throw new SerializerFactoryException("Can't create XML serializer for " + beanClazz.getName(), e); } }
From source file:com.nginious.http.serialize.XmlSerializerCreator.java
License:Apache License
/** * Creates bytecode which implements the {@link XmlSerializer#serializeProperties(javax.xml.transform.sax.TransformerHandler, Object)} * method for the serializer class being created. * //from w ww. ja v a 2 s .c om * @param writer class byte code writer * @param intBeanClazzName binary name of serializer class being generated * @return a method visitor for writing bytecode inside the generated method */ private MethodVisitor createSerializeMethod(ClassWriter writer, String intBeanClazzName) { String[] exceptions = { "com/nginious/serialize/SerializerException" }; MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "serializeProperties", "(Ljavax/xml/transform/sax/TransformerHandler;Ljava/lang/Object;)V", null, exceptions); visitor.visitCode(); Label label = new Label(); visitor.visitVarInsn(Opcodes.ALOAD, 2); visitor.visitJumpInsn(Opcodes.IFNONNULL, label); visitor.visitInsn(Opcodes.RETURN); visitor.visitLabel(label); visitor.visitVarInsn(Opcodes.ALOAD, 0); visitor.visitVarInsn(Opcodes.ALOAD, 2); visitor.visitTypeInsn(Opcodes.CHECKCAST, intBeanClazzName); visitor.visitIntInsn(Opcodes.ASTORE, 3); return visitor; }
From source file:com.nginious.http.xsp.expr.ExpressionCompiler.java
License:Apache License
/** * Creates a compiled expression from the specified tree value node expression. The class bytecode for the * compiled expression is generated at runtime. * // w w w .j av a2s . com * @param uncompiled the uncompiled tree value node expression * @return the compiled expression * @throws ExpressionException if unable to compile expression */ public Expression compile(TreeExpression uncompiled) throws ExpressionException { ClassWriter writer = new ClassWriter(0); // Create class String className = classBaseName + classNameCounter.getAndIncrement(); String classIdentifier = className.replace('.', '/'); writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, classIdentifier, "L" + classIdentifier + ";", "com/nginious/http/xsp/expr/Expression", null); // Create constructor MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "<init>", "()V", null, null); visitor.visitCode(); visitor.visitVarInsn(Opcodes.ALOAD, 0); visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/expr/Expression", "<init>", "()V"); visitor.visitInsn(Opcodes.RETURN); visitor.visitMaxs(1, 1); visitor.visitEnd(); // protected abstract boolean evaluateBoolean(); visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateBoolean", "()Z", null, null); if (uncompiled.getType() == Type.BOOLEAN) { uncompiled.compile(visitor); } else if (uncompiled.getType() == Type.DOUBLE) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateDouble", "()D"); Label endLabel = new Label(); Label falseLabel = new Label(); visitor.visitLdcInsn(0.0d); visitor.visitInsn(Opcodes.DCMPL); visitor.visitJumpInsn(Opcodes.IFEQ, falseLabel); visitor.visitLdcInsn(true); visitor.visitJumpInsn(Opcodes.GOTO, endLabel); visitor.visitLabel(falseLabel); visitor.visitLdcInsn(false); visitor.visitLabel(endLabel); } else if (uncompiled.getType() == Type.INT) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateInt", "()I"); Label endLabel = new Label(); Label falseLabel = new Label(); visitor.visitLdcInsn(0); visitor.visitJumpInsn(Opcodes.IFNE, falseLabel); visitor.visitLdcInsn(true); visitor.visitJumpInsn(Opcodes.GOTO, endLabel); visitor.visitLabel(falseLabel); visitor.visitLdcInsn(false); visitor.visitLabel(endLabel); } else if (uncompiled.getType() == Type.STRING) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateString", "()Ljava/lang/String;"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "parseBoolean", "(Ljava/lang/String;)Z"); } visitor.visitInsn(Opcodes.IRETURN); visitor.visitMaxs(5, 5); visitor.visitEnd(); // protected abstract int evaluateInt(); visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateInt", "()I", null, null); if (uncompiled.getType() == Type.BOOLEAN) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateBoolean", "()Z"); Label endLabel = new Label(); Label falseLabel = new Label(); visitor.visitJumpInsn(Opcodes.IFEQ, falseLabel); visitor.visitLdcInsn(1); visitor.visitJumpInsn(Opcodes.GOTO, endLabel); visitor.visitLabel(falseLabel); visitor.visitLdcInsn(0); visitor.visitLabel(endLabel); } else if (uncompiled.getType() == Type.DOUBLE) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateDouble", "()D"); visitor.visitInsn(Opcodes.D2I); } else if (uncompiled.getType() == Type.INT) { uncompiled.compile(visitor); } else if (uncompiled.getType() == Type.STRING) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateString", "()Ljava/lang/String;"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I"); } visitor.visitInsn(Opcodes.IRETURN); visitor.visitMaxs(5, 5); visitor.visitEnd(); // protected abstract double evaluateDouble(); visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateDouble", "()D", null, null); if (uncompiled.getType() == Type.BOOLEAN) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateBoolean", "()Z"); Label endLabel = new Label(); Label falseLabel = new Label(); visitor.visitJumpInsn(Opcodes.IFEQ, falseLabel); visitor.visitLdcInsn(1.0d); visitor.visitJumpInsn(Opcodes.GOTO, endLabel); visitor.visitLabel(falseLabel); visitor.visitLdcInsn(0.0d); visitor.visitLabel(endLabel); } else if (uncompiled.getType() == Type.DOUBLE) { uncompiled.compile(visitor); } else if (uncompiled.getType() == Type.INT) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateInt", "()I"); visitor.visitInsn(Opcodes.I2D); } else if (uncompiled.getType() == Type.STRING) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateString", "()Ljava/lang/String;"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "parseDouble", "(Ljava/lang/String;)D"); } visitor.visitInsn(Opcodes.DRETURN); visitor.visitMaxs(5, 5); visitor.visitEnd(); // protected abstract String evaluateString(); visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateString", "()Ljava/lang/String;", null, null); if (uncompiled.getType() == Type.BOOLEAN) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateBoolean", "()Z"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "toString", "(Z)Ljava/lang/String;"); } else if (uncompiled.getType() == Type.DOUBLE) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateDouble", "()D"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "toString", "(D)Ljava/lang/String;"); } else if (uncompiled.getType() == Type.INT) { visitor.visitVarInsn(Opcodes.ALOAD, 0); // this visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateInt", "()I"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;"); } else if (uncompiled.getType() == Type.STRING) { uncompiled.compile(visitor); } visitor.visitInsn(Opcodes.ARETURN); visitor.visitMaxs(6, 6); visitor.visitEnd(); // public abstract Type getType(); visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "getType", "()Lcom/nginious/http/xsp/expr/Type;", null, null); if (uncompiled.getType() == Type.BOOLEAN) { visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "BOOLEAN", "Lcom/nginious/http/xsp/expr/Type;"); visitor.visitInsn(Opcodes.ARETURN); } else if (uncompiled.getType() == Type.DOUBLE) { visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "DOUBLE", "Lcom/nginious/http/xsp/expr/Type;"); visitor.visitInsn(Opcodes.ARETURN); } else if (uncompiled.getType() == Type.INT) { visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "INT", "Lcom/nginious/http/xsp/expr/Type;"); visitor.visitInsn(Opcodes.ARETURN); } else if (uncompiled.getType() == Type.STRING) { visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "STRING", "Lcom/nginious/http/xsp/expr/Type;"); visitor.visitInsn(Opcodes.ARETURN); } visitor.visitMaxs(1, 1); visitor.visitEnd(); try { writer.visitEnd(); byte[] clazzBytes = writer.toByteArray(); Class<?> clazz = loadClass(className, clazzBytes); return (Expression) clazz.newInstance(); } catch (Exception e) { throw new ExpressionException("Can't instantiate compiled expression", e); } }
From source file:com.nginious.http.xsp.XspCompiler.java
License:Apache License
/** * Creates subclass of {@link XspService} for the XSP file represented by the specified document * tree node structure./*from ww w . j a v a2 s . co m*/ * * @param document the document tree node structure * @param srcFilePath the XSP file source path * @return a descriptor for the generated subclass * @throws XspException if unable to create subclass */ private ClassDescriptor compileService(DocumentPart document, String srcFilePath) throws XspException { ClassWriter writer = new ClassWriter(0); // Create class String packageName = document.getMetaContent("package"); String intServiceClazzName = createIntServiceClassName(packageName, srcFilePath); String serviceClazzName = createServiceClassName(packageName, srcFilePath); writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, intServiceClazzName, "Lcom/nginious/http/xsp/XspService;", "com/nginious/http/xsp/XspService", null); // Create constructor createConstructor(writer, "com/nginious/http/xsp/XspService"); // Create xsp service method MethodVisitor visitor = createXspMethod(writer); Label tryLabel = new Label(); Label startCatchLabel = new Label(); Label endCatchLabel = new Label(); // Start try block visitor.visitTryCatchBlock(tryLabel, startCatchLabel, endCatchLabel, "java/lang/Throwable"); visitor.visitLabel(tryLabel); visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/xsp/expr/HttpRequestVariables"); visitor.visitInsn(Opcodes.DUP); visitor.visitVarInsn(Opcodes.ALOAD, 1); visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/expr/HttpRequestVariables", "<init>", "(Lcom/nginious/http/HttpRequest;)V"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "setVariables", "(Lcom/nginious/http/xsp/expr/Variables;)V"); document.compile(intServiceClazzName, writer, visitor); visitor.visitLabel(startCatchLabel); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "removeVariables", "()V"); visitor.visitLdcInsn(true); visitor.visitInsn(Opcodes.IRETURN); // Start finally block visitor.visitLabel(endCatchLabel); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "removeVariables", "()V"); visitor.visitInsn(Opcodes.ATHROW); visitor.visitMaxs(12, 12); visitor.visitEnd(); document.compileMethod(intServiceClazzName, writer); writer.visitEnd(); byte[] clazzBytes = writer.toByteArray(); return new ClassDescriptor(serviceClazzName, clazzBytes); }
From source file:com.nginious.http.xsp.XspCompiler.java
License:Apache License
/** * Creates a constructor with no arguments for the XSP service class being created with the * specified class writer. Bytecode is created for calling the superclass constructor in the * superclass with the specified supperclass name. * /* w ww.j a v a 2s . c o m*/ * @param writer the class writer * @param superclassName the superclass name * @see XspService */ void createConstructor(ClassWriter writer, String superclassName) { MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); visitor.visitCode(); visitor.visitVarInsn(Opcodes.ALOAD, 0); visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassName, "<init>", "()V"); visitor.visitInsn(Opcodes.RETURN); visitor.visitMaxs(1, 1); visitor.visitEnd(); }
From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java
License:Apache License
/** * * /*w w w . j av a2s . c om*/ * * @param cw * @param mv * @param processorName com/nway/commons/dbutils/DynamicBeanProcessorImpl * @param beanName com/nway/commons/dbutils/test/User * @return [0]:bean[1]createBean */ private Object[] prepScript(ClassWriter cw, MethodVisitor mv, String processorName, String beanName) { Object[] lab = new Object[2]; cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, processorName, null, "com/nway/spring/jdbc/bean/DbBeanFactory", null); cw.visitSource(processorName.substring(processorName.lastIndexOf('/') + 1) + ".java", null); { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(6, l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nway/spring/jdbc/bean/DbBeanFactory", "<init>", "()V", false); mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + processorName + ";", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "createBean", "(Ljava/sql/ResultSet;Ljava/lang/Class;)Ljava/lang/Object;", "<T:Ljava/lang/Object;>(Ljava/sql/ResultSet;Ljava/lang/Class<TT;>;)TT;", new String[] { "java/sql/SQLException" }); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(10, l0); mv.visitTypeInsn(Opcodes.NEW, beanName); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, beanName, "<init>", "()V", false); mv.visitVarInsn(Opcodes.ASTORE, 3); lab[0] = l0; lab[1] = mv; } return lab; }