use of org.apache.xbean.asm5.Type in project tomee by apache.
the class Cmp2Generator method createSetter.
/**
* Generate a concrete setter field for a CMP field.
* At this point, we're just generating a simple
* accessor for the field, given the type. The
* JPA engine when it makes this implementation class
* a managed class define whatever additional logic
* might be required.
*
* @param cmpField The CMP field backing this setter method.
*/
private void createSetter(final CmpField cmpField) {
final String methodName = setterName(cmpField.getName());
final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, "(" + cmpField.getDescriptor() + ")V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(cmpField.getType().getOpcode(ILOAD), 1);
mv.visitFieldInsn(PUTFIELD, implClassName, cmpField.getName(), cmpField.getDescriptor());
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.apache.xbean.asm5.Type in project tomee by apache.
the class CmrField method createSignature.
private static String createSignature(final Type type, final Type... genericTypes) {
final StringBuilder builder = new StringBuilder();
builder.append("L").append(type.getInternalName());
if (genericTypes.length > 0) {
builder.append("<");
for (final Type genericType : genericTypes) {
builder.append(genericType.getDescriptor());
}
builder.append(">");
}
builder.append(";");
return builder.toString();
}
use of org.apache.xbean.asm5.Type in project tomee by apache.
the class DynamicSubclass method visitConstructor.
private static MethodVisitor visitConstructor(final ClassWriter cw, final String proxyClassFileName, final String classFileName, final Constructor<?> constructor) {
final String descriptor = Type.getConstructorDescriptor(constructor);
final String[] exceptions = new String[constructor.getExceptionTypes().length];
for (int i = 0; i < exceptions.length; i++) {
exceptions[i] = Type.getInternalName(constructor.getExceptionTypes()[i]);
}
final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, exceptions);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
int index = 1;
for (final Type type : Type.getArgumentTypes(descriptor)) {
mv.visitVarInsn(type.getOpcode(ILOAD), index);
index += size(type);
}
mv.visitMethodInsn(INVOKESPECIAL, classFileName, "<init>", descriptor, false);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(PUTFIELD, proxyClassFileName, "this$handler", "Ljava/lang/reflect/InvocationHandler;");
mv.visitInsn(RETURN);
mv.visitMaxs(2, 1);
return mv;
}
use of org.apache.xbean.asm5.Type in project apex-core by apache.
the class ClassNodeType method visit.
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
// parse the signature first so Type variable can be captured from the signature
if (signature != null) {
SignatureReader sr = new SignatureReader(signature);
sr.accept(csv);
}
super.visit(version, access, name, signature, superName, interfaces);
}
use of org.apache.xbean.asm5.Type in project jodd by oblac.
the class BigClassTest method testAllFeatures.
@Test
public void testAllFeatures() throws IOException, IllegalAccessException, InstantiationException {
StatCounter.counter = 0;
final MutableBoolean firstTime = new MutableBoolean(true);
ProxyAspect aspect = new ProxyAspect(StatCounterAdvice.class, new ProxyPointcutSupport() {
public boolean apply(MethodInfo mi) {
if (firstTime.value) {
firstTime.value = false;
ClassInfo ci = mi.getClassInfo();
assertEquals("BigFatJoe", ci.getClassname());
assertEquals(BigFatJoe.class.getPackage().getName(), ci.getPackage());
assertEquals("jodd/proxetta/data/BigFatJoe", ci.getReference());
assertEquals("jodd/proxetta/data/SmallSkinnyZoe", ci.getSuperName());
AnnotationInfo[] anns = ci.getAnnotations();
assertNotNull(anns);
assertEquals(3, anns.length);
AnnotationInfo ai = anns[0];
assertSame(ai, getAnnotation(ci, MadvocAction.class));
assertEquals(MadvocAction.class.getName(), ai.getAnnotationClassname());
assertEquals("L" + MadvocAction.class.getName().replace('.', '/') + ";", ai.getAnnotationSignature());
assertEquals("madvocAction", ai.getElement("value"));
ai = anns[1];
assertSame(ai, getAnnotation(ci, PetiteBean.class));
assertEquals(PetiteBean.class.getName(), ai.getAnnotationClassname());
assertEquals("L" + PetiteBean.class.getName().replace('.', '/') + ";", ai.getAnnotationSignature());
ai = anns[2];
assertSame(ai, getAnnotation(ci, InterceptedBy.class));
assertEquals(InterceptedBy.class.getName(), ai.getAnnotationClassname());
assertEquals("L" + InterceptedBy.class.getName().replace('.', '/') + ";", ai.getAnnotationSignature());
assertTrue(ai.getElement("value") instanceof Object[]);
assertFalse(ai.getElement("value") instanceof String[]);
Object c1 = ((Object[]) ai.getElement("value"))[0];
assertEquals("Ljodd/proxetta/data/Str;", ((Type) c1).getDescriptor());
}
if (mi.getMethodName().equals("publicMethod")) {
AnnotationInfo[] anns = mi.getAnnotations();
assertNotNull(anns);
assertEquals(3, anns.length);
AnnotationInfo ai = anns[0];
assertSame(ai, getAnnotation(mi, Action.class));
assertEquals(Action.class.getName(), ai.getAnnotationClassname());
assertEquals("value", ai.getElement("value"));
assertEquals("alias", ai.getElement("alias"));
ai = anns[1];
assertSame(ai, getAnnotation(mi, PetiteInject.class));
assertEquals(PetiteInject.class.getName(), ai.getAnnotationClassname());
assertEquals(0, ai.getElementNames().size());
ai = anns[2];
assertSame(ai, getAnnotation(mi, Transaction.class));
assertEquals(Transaction.class.getName(), ai.getAnnotationClassname());
assertEquals(2, ai.getElementNames().size());
String s = (String) ai.getElement("propagation");
assertEquals("PROPAGATION_REQUIRES_NEW", s);
}
if (mi.getMethodName().equals("superPublicMethod")) {
AnnotationInfo[] anns = mi.getAnnotations();
assertNotNull(anns);
assertEquals(3, anns.length);
AnnotationInfo ai = anns[0];
assertSame(ai, getAnnotation(mi, Action.class));
assertEquals(Action.class.getName(), ai.getAnnotationClassname());
assertEquals(0, ai.getElementNames().size());
ai = anns[1];
assertSame(ai, getAnnotation(mi, PetiteInject.class));
assertEquals(PetiteInject.class.getName(), ai.getAnnotationClassname());
assertEquals(0, ai.getElementNames().size());
ai = anns[2];
assertSame(ai, getAnnotation(mi, Transaction.class));
assertEquals(Transaction.class.getName(), ai.getAnnotationClassname());
assertEquals(0, ai.getElementNames().size());
}
//System.out.println(!isRootMethod(mi) + " " + mi.getDeclaredClassName() + '#' + mi.getMethodName());
return !isRootMethod(mi);
}
});
byte[] classBytes = ProxyProxetta.withAspects(aspect).builder(BigFatJoe.class).create();
// URL resource = BigFatJoe.class.getResource("/" + BigFatJoe.class.getName().replace(".", "/") + ".class");
// jodd.io.FileUtil.copy(FileUtil.toFile(resource), new java.io.File(SystemUtil.getUserHome(), "jo.class"));
// jodd.io.FileUtil.writeBytes(new java.io.File(SystemUtil.getUserHome(), "joe.class"), classBytes);
Class clazz = ClassLoaderUtil.defineClass(null, classBytes);
BigFatJoe bigFatJoe = (BigFatJoe) clazz.newInstance();
assertEquals(BigFatJoe.class.getName() + "$$Proxetta", bigFatJoe.getClass().getName());
assertEquals(BigFatJoe.class, ProxettaUtil.getTargetClass(bigFatJoe.getClass()));
// test invocation
// 2 x static + 1 x instance
assertEquals(3, StatCounter.counter);
bigFatJoe.publicMethod();
assertEquals(4, StatCounter.counter);
bigFatJoe.callInnerMethods();
// private method is not overridden
assertEquals(7, StatCounter.counter);
bigFatJoe.superPublicMethod();
assertEquals(8, StatCounter.counter);
bigFatJoe.callInnerMethods2();
// only public super methods are overridden
assertEquals(9, StatCounter.counter);
// test class annotation
MadvocAction ma = (MadvocAction) clazz.getAnnotation(MadvocAction.class);
assertEquals("madvocAction", ma.value());
InterceptedBy ib = (InterceptedBy) clazz.getAnnotation(InterceptedBy.class);
assertNotNull(ib.value());
assertEquals(2, ib.value().length);
// test method annotation
ClassDescriptor cd = ClassIntrospector.lookup(clazz);
Method m = cd.getMethodDescriptor("publicMethod", false).getMethod();
assertNotNull(m);
Annotation[] aa = m.getAnnotations();
assertEquals(3, aa.length);
Action a = (Action) aa[0];
assertEquals("alias", a.alias());
assertEquals("extension", a.extension());
assertEquals("method", a.method());
assertEquals("value", a.value());
PetiteInject pi = (PetiteInject) aa[1];
assertEquals("", pi.value());
Transaction tx = (Transaction) aa[2];
assertTrue(tx.readOnly());
assertEquals(1000, tx.timeout());
assertEquals("PROPAGATION_REQUIRES_NEW", tx.propagation());
bigFatJoe.runInnerClass();
// proxy + call
assertEquals(11, StatCounter.counter);
}
Aggregations