commit
41fff7c0d3
|
@ -0,0 +1,47 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>cglib</artifactId>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
|
||||
<dependency>
|
||||
<groupId>cglib</groupId>
|
||||
<artifactId>cglib</artifactId>
|
||||
<version>${cglib.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<cglib.version>3.2.4</cglib.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
</properties>
|
||||
|
||||
|
||||
</project>
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.cglib.mixin;
|
||||
|
||||
public class Class1 implements Interface1 {
|
||||
@Override
|
||||
public String first() {
|
||||
return "first behaviour";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.cglib.mixin;
|
||||
|
||||
public class Class2 implements Interface2 {
|
||||
@Override
|
||||
public String second() {
|
||||
return "second behaviour";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.cglib.mixin;
|
||||
|
||||
public interface Interface1 {
|
||||
String first();
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.cglib.mixin;
|
||||
|
||||
public interface Interface2 {
|
||||
String second();
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.baeldung.cglib.mixin;
|
||||
|
||||
public interface MixinInterface extends Interface1, Interface2 {
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.cglib.proxy;
|
||||
|
||||
public class PersonService {
|
||||
public String sayHello(String name) {
|
||||
return "Hello " + name;
|
||||
}
|
||||
|
||||
public Integer lengthOfName(String name) {
|
||||
return name.length();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.cglib.proxy;
|
||||
|
||||
|
||||
import net.sf.cglib.beans.BeanGenerator;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class BeanGeneratorTest {
|
||||
|
||||
@Test
|
||||
public void givenBeanCreator_whenAddProperty_thenClassShouldHaveFieldValue() throws Exception {
|
||||
//given
|
||||
BeanGenerator beanGenerator = new BeanGenerator();
|
||||
|
||||
//when
|
||||
beanGenerator.addProperty("name", String.class);
|
||||
Object myBean = beanGenerator.create();
|
||||
Method setter = myBean
|
||||
.getClass()
|
||||
.getMethod("setName", String.class);
|
||||
setter.invoke(myBean, "some string value set by a cglib");
|
||||
|
||||
//then
|
||||
Method getter = myBean
|
||||
.getClass()
|
||||
.getMethod("getName");
|
||||
assertEquals("some string value set by a cglib", getter.invoke(myBean));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.cglib.proxy;
|
||||
|
||||
import com.baeldung.cglib.mixin.*;
|
||||
import net.sf.cglib.proxy.Mixin;
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class MixinTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoClasses_whenMixedIntoOne_thenMixinShouldHaveMethodsFromBothClasses() throws Exception {
|
||||
//when
|
||||
Mixin mixin = Mixin.create(
|
||||
new Class[]{Interface1.class, Interface2.class, MixinInterface.class},
|
||||
new Object[]{new Class1(), new Class2()}
|
||||
);
|
||||
MixinInterface mixinDelegate = (MixinInterface) mixin;
|
||||
|
||||
//then
|
||||
assertEquals("first behaviour", mixinDelegate.first());
|
||||
assertEquals("second behaviour", mixinDelegate.second());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.cglib.proxy;
|
||||
|
||||
import net.sf.cglib.proxy.Enhancer;
|
||||
import net.sf.cglib.proxy.FixedValue;
|
||||
import net.sf.cglib.proxy.MethodInterceptor;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ProxyTest {
|
||||
@Test
|
||||
public void givenPersonService_whenSayHello_thenReturnResult() {
|
||||
//given
|
||||
PersonService personService = new PersonService();
|
||||
|
||||
//when
|
||||
String res = personService.sayHello("Tom");
|
||||
|
||||
//then
|
||||
assertEquals(res, "Hello Tom");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEnhancerProxy_whenExtendPersonService_thenInterceptMethod() throws Exception {
|
||||
//given
|
||||
Enhancer enhancer = new Enhancer();
|
||||
enhancer.setSuperclass(PersonService.class);
|
||||
enhancer.setCallback((FixedValue) () -> "Hello Tom!");
|
||||
PersonService proxy = (PersonService) enhancer.create();
|
||||
|
||||
//when
|
||||
String res = proxy.sayHello(null);
|
||||
|
||||
//then
|
||||
assertEquals("Hello Tom!", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEnhancer_whenExecuteMethodOnProxy_thenInterceptOnlyStringReturnTypeMethod() throws Exception {
|
||||
//given
|
||||
Enhancer enhancer = new Enhancer();
|
||||
enhancer.setSuperclass(PersonService.class);
|
||||
enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
|
||||
if (method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) {
|
||||
return "Hello Tom!";
|
||||
} else {
|
||||
return proxy.invokeSuper(obj, args);
|
||||
}
|
||||
});
|
||||
|
||||
//when
|
||||
PersonService proxy = (PersonService) enhancer.create();
|
||||
|
||||
//then
|
||||
assertEquals("Hello Tom!", proxy.sayHello(null));
|
||||
int lengthOfName = proxy.lengthOfName("Mary");
|
||||
assertEquals(4, lengthOfName);
|
||||
}
|
||||
|
||||
}
|
1
pom.xml
1
pom.xml
|
@ -28,6 +28,7 @@
|
|||
<module>autovalue</module>
|
||||
|
||||
<module>cdi</module>
|
||||
<module>cglib</module>
|
||||
<!-- <module>core-java-9</module> -->
|
||||
<module>core-java</module>
|
||||
<module>couchbase-sdk</module>
|
||||
|
|
|
@ -116,7 +116,13 @@
|
|||
<groupId>com.maxmind.geoip2</groupId>
|
||||
<artifactId>geoip2</artifactId>
|
||||
<version>${geoip2.version}</version>
|
||||
</dependency>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
Loading…
Reference in New Issue