Skip to content

Commit 0b8de38

Browse files
Merge pull request #18829 from Deepak-Vohra/master
BAEL-9439 Update pom.xml
2 parents b2a3698 + 977c7d8 commit 0b8de38

File tree

12 files changed

+281
-1
lines changed

12 files changed

+281
-1
lines changed

core-java-modules/core-java-lang-oop-patterns-2/pom.xml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
<groupId>com.baeldung.core-java-modules</groupId>
1313
<version>0.0.1-SNAPSHOT</version>
1414
</parent>
15+
16+
<properties>
17+
<logback.configurationFileName>logback.xml</logback.configurationFileName>
18+
</properties>
1519

1620
<dependencies>
1721
<dependency>
@@ -24,6 +28,27 @@
2428
<artifactId>jackson-databind</artifactId>
2529
<version>${jackson.version}</version>
2630
</dependency>
31+
<dependency>
32+
<groupId>org.slf4j</groupId>
33+
<artifactId>slf4j-api</artifactId>
34+
<version>2.0.7</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>ch.qos.logback</groupId>
38+
<artifactId>logback-classic</artifactId>
39+
<version>1.4.11</version>
40+
<scope>runtime</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework</groupId>
44+
<artifactId>spring-aop</artifactId>
45+
<version>6.0.11</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework</groupId>
49+
<artifactId>spring-context</artifactId>
50+
<version>6.0.11</version>
51+
</dependency>
2752
</dependencies>
2853

