Search in sources :

Example 6 with ProxyProxettaBuilder

use of jodd.proxetta.impl.ProxyProxettaBuilder in project jodd by oblac.

the class SubclassTest method testJdk.

@Test
public void testJdk() throws Exception {
    ProxyProxetta proxyProxetta = ProxyProxetta.withAspects(new ProxyAspect(StatCounterAdvice.class, new AllMethodsPointcut()));
    proxyProxetta.setVariableClassName(false);
    ProxyProxettaBuilder builder = proxyProxetta.builder();
    builder.setTarget(Object.class);
    try {
        builder.define();
        fail("Default class loader should not load java.*");
    } catch (RuntimeException rex) {
    // ignore
    }
    builder = proxyProxetta.builder();
    builder.setTarget(Object.class);
    builder.setTargetProxyClassName("foo.");
    Object object = builder.newInstance();
    assertNotNull(object);
    assertEquals("foo.Object$$Proxetta", object.getClass().getName());
    System.out.println("----------list");
    StatCounter.counter = 0;
    builder = proxyProxetta.builder(ArrayList.class, "foo.");
    List list = (List) builder.newInstance();
    assertNotNull(list);
    assertEquals("foo.ArrayList$$Proxetta", list.getClass().getName());
    assertEquals(1, StatCounter.counter);
    list.add(new Integer(1));
    assertTrue(StatCounter.counter == 3 || StatCounter.counter == 2);
    System.out.println("----------set");
    builder = proxyProxetta.builder(HashSet.class, "foo.");
    Set set = (Set) builder.newInstance();
    assertNotNull(set);
    assertEquals("foo.HashSet$$Proxetta", set.getClass().getName());
    assertTrue(StatCounter.counter == 4 || StatCounter.counter == 3);
    set.add(new Integer(1));
    assertTrue(StatCounter.counter == 5 || StatCounter.counter == 4);
}
Also used : ProxyProxetta(jodd.proxetta.impl.ProxyProxetta) AllMethodsPointcut(jodd.proxetta.pointcuts.AllMethodsPointcut) HashSet(java.util.HashSet) Set(java.util.Set) ProxyProxettaBuilder(jodd.proxetta.impl.ProxyProxettaBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with ProxyProxettaBuilder

use of jodd.proxetta.impl.ProxyProxettaBuilder in project jodd by oblac.

the class PathrefProxetta method defineProxy.

/**
	 * Generates new class.
	 */
public Class defineProxy(Class target) {
    ProxyProxettaBuilder builder = proxetta.builder();
    builder.setTarget(target);
    return builder.define();
}
Also used : ProxyProxettaBuilder(jodd.proxetta.impl.ProxyProxettaBuilder)

Example 8 with ProxyProxettaBuilder

use of jodd.proxetta.impl.ProxyProxettaBuilder in project jodd by oblac.

the class PetiteProxettaContainer method createBeanDefinitionForRegistration.

//    @Override
//    public BeanDefinition registerPetiteBean(
//            Class type,
//            String name,
//            Class<? extends Scope> scopeType,
//            WiringMode wiringMode,
//            boolean define) {
//
//        if (name == null) {
//            name = PetiteUtil.resolveBeanName(type, false);
//        }
//
//        ProxyProxettaBuilder builder = proxetta.builder();
//        builder.setTarget(type);
//        type = builder.define();
//
//        return super.registerPetiteBean(type, name, scopeType, wiringMode, false);
//    }
/**
     * Applies proxetta on bean class before bean registration.
     */
@Override
protected BeanDefinition createBeanDefinitionForRegistration(String name, Class type, Scope scope, WiringMode wiringMode) {
    if (proxetta != null) {
        ProxyProxettaBuilder builder = proxetta.builder();
        builder.setTarget(type);
        type = builder.define();
    }
    return super.createBeanDefinitionForRegistration(name, type, scope, wiringMode);
}
Also used : ProxyProxettaBuilder(jodd.proxetta.impl.ProxyProxettaBuilder)

Example 9 with ProxyProxettaBuilder

use of jodd.proxetta.impl.ProxyProxettaBuilder in project jodd by oblac.

the class MethodInfoTest method testMethodInfo.

@Test
public void testMethodInfo() {
    final ValueHolder<MethodInfo> valueHolder = ValueHolderWrapper.create();
    ProxyAspect proxyAspect = new ProxyAspect(FooProxyAdvice.class, new ProxyPointcut() {

        public boolean apply(MethodInfo methodInfo) {
            if (methodInfo.getMethodName().equals("p1")) {
                valueHolder.set(methodInfo);
                return true;
            }
            return false;
        }
    });
    ProxyProxetta proxyProxetta = ProxyProxetta.withAspects(proxyAspect);
    proxyProxetta.setClassNameSuffix("$$$Proxetta888");
    ProxyProxettaBuilder pb = proxyProxetta.builder();
    pb.setTarget(Foo.class);
    Foo foo = (Foo) pb.newInstance();
    assertNotNull(foo);
    MethodInfo mi = valueHolder.get();
    assertEquals("p1", mi.getMethodName());
    assertEquals(Foo.class.getName().replace('.', '/'), mi.getClassname());
    assertEquals("(java.lang.String)", mi.getDeclaration());
    assertEquals("(Ljava/lang/String;)Ljava/lang/String;", mi.getDescription());
    assertEquals("java.lang.String", mi.getReturnType());
    assertEquals("Ljava/lang/String;", mi.getReturnTypeName());
    assertEquals("java.lang.String p1(java.lang.String)", mi.getSignature());
    assertEquals(1, mi.getArgumentsCount());
    assertEquals("Ljava/lang/String;", mi.getArgumentTypeName(1));
    assertTrue(mi.isTopLevelMethod());
    AnnotationInfo[] anns = mi.getArgumentAnnotations(0);
    assertNotNull(anns);
    assertEquals(1, anns.length);
    assertEquals(FooAnn.class.getName(), anns[0].getAnnotationClassname());
}
Also used : ProxyProxetta(jodd.proxetta.impl.ProxyProxetta) ProxyProxettaBuilder(jodd.proxetta.impl.ProxyProxettaBuilder) Foo(jodd.proxetta.data.Foo) FooAnn(jodd.proxetta.data.FooAnn) Test(org.junit.Test)

Example 10 with ProxyProxettaBuilder

use of jodd.proxetta.impl.ProxyProxettaBuilder in project jodd by oblac.

the class ReturnTest method testNullReturns.

@Test
public void testNullReturns() throws Exception {
    ProxyProxetta proxetta = ProxyProxetta.withAspects(new ProxyAspect(ReturnNullAdvice.class, new AllMethodsPointcut()));
    ProxyProxettaBuilder builder = proxetta.builder(Retro.class, ".Retro3");
    //		proxetta.setDebugFolder("d:\\");
    Class proxyClass = builder.define();
    Object proxy = proxyClass.newInstance();
    Retro retro = (Retro) proxy;
    assertNotNull(retro);
    assertNull(retro.method1());
    assertEquals(0, retro.method2());
    assertEquals(0, retro.method3());
    assertEquals(0, retro.method4());
    assertEquals(0, retro.method5());
    assertEquals(false, retro.method6());
    assertEquals(0, retro.method7(), 0.005);
    assertEquals(0, retro.method8(), 0.005);
    assertNull(retro.method9());
    assertEquals(0, retro.method11());
    retro.method10();
}
Also used : ProxyProxetta(jodd.proxetta.impl.ProxyProxetta) AllMethodsPointcut(jodd.proxetta.pointcuts.AllMethodsPointcut) ProxyProxettaBuilder(jodd.proxetta.impl.ProxyProxettaBuilder) ReturnNullAdvice(jodd.proxetta.data.ReturnNullAdvice) Retro(jodd.proxetta.data.Retro) Test(org.junit.Test)

Aggregations

ProxyProxettaBuilder (jodd.proxetta.impl.ProxyProxettaBuilder)14 ProxyProxetta (jodd.proxetta.impl.ProxyProxetta)10 Test (org.junit.Test)9 AllMethodsPointcut (jodd.proxetta.pointcuts.AllMethodsPointcut)5 Field (java.lang.reflect.Field)2 Retro (jodd.proxetta.data.Retro)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 ProxettaBeanDefinition (jodd.petite.proxetta.ProxettaBeanDefinition)1 ProxyAspect (jodd.proxetta.ProxyAspect)1 Foo (jodd.proxetta.data.Foo)1 FooAnn (jodd.proxetta.data.FooAnn)1 ReflectionReplacementAdvice (jodd.proxetta.data.ReflectionReplacementAdvice)1 ReturnNullAdvice (jodd.proxetta.data.ReturnNullAdvice)1