Skip to content

Commit 26ded9b

Browse files
committed
Merge branch 'tomekl007-BAEL-633'
2 parents 72d5bfc + dbc52e8 commit 26ded9b

11 files changed

Lines changed: 205 additions & 0 deletions

File tree

‎cglib/pom.xml‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>parent-modules</artifactId>
7+
<groupId>com.baeldung</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>cglib</artifactId>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-compiler-plugin</artifactId>
18+
<configuration>
19+
<source>1.8</source>
20+
<target>1.8</target>
21+
</configuration>
22+
</plugin>
23+
</plugins>
24+
</build>
25+
26+
<dependencies>
27+
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
28+
<dependency>
29+
<groupId>cglib</groupId>
30+
<artifactId>cglib</artifactId>
31+
<version>${cglib.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>junit</groupId>
35+
<artifactId>junit</artifactId>
36+
<version>${junit.version}</version>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
41+
<properties>
42+
<cglib.version>3.2.4</cglib.version>
43+
<junit.version>4.12</junit.version>
44+
</properties>
45+
46+
47+
</project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.cglib.mixin;
2+
3+
public class Class1 implements Interface1 {
4+
@Override
5+
public String first() {
6+
return "first behaviour";
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.cglib.mixin;
2+
3+
public class Class2 implements Interface2 {
4+
@Override
5+
public String second() {
6+
return "second behaviour";
7+
}
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.cglib.mixin;
2+
3+
public interface Interface1 {
4+
String first();
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.cglib.mixin;
2+
3+
public interface Interface2 {
4+
String second();
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.cglib.mixin;
2+
3+
public interface MixinInterface extends Interface1, Interface2 {
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.cglib.proxy;
2+
3+
public class PersonService {
4+
public String sayHello(String name) {
5+
return "Hello " + name;
6+
}
7+
8+
public Integer lengthOfName(String name) {
9+
return name.length();
10+
}
11+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.cglib.proxy;
2+
3+
4+
import net.sf.cglib.beans.BeanGenerator;
5+
import org.junit.Test;
6+
7+
import java.lang.reflect.Method;
8+
9+
import static junit.framework.TestCase.assertEquals;
10+
11+
public class BeanGeneratorTest {
12+
13+
@Test
14+
public void givenBeanCreator_whenAddProperty_thenClassShouldHaveFieldValue() throws Exception {
15+
//given
16+
BeanGenerator beanGenerator = new BeanGenerator();
17+
18+
//when
19+
beanGenerator.addProperty("name", String.class);
20+
Object myBean = beanGenerator.create();
21+
Method setter = myBean
22+
.getClass()
23+
.getMethod("setName", String.class);
24+
setter.invoke(myBean, "some string value set by a cglib");
25+
26+
//then
27+
Method getter = myBean
28+
.getClass()
29+
.getMethod("getName");
30+
assertEquals("some string value set by a cglib", getter.invoke(myBean));
31+
}
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.cglib.proxy;
2+
3+
import com.baeldung.cglib.mixin.*;
4+
import net.sf.cglib.proxy.Mixin;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class MixinTest {
10+
11+
@Test
12+
public void givenTwoClasses_whenMixedIntoOne_thenMixinShouldHaveMethodsFromBothClasses() throws Exception {
13+
//when
14+
Mixin mixin = Mixin.create(
15+
new Class[]{Interface1.class, Interface2.class, MixinInterface.class},
16+
new Object[]{new Class1(), new Class2()}
17+
);
18+
MixinInterface mixinDelegate = (MixinInterface) mixin;
19+
20+
//then
21+
assertEquals("first behaviour", mixinDelegate.first());
22+
assertEquals("second behaviour", mixinDelegate.second());
23+
}
24+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.cglib.proxy;
2+
3+
import net.sf.cglib.proxy.Enhancer;
4+
import net.sf.cglib.proxy.FixedValue;
5+
import net.sf.cglib.proxy.MethodInterceptor;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class ProxyTest {
11+
@Test
12+
public void givenPersonService_whenSayHello_thenReturnResult() {
13+
//given
14+
PersonService personService = new PersonService();
15+
16+
//when
17+
String res = personService.sayHello("Tom");
18+
19+
//then
20+
assertEquals(res, "Hello Tom");
21+
}
22+
23+
@Test
24+
public void givenEnhancerProxy_whenExtendPersonService_thenInterceptMethod() throws Exception {
25+
//given
26+
Enhancer enhancer = new Enhancer();
27+
enhancer.setSuperclass(PersonService.class);
28+
enhancer.setCallback((FixedValue) () -> "Hello Tom!");
29+
PersonService proxy = (PersonService) enhancer.create();
30+
31+
//when
32+
String res = proxy.sayHello(null);
33+
34+
//then
35+
assertEquals("Hello Tom!", res);
36+
}
37+
38+
@Test
39+
public void givenEnhancer_whenExecuteMethodOnProxy_thenInterceptOnlyStringReturnTypeMethod() throws Exception {
40+
//given
41+
Enhancer enhancer = new Enhancer();
42+
enhancer.setSuperclass(PersonService.class);
43+
enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
44+
if (method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) {
45+
return "Hello Tom!";
46+
} else {
47+
return proxy.invokeSuper(obj, args);
48+
}
49+
});
50+
51+
//when
52+
PersonService proxy = (PersonService) enhancer.create();
53+
54+
//then
55+
assertEquals("Hello Tom!", proxy.sayHello(null));
56+
int lengthOfName = proxy.lengthOfName("Mary");
57+
assertEquals(4, lengthOfName);
58+
}
59+
60+
}

0 commit comments

Comments
 (0)