29-
</project>
54+
</project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.overridemethod;
2+
3+
public interface Calculator {
4+
int add(int a, int b);
5+
int subtract(int a, int b);
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.overridemethod;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class SimpleCalculator implements Calculator {
7+
private static final Logger log = LoggerFactory.getLogger(SimpleCalculator.class);
8+
9+
@Override
10+
public int add(int a, int b) {
11+
log.info("SimpleCalculator: Adding {} and {}", a, b); // Use parameterized logging {}
12+
return a + b;
13+
}
14+
15+
@Override
16+
public int subtract(int a, int b) {
17+
log.info("SimpleCalculator: Subtracting {} from {}", b, a);
18+
return a - b;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.overridemethod.decorator;
2+
3+
import com.baeldung.overridemethod.Calculator;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class MeteredCalculatorDecorator implements Calculator {
8+
private final Calculator wrappedCalculator;
9+
private final Map<String, Integer> methodCalls;
10+
11+
public MeteredCalculatorDecorator(Calculator calculator) {
12+
this.wrappedCalculator = calculator;
13+
this.methodCalls = new HashMap<>();
14+
// Initialize counts for clarity
15+
methodCalls.put("add", 0);
16+
methodCalls.put("subtract", 0);
17+
}
18+
19+
@Override
20+
public int add(int a, int b) {
21+
// Track the call count
22+
methodCalls.merge("add", 1, Integer::sum);
23+
return wrappedCalculator.add(a, b); // Delegation
24+
}
25+
26+
@Override
27+
public int subtract(int a, int b) {
28+
// Track the call count
29+
methodCalls.merge("subtract", 1, Integer::sum);
30+
return wrappedCalculator.subtract(a, b); // Delegation
31+
}
32+
33+
// Public method to expose the call counts for testing
34+
public int getCallCount(String methodName) {
35+
return methodCalls.getOrDefault(methodName, 0);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.overridemethod.proxy.jdk;
2+
3+
import java.lang.reflect.InvocationHandler;
4+
import java.lang.reflect.Method;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
public class LoggingInvocationHandler implements InvocationHandler {
9+
private static final Logger log = LoggerFactory.getLogger(LoggingInvocationHandler.class);
10+
private final Object target;
11+
12+
public LoggingInvocationHandler(Object target) {
13+
this.target = target;
14+
}
15+
16+
@Override
17+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
18+
19+
log.debug("PROXY LOG: Intercepting method: {}", method.getName());
20+
21+
Object result = method.invoke(target, args);
22+
23+
log.debug("PROXY LOG: Method {} executed.", method.getName());
24+
25+
return result;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.overridemethod.proxy.spring;
2+
3+
import org.aopalliance.intercept.MethodInterceptor;
4+
import org.aopalliance.intercept.MethodInvocation;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
public class LoggingMethodInterceptor implements MethodInterceptor {
9+
private static final Logger log = LoggerFactory.getLogger(LoggingMethodInterceptor.class);
10+
11+
@Override
12+
public Object invoke(MethodInvocation invocation) throws Throwable {
13+
14+
log.debug("SPRING PROXY: Intercepting method: {}", invocation.getMethod().getName());
15+
16+
Object result = invocation.proceed();
17+
18+
log.debug("SPRING PROXY: Method {} completed.", invocation.getMethod().getName());
19+
20+
return result;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.overridemethod.subclass;
2+
3+
import com.baeldung.overridemethod.SimpleCalculator;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
public class LoggingCalculator extends SimpleCalculator {
8+
private static final Logger log = LoggerFactory.getLogger(LoggingCalculator.class);
9+
10+
@Override
11+
public int add(int a, int b) {
12+
log.debug("LOG: Before addition.");
13+
int result = super.add(a, b);
14+
log.debug("LOG: After addition. Result: {}", result);
15+
return result;
16+
}
17+
18+
@Override
19+
public int subtract(int a, int b) {
20+
log.debug("LOG: Before subtraction.");
21+
int result = super.subtract(a, b);
22+
log.debug("LOG: After subtraction. Result: {}", result);
23+
return result;
24+
}
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<root level="INFO">
10+
<appender-ref ref="STDOUT" />
11+
</root>
12+
13+
<logger name="com.baeldung.overridemethod" level="DEBUG" additivity="false">
14+
<appender-ref ref="STDOUT"/>
15+
</logger>
16+
17+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.overridemethod.decorator;
2+
3+
import com.baeldung.overridemethod.Calculator;
4+
import com.baeldung.overridemethod.SimpleCalculator;
5+
import org.junit.jupiter.api.Test;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
public class DecoratorPatternTest {
9+
10+
@Test
11+
void givenACalculator_whenUsingMeteredDecorator_thenMethodCallsAreCountedCorrectly() {
12+
// ARRANGE
13+
Calculator simpleCalc = new SimpleCalculator();
14+
15+
// Use the MeteredCalculatorDecorator decorator
16+
MeteredCalculatorDecorator decoratedCalc = new MeteredCalculatorDecorator(simpleCalc);
17+
18+
// ACT
19+
// Call add twice
20+
decoratedCalc.add(10, 5);
21+
decoratedCalc.add(2, 3);
22+
23+
// Call subtract once
24+
decoratedCalc.subtract(10, 5);
25+
26+
// ASSERT Core Functionality (optional, but good practice)
27+
assertEquals(15, decoratedCalc.add(10, 5), "Core functionality must still work.");
28+
29+
// ASSERT the call counts
30+
// 1. Assert 'add' was called 3 times (2 from ACT + 1 from ASSERT Core)
31+
assertEquals(3, decoratedCalc.getCallCount("add"), "The 'add' method should have been called 3 times.");
32+
33+
// 2. Assert 'subtract' was called 1 time
34+
assertEquals(1, decoratedCalc.getCallCount("subtract"), "The 'subtract' method should have been called 1 time.");
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.overridemethod.proxy.jdk;
2+
3+
import com.baeldung.overridemethod.Calculator;
4+
import com.baeldung.overridemethod.SimpleCalculator;
5+
import org.junit.jupiter.api.Test;
6+
import java.lang.reflect.Proxy;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class DynamicProxyTest {
10+
11+
@Test
12+
void givenACalculator_whenUsingJdkDynamicProxy_thenJdkDynamicProxyCanBeUsed() {
13+
Calculator simpleCalc = new SimpleCalculator();
14+
LoggingInvocationHandler handler = new LoggingInvocationHandler(simpleCalc);
15+
16+
Calculator proxyCalc = (Calculator) Proxy.newProxyInstance(
17+
Calculator.class.getClassLoader(),
18+
new Class<?>[] { Calculator.class },
19+
handler
20+
);
21+
22+
assertEquals(30, proxyCalc.add(20, 10));
23+
assertEquals(10, proxyCalc.subtract(20, 10));
24+
}
25+
}
26+

0 commit comments

Comments
 (0)