moved files to core-java-modules/core-java-reflection
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.dynamicproxy;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class DynamicInvocationHandler implements InvocationHandler {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(DynamicInvocationHandler.class);
|
||||
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
LOGGER.info("Invoked method: {}", method.getName());
|
||||
|
||||
return 42;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.dynamicproxy;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TimingDynamicInvocationHandler implements InvocationHandler {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(TimingDynamicInvocationHandler.class);
|
||||
private final Map<String, Method> methods = new HashMap<>();
|
||||
|
||||
private Object target;
|
||||
|
||||
TimingDynamicInvocationHandler(Object target) {
|
||||
this.target = target;
|
||||
|
||||
for(Method method: target.getClass().getDeclaredMethods()) {
|
||||
this.methods.put(method.getName(), method);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
long start = System.nanoTime();
|
||||
Object result = methods.get(method.getName()).invoke(target, args);
|
||||
long elapsed = System.nanoTime() - start;
|
||||
|
||||
LOGGER.info("Executing {} finished in {} ns", method.getName(), elapsed);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.java.reflection;
|
||||
|
||||
public abstract class Animal implements Eating {
|
||||
|
||||
public static final String CATEGORY = "domestic";
|
||||
|
||||
private String name;
|
||||
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
protected abstract String getSound();
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.java.reflection;
|
||||
|
||||
public class Bird extends Animal {
|
||||
private boolean walks;
|
||||
|
||||
public Bird() {
|
||||
super("bird");
|
||||
}
|
||||
|
||||
public Bird(String name, boolean walks) {
|
||||
super(name);
|
||||
setWalks(walks);
|
||||
}
|
||||
|
||||
public Bird(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String eats() {
|
||||
return "grains";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSound() {
|
||||
return "chaps";
|
||||
}
|
||||
|
||||
public boolean walks() {
|
||||
return walks;
|
||||
}
|
||||
|
||||
public void setWalks(boolean walks) {
|
||||
this.walks = walks;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.java.reflection;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
public class DynamicGreeter implements Greeter {
|
||||
|
||||
private String greet;
|
||||
|
||||
public DynamicGreeter(String greet) {
|
||||
this.greet = greet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends Annotation> annotationType() {
|
||||
return DynamicGreeter.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String greet() {
|
||||
return greet;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.java.reflection;
|
||||
|
||||
public interface Eating {
|
||||
String eats();
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.java.reflection;
|
||||
|
||||
public class Goat extends Animal implements Locomotion {
|
||||
|
||||
public Goat(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSound() {
|
||||
return "bleat";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocomotion() {
|
||||
return "walks";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String eats() {
|
||||
return "grass";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.java.reflection;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Greeter {
|
||||
|
||||
public String greet() default "";
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.baeldung.java.reflection;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
public class GreetingAnnotation {
|
||||
|
||||
private static final String ANNOTATION_METHOD = "annotationData";
|
||||
private static final String ANNOTATION_FIELDS = "declaredAnnotations";
|
||||
private static final String ANNOTATIONS = "annotations";
|
||||
|
||||
public static void main(String ...args) {
|
||||
Greeter greetings = Greetings.class.getAnnotation(Greeter.class);
|
||||
System.err.println("Hello there, " + greetings.greet() + " !!");
|
||||
|
||||
Greeter targetValue = new DynamicGreeter("Good evening");
|
||||
//alterAnnotationValueJDK8(Greetings.class, Greeter.class, targetValue);
|
||||
alterAnnotationValueJDK7(Greetings.class, Greeter.class, targetValue);
|
||||
|
||||
greetings = Greetings.class.getAnnotation(Greeter.class);
|
||||
System.err.println("Hello there, " + greetings.greet() + " !!");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void alterAnnotationValueJDK8(Class<?> targetClass, Class<? extends Annotation> targetAnnotation, Annotation targetValue) {
|
||||
try {
|
||||
Method method = Class.class.getDeclaredMethod(ANNOTATION_METHOD, null);
|
||||
method.setAccessible(true);
|
||||
|
||||
Object annotationData = method.invoke(targetClass);
|
||||
|
||||
Field annotations = annotationData.getClass().getDeclaredField(ANNOTATIONS);
|
||||
annotations.setAccessible(true);
|
||||
|
||||
Map<Class<? extends Annotation>, Annotation> map = (Map<Class<? extends Annotation>, Annotation>) annotations.get(annotationData);
|
||||
map.put(targetAnnotation, targetValue);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void alterAnnotationValueJDK7(Class<?> targetClass, Class<? extends Annotation> targetAnnotation, Annotation targetValue) {
|
||||
try {
|
||||
Field annotations = Class.class.getDeclaredField(ANNOTATIONS);
|
||||
annotations.setAccessible(true);
|
||||
|
||||
Map<Class<? extends Annotation>, Annotation> map = (Map<Class<? extends Annotation>, Annotation>) annotations.get(targetClass);
|
||||
System.out.println(map);
|
||||
map.put(targetAnnotation, targetValue);
|
||||
System.out.println(map);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.baeldung.java.reflection;
|
||||
|
||||
@Greeter(greet="Good morning")
|
||||
public class Greetings {
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.java.reflection;
|
||||
|
||||
public interface Locomotion {
|
||||
String getLocomotion();
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.java.reflection;
|
||||
|
||||
public class Operations {
|
||||
|
||||
public double publicSum(int a, double b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public static double publicStaticMultiply(float a, long b) {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
private boolean privateAnd(boolean a, boolean b) {
|
||||
return a && b;
|
||||
}
|
||||
|
||||
protected int protectedMax(int a, int b) {
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.baeldung.java.reflection;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
}
|
||||
Reference in New Issue
Block a user