Created
March 20, 2018 15:23
-
-
Save Brutt/e061a8c3a464fcebc72c738429278b09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
/** | |
* Created by Red8 on 20/03/2018. | |
*/ | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface Inject { | |
Class clazz(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface InjectRandomInt { | |
int min() default 0; | |
int max(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class MessagePrinter { | |
private String message; | |
@InjectRandomInt(min = 2, max = 5) | |
private int count; | |
@Inject(clazz = ArrayList.class) | |
List suffix; | |
@Run | |
public void print() { | |
for (int i = 0; i < 2; i++) { | |
System.out.println(message + suffix); | |
} | |
} | |
public String getMessage() { | |
return message; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
public int getCount() { | |
return count; | |
} | |
public void setCount(int count) { | |
this.count = count; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Метод принимает object и вызывает у него все методы без параметров// | |
//Метод принимает object и выводит на экран все сигнатуры методов в ко//торых есть final | |
//Метод принимает Class и выводит всех предков класса и все интерфейсы которое класс имплементирует | |
//Метод принимает объект и меняет всего его поля на их нулевые значение (null, 0, false etc)+ | |
//Метод принимает объект и вызывает у него все методы которые начинаются на run. | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
class Test2 { | |
public void getAllMethods(Object object) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { | |
Class classObject = object.getClass(); | |
Method[] methods = classObject.getMethods(); | |
for (Method method : methods) { | |
if (method.toString().contains("()")){ | |
String methodName = method.getName(); | |
Method curMethod = classObject.getMethod(methodName); | |
curMethod.invoke(object); | |
System.out.println(method.getName()); | |
} | |
} | |
} | |
public static void main(String[] args) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { | |
/*String[] names = new String[10]; | |
for (int i = 0; i < names.length; i++) { | |
names[i] = "Tolik"; | |
} | |
for (int i = 0; i < names.length; i++) { | |
System.out.println(names[i]); | |
}*/ | |
String str = "123"; | |
Test2 t2 = new Test2(); | |
t2.getAllMethods(str); | |
} | |
static void reflection() throws IllegalAccessException, NoSuchMethodException { | |
String name = "Tolik"; | |
Class clazz = name.getClass(); | |
Constructor constructor = clazz.getConstructor(char[].class, int.class, int.class); | |
//clazz = String.class; | |
Field[] fields = clazz.getDeclaredFields(); | |
for (Field field : fields) { | |
if (field.getName().equals("value")) { | |
char[] newArray = "Tolik mudak".toCharArray(); | |
field.setAccessible(true); | |
field.set(name, newArray); | |
} | |
} | |
System.out.println(name); | |
} | |
} | |
// equals | |
// hashCode | |
// toString | |
// clone | |
// finalize | |
// getClass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.List; | |
import java.util.Random; | |
public class Test3 { | |
//Принимает объект и вызывает методы проанотированные аннотацией @Run (аннотация Ваша, написать самим) | |
public void getAllMethodsWithRunAnnotation(Object object) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { | |
Class classObject = object.getClass(); | |
Method[] methods = classObject.getMethods(); | |
for (Method method : methods) { | |
if (method.getAnnotation(Run.class) != null){ | |
String methodName = method.getName(); | |
Method curMethod = classObject.getMethod(methodName); | |
curMethod.invoke(object); | |
System.out.println(method.getName()); | |
} | |
} | |
} | |
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { | |
MessagePrinter messagePrinter = new MessagePrinter(); | |
messagePrinter.setMessage("Hello hava"); | |
Test3 t3 = new Test3(); | |
t3.getAllMethodsWithRunAnnotation(messagePrinter); | |
//injectRandomIntValues(messagePrinter); | |
//messagePrinter.print(); | |
} | |
public static void getInjects(Object value) throws IllegalAccessException, InstantiationException { | |
Class clazz = value.getClass(); | |
for (Field field : clazz.getDeclaredFields()) { | |
Inject annotation = | |
field.getAnnotation(Inject.class); | |
if (annotation != null) { | |
field.setAccessible(true); | |
field.set(value, annotation.clazz() == void.class ? field.getType().newInstance() : annotation.clazz().newInstance()); | |
field.setAccessible(false); | |
} | |
} | |
} | |
public static void injectRandomIntValues(Object value) throws IllegalAccessException { | |
Class clazz = value.getClass(); | |
Random random = new Random(); | |
for (Field field : clazz.getDeclaredFields()) { | |
InjectRandomInt annotation = | |
field.getAnnotation(InjectRandomInt.class); | |
if (annotation != null) { | |
int min = annotation.min(); | |
int max = annotation.max(); | |
int randomValue = min + random.nextInt(max - min + 1); | |
field.setAccessible(true); | |
field.set(value, randomValue); | |
field.setAccessible(false); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment