use of org.glassfish.hk2.external.org.objectweb.asm.ClassWriter in project Payara by payara.
the class ProviderImplGenerator method generateConstructor.
private void generateConstructor(ClassWriter cw, String generatedClassName, FlashlightProbeProvider provider) {
Method m = Method.getMethod("void <init> ()");
GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, null, cw);
gen.loadThis();
gen.invokeConstructor(Type.getType(Object.class), m);
Type probeRegType = Type.getType(ProbeRegistry.class);
Type probeType = Type.getType(FlashlightProbe.class);
gen.loadThis();
for (FlashlightProbe probe : provider.getProbes()) {
gen.dup();
String fieldName = "_flashlight_" + probe.getProbeName();
gen.push(probe.getId());
gen.invokeStatic(probeRegType, Method.getMethod("org.glassfish.flashlight.provider.FlashlightProbe getProbeById(int)"));
gen.visitFieldInsn(Opcodes.PUTFIELD, generatedClassName, fieldName, probeType.getDescriptor());
}
gen.pop();
// return the value from constructor
gen.returnValue();
gen.endMethod();
}
use of org.glassfish.hk2.external.org.objectweb.asm.ClassWriter in project Payara by payara.
the class CompositeUtil method visitClass.
/**
* This method starts the class definition, adding the JAX-B annotations to allow for marshalling via JAX-RS
*/
private void visitClass(ClassWriter classWriter, String className, Set<Class<?>> ifaces, Map<String, Map<String, Object>> properties) {
String[] ifaceNames = new String[ifaces.size() + 1];
int i = 1;
ifaceNames[0] = getInternalName(RestModel.class.getName());
for (Class<?> iface : ifaces) {
ifaceNames[i++] = iface.getName().replace(".", "/");
}
className = getInternalName(className);
classWriter.visit(V1_6, ACC_PUBLIC + ACC_SUPER, className, null, "org/glassfish/admin/rest/composite/RestModelImpl", ifaceNames);
// Add @XmlRootElement
classWriter.visitAnnotation("Ljavax/xml/bind/annotation/XmlRootElement;", true).visitEnd();
// Add @XmlAccessType
AnnotationVisitor annotation = classWriter.visitAnnotation("Ljavax/xml/bind/annotation/XmlAccessorType;", true);
annotation.visitEnum("value", "Ljavax/xml/bind/annotation/XmlAccessType;", "FIELD");
annotation.visitEnd();
}
use of org.glassfish.hk2.external.org.objectweb.asm.ClassWriter in project Payara by payara.
the class CompositeUtil method createGettersAndSetters.
/**
* Create getters and setters for the given field
*/
private void createGettersAndSetters(ClassWriter cw, Class c, String className, String name, Map<String, Object> props) {
Class<?> type = (Class<?>) props.get("type");
String internalType = getInternalTypeString(type);
className = getInternalName(className);
// Create the getter
MethodVisitor getter = cw.visitMethod(ACC_PUBLIC, "get" + name, "()" + internalType, null, null);
getter.visitCode();
getter.visitVarInsn(ALOAD, 0);
getter.visitFieldInsn(GETFIELD, className, getPropertyName(name), internalType);
getter.visitInsn(type.isPrimitive() ? Primitive.getPrimitive(internalType).getReturnOpcode() : ARETURN);
getter.visitMaxs(0, 0);
getter.visitEnd();
Map<String, Map<String, Object>> annotations = (Map<String, Map<String, Object>>) props.get("annotations");
if (annotations != null) {
for (Map.Entry<String, Map<String, Object>> entry : annotations.entrySet()) {
String annotationClass = entry.getKey();
Map<String, Object> annotationValues = entry.getValue();
AnnotationVisitor av = getter.visitAnnotation("L" + getInternalName(annotationClass) + ";", true);
for (Map.Entry<String, Object> values : annotationValues.entrySet()) {
final String paramName = values.getKey();
Object paramValue = values.getValue();
if (Class.class.isAssignableFrom(paramValue.getClass())) {
paramValue = org.glassfish.hk2.external.org.objectweb.asm.Type.getType("L" + getInternalName(paramValue.getClass().getName()) + ";");
}
if (paramValue.getClass().isArray() && (Array.getLength(paramValue) == 0)) {
continue;
}
av.visit(paramName, paramValue);
}
av.visitEnd();
}
}
// Create the setter
MethodVisitor setter = cw.visitMethod(ACC_PUBLIC, "set" + name, "(" + internalType + ")V", null, null);
setter.visitCode();
setter.visitVarInsn(ALOAD, 0);
setter.visitVarInsn(type.isPrimitive() ? Primitive.getPrimitive(internalType).getSetOpCode() : ALOAD, 1);
setter.visitFieldInsn(PUTFIELD, className, getPropertyName(name), internalType);
setter.visitVarInsn(ALOAD, 0);
setter.visitLdcInsn(name);
setter.visitMethodInsn(INVOKEVIRTUAL, className, "fieldSet", "(Ljava/lang/String;)V");
setter.visitInsn(RETURN);
setter.visitMaxs(0, 0);
setter.visitEnd();
}
use of org.glassfish.hk2.external.org.objectweb.asm.ClassWriter in project Payara by payara.
the class CompositeUtil method createField.
/**
* Add the field to the class, adding the @XmlAttribute annotation for marshalling purposes.
*/
private void createField(ClassWriter cw, String name, Class<?> type) {
String internalType = getInternalTypeString(type);
FieldVisitor field = cw.visitField(ACC_PRIVATE, getPropertyName(name), internalType, null, null);
field.visitAnnotation("Ljavax/xml/bind/annotation/XmlAttribute;", true).visitEnd();
field.visitEnd();
}
Aggregations