Search in sources :

Example 1 with AllMethodsPointcut

use of jodd.proxetta.pointcuts.AllMethodsPointcut in project jodd by oblac.

the class ReturnTest method testWrapperWithProxyReturns.

@Test
public void testWrapperWithProxyReturns() throws Exception {
    ProxyProxetta proxetta = ProxyProxetta.withAspects(new ProxyAspect(ReflectionReplacementAdvice.class, new AllMethodsPointcut()));
    ProxyProxettaBuilder builder = proxetta.builder(Retro.class, ".Retro2");
    //		proxetta.setDebugFolder("d:\\");
    Class proxyClass = builder.define();
    Object proxy = proxyClass.newInstance();
    Field field = proxyClass.getField("$__target$0");
    Retro retro = new Retro();
    retro.flag = true;
    field.set(proxy, retro);
    retro = (Retro) proxy;
    assertNotNull(retro);
    assertEquals("retro", retro.method1());
    assertEquals(2, retro.method2());
    assertEquals(3, retro.method3());
    assertEquals(4, retro.method4());
    assertEquals(5, retro.method5());
    assertEquals(true, retro.method6());
    assertEquals(7.7, retro.method7(), 0.005);
    assertEquals(8.8, retro.method8(), 0.005);
    assertEquals(9, retro.method9().length);
    assertEquals('r', retro.method11());
    retro.method10();
}
Also used : ProxyProxetta(jodd.proxetta.impl.ProxyProxetta) Field(java.lang.reflect.Field) AllMethodsPointcut(jodd.proxetta.pointcuts.AllMethodsPointcut) ProxyProxettaBuilder(jodd.proxetta.impl.ProxyProxettaBuilder) ReflectionReplacementAdvice(jodd.proxetta.data.ReflectionReplacementAdvice) Retro(jodd.proxetta.data.Retro) Test(org.junit.Test)

Example 2 with AllMethodsPointcut

use of jodd.proxetta.pointcuts.AllMethodsPointcut 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 3 with AllMethodsPointcut

use of jodd.proxetta.pointcuts.AllMethodsPointcut 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)

Example 4 with AllMethodsPointcut

use of jodd.proxetta.pointcuts.AllMethodsPointcut in project jodd by oblac.

the class SubclassTest method testProxyClassNames.

@Test
public void testProxyClassNames() {
    ProxyProxetta proxyProxetta = ProxyProxetta.withAspects(new ProxyAspect(FooProxyAdvice.class, new AllMethodsPointcut()));
    proxyProxetta.setVariableClassName(true);
    ProxyProxettaBuilder builder = proxyProxetta.builder();
    builder.setTarget(Foo.class);
    Foo foo = (Foo) builder.newInstance();
    assertNotNull(foo);
    assertEquals(Foo.class.getName() + "$$Proxetta", StringUtil.substring(foo.getClass().getName(), 0, -1));
    builder = proxyProxetta.builder();
    builder.setTarget(Foo.class);
    foo = (Foo) builder.newInstance();
    assertNotNull(foo);
    assertEquals(Foo.class.getName() + "$$Proxetta", StringUtil.substring(foo.getClass().getName(), 0, -1));
    proxyProxetta.setClassNameSuffix("$$Ppp");
    builder = proxyProxetta.builder();
    builder.setTarget(Foo.class);
    foo = (Foo) builder.newInstance();
    assertNotNull(foo);
    assertEquals(Foo.class.getName() + "$$Ppp", StringUtil.substring(foo.getClass().getName(), 0, -1));
    proxyProxetta.setClassNameSuffix("$$Proxetta");
    proxyProxetta.setVariableClassName(false);
    builder = proxyProxetta.builder(Foo.class, ".Too");
    foo = (Foo) builder.newInstance();
    assertNotNull(foo);
    assertEquals(Foo.class.getPackage().getName() + ".Too$$Proxetta", foo.getClass().getName());
    builder = proxyProxetta.builder();
    builder.setTarget(Foo.class);
    builder.setTargetProxyClassName("foo.");
    foo = (Foo) builder.newInstance();
    assertNotNull(foo);
    assertEquals("foo.Foo$$Proxetta", foo.getClass().getName());
    proxyProxetta.setClassNameSuffix(null);
    builder = proxyProxetta.builder();
    builder.setTargetProxyClassName("foo.Fff");
    builder.setTarget(Foo.class);
    foo = (Foo) builder.newInstance();
    assertNotNull(foo);
    assertEquals("foo.Fff", foo.getClass().getName());
}
Also used : ProxyProxetta(jodd.proxetta.impl.ProxyProxetta) AllMethodsPointcut(jodd.proxetta.pointcuts.AllMethodsPointcut) ProxyProxettaBuilder(jodd.proxetta.impl.ProxyProxettaBuilder) Test(org.junit.Test)

Example 5 with AllMethodsPointcut

use of jodd.proxetta.pointcuts.AllMethodsPointcut in project jodd by oblac.

the class SubclassTest method testInnerOverride.

@Test
public void testInnerOverride() {
    ProxyProxetta proxyProxetta = ProxyProxetta.withAspects(new ProxyAspect(FooProxyAdvice.class, new AllMethodsPointcut()));
    ProxyProxettaBuilder builder = proxyProxetta.builder();
    builder.setTarget(Two.class);
    builder.setTargetProxyClassName("foo.");
    Two two = (Two) builder.newInstance();
    assertNotNull(two);
    assertEquals("foo.Two$$Proxetta", two.getClass().getName());
}
Also used : ProxyProxetta(jodd.proxetta.impl.ProxyProxetta) AllMethodsPointcut(jodd.proxetta.pointcuts.AllMethodsPointcut) ProxyProxettaBuilder(jodd.proxetta.impl.ProxyProxettaBuilder) Test(org.junit.Test)

Aggregations

ProxyProxetta (jodd.proxetta.impl.ProxyProxetta)5 ProxyProxettaBuilder (jodd.proxetta.impl.ProxyProxettaBuilder)5 AllMethodsPointcut (jodd.proxetta.pointcuts.AllMethodsPointcut)5 Test (org.junit.Test)5 Retro (jodd.proxetta.data.Retro)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 ReflectionReplacementAdvice (jodd.proxetta.data.ReflectionReplacementAdvice)1 ReturnNullAdvice (jodd.proxetta.data.ReturnNullAdvice)1