Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions hw05-aop/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id 'java'
}

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

dependencies {
}
14 changes: 14 additions & 0 deletions hw05-aop/src/main/java/ru/otus/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.otus;

import ru.otus.app.CalculateImpl;
import ru.otus.app.Ioc;

public class Main {
public static void main(String[] args) {
var calculate = Ioc.createCalculateLogImpl(new CalculateImpl());
calculate.calculation(1323);



}
}
6 changes: 6 additions & 0 deletions hw05-aop/src/main/java/ru/otus/app/Calculate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ru.otus.app;


public interface Calculate {
void calculation(int number);
}
9 changes: 9 additions & 0 deletions hw05-aop/src/main/java/ru/otus/app/CalculateImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.otus.app;

public class CalculateImpl implements Calculate {
@Log
@Override
public void calculation(int number) {
System.out.println(number + number);
}
}
52 changes: 52 additions & 0 deletions hw05-aop/src/main/java/ru/otus/app/Ioc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ru.otus.app;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Ioc {
private Ioc() {}

public static Calculate createCalculateLogImpl(Calculate calculate){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Решение должно быть универсальное, а не заточенное под конкретный класс. Иначе теряется весь смысл с таким прокси, проще метод в конкретном классе сделать

InvocationHandler invocationHandler = new MyHandler(calculate);
return (Calculate) Proxy.newProxyInstance(Ioc.class.getClassLoader(), new Class<?>[]{Calculate.class}, invocationHandler);

}
private static class MyHandler implements InvocationHandler {
private final Calculate calculate;
private final List<Method> methodsWithAnnotation;

MyHandler(Calculate calculate) {
this.calculate = calculate;
List<Method> methodsWithAnnotationCalculate = new ArrayList<>();

for (Method method : calculate.getClass().getDeclaredMethods()) {
for (Annotation annotation : method.getDeclaredAnnotations()) {
if (annotation.annotationType() == Log.class) {
methodsWithAnnotationCalculate.add(method);
}
}
}
this.methodsWithAnnotation = methodsWithAnnotationCalculate;
}


@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//проверяем по названию и по массиву аннотаций
for (Method annotatedMethod: methodsWithAnnotation) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отличное по скорости решение.

if (annotatedMethod.getName().equals(method.getName()) &&
Arrays.toString(annotatedMethod.getParameterTypes()).equals(Arrays.toString(method.getParameterTypes()))) {
System.out.println("выполняем метод " + method.getName() + ", параметры " + Arrays.toString(args));
break;
}
}
return method.invoke(calculate, args);
}

}
}
12 changes: 12 additions & 0 deletions hw05-aop/src/main/java/ru/otus/app/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

package ru.otus.app;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Log {
}