renamed stuff and added generic reflection WIP
This commit is contained in:
parent
927611d44b
commit
7bc4b96fd7
36 changed files with 198 additions and 46 deletions
2
pom.xml
2
pom.xml
|
|
@ -2,7 +2,7 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>nl.sander</groupId>
|
<groupId>com.github.shautvast</groupId>
|
||||||
<artifactId>reflective</artifactId>
|
<artifactId>reflective</artifactId>
|
||||||
<version>1.0.0</version>
|
<version>1.0.0</version>
|
||||||
<name>reflective</name>
|
<name>reflective</name>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
package com.github.shautvast.reflective;
|
||||||
|
|
||||||
|
public abstract class AbstractMetaClassFactory {
|
||||||
|
|
||||||
|
public abstract MetaClass create();
|
||||||
|
}
|
||||||
36
src/main/java/com/github/shautvast/reflective/MetaClass.java
Normal file
36
src/main/java/com/github/shautvast/reflective/MetaClass.java
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.github.shautvast.reflective;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class MetaClass {
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
private final Map<String, MetaField> fields;
|
||||||
|
private final Map<String, MetaMethod> methods;
|
||||||
|
|
||||||
|
public MetaClass(String name) {
|
||||||
|
this.name = name;
|
||||||
|
this.fields = new HashMap<>();
|
||||||
|
this.methods = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<MetaField> getFields() {
|
||||||
|
return Set.copyOf(fields.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<MetaMethod> getMethods() {
|
||||||
|
return Set.copyOf(methods.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
void addField(int access, String name, String descriptor) {
|
||||||
|
fields.put(name, new MetaField(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addMethod(int access, String methodname, String descriptor) {
|
||||||
|
methods.put(methodname, new MetaMethod(methodname));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
package com.github.shautvast.reflective;
|
||||||
|
|
||||||
|
import com.github.shautvast.reflective.compare.AbstractComparator;
|
||||||
|
import com.github.shautvast.reflective.java.Java;
|
||||||
|
import org.objectweb.asm.ClassVisitor;
|
||||||
|
import org.objectweb.asm.FieldVisitor;
|
||||||
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
|
|
||||||
|
import static org.objectweb.asm.Opcodes.ASM9;
|
||||||
|
|
||||||
|
public class MetaClassFactory extends ClassVisitor {
|
||||||
|
public static final String SUPER = Java.internalName(AbstractComparator.class);
|
||||||
|
|
||||||
|
private boolean isRecord = false;
|
||||||
|
|
||||||
|
final ClassNode classNode = new ClassNode();
|
||||||
|
|
||||||
|
private MetaClass metaClassToBuild;
|
||||||
|
|
||||||
|
public MetaClassFactory() {
|
||||||
|
super(ASM9);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
|
||||||
|
metaClassToBuild = new MetaClass(Java.externalName(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
|
||||||
|
metaClassToBuild.addField(access, name, descriptor);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MethodVisitor visitMethod(int access, String methodname,
|
||||||
|
String descriptor, String signature, String[] exceptions) {
|
||||||
|
metaClassToBuild.addMethod(access, methodname, descriptor);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MetaClass getMetaClass() {
|
||||||
|
return metaClassToBuild;
|
||||||
|
}
|
||||||
|
}
|
||||||
10
src/main/java/com/github/shautvast/reflective/MetaField.java
Normal file
10
src/main/java/com/github/shautvast/reflective/MetaField.java
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.github.shautvast.reflective;
|
||||||
|
|
||||||
|
public class MetaField {
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public MetaField(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.github.shautvast.reflective;
|
||||||
|
|
||||||
|
public class MetaMethod {
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public MetaMethod(String methodname) {
|
||||||
|
this.name = methodname;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.github.shautvast.reflective;
|
||||||
|
|
||||||
|
import com.github.shautvast.reflective.java.Java;
|
||||||
|
import org.objectweb.asm.ClassReader;
|
||||||
|
|
||||||
|
public class Reflective {
|
||||||
|
|
||||||
|
public static MetaClass getMetaForClass(Class<?> type){
|
||||||
|
return getMetaClass(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MetaClass getMetaClass(Class<?> type) {
|
||||||
|
try {
|
||||||
|
ClassReader cr = Java.getClassReader(type);
|
||||||
|
|
||||||
|
MetaClassFactory factory = new MetaClassFactory();
|
||||||
|
cr.accept(factory, ClassReader.SKIP_FRAMES);
|
||||||
|
return factory.getMetaClass();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
public abstract class AbstractComparator {
|
public abstract class AbstractComparator {
|
||||||
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import nl.sander.reflective.java.Java;
|
import com.github.shautvast.reflective.java.Java;
|
||||||
import org.objectweb.asm.ClassVisitor;
|
import org.objectweb.asm.ClassVisitor;
|
||||||
import org.objectweb.asm.MethodVisitor;
|
import org.objectweb.asm.MethodVisitor;
|
||||||
import org.objectweb.asm.tree.*;
|
import org.objectweb.asm.tree.*;
|
||||||
|
|
@ -14,7 +14,6 @@ class ComparatorFactory extends ClassVisitor {
|
||||||
|
|
||||||
public static final String SUPER = Java.internalName(AbstractComparator.class);
|
public static final String SUPER = Java.internalName(AbstractComparator.class);
|
||||||
|
|
||||||
|
|
||||||
private boolean isRecord = false;
|
private boolean isRecord = false;
|
||||||
|
|
||||||
public ComparatorFactory() {
|
public ComparatorFactory() {
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import nl.sander.reflective.java.ByteClassLoader;
|
import com.github.shautvast.reflective.java.ByteClassLoader;
|
||||||
import nl.sander.reflective.java.Java;
|
import com.github.shautvast.reflective.java.Java;
|
||||||
import nl.sander.reflective.tomap.ToMap;
|
import com.github.shautvast.reflective.tomap.ToMap;
|
||||||
import org.objectweb.asm.ClassReader;
|
import org.objectweb.asm.ClassReader;
|
||||||
import org.objectweb.asm.ClassWriter;
|
import org.objectweb.asm.ClassWriter;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.java;
|
package com.github.shautvast.reflective.java;
|
||||||
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.java;
|
package com.github.shautvast.reflective.java;
|
||||||
|
|
||||||
import org.objectweb.asm.ClassReader;
|
import org.objectweb.asm.ClassReader;
|
||||||
|
|
||||||
|
|
@ -16,9 +16,15 @@ public class Java {
|
||||||
public static String internalName(String className) {
|
public static String internalName(String className) {
|
||||||
return className.replaceAll("\\.", "/");
|
return className.replaceAll("\\.", "/");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String internalName(Class<?> type) {
|
public static String internalName(Class<?> type) {
|
||||||
return internalName(type.getName());
|
return internalName(type.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String externalName(String internalName){
|
||||||
|
return internalName.replaceAll("/",".");
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean hasArgs(String desc) {
|
public static boolean hasArgs(String desc) {
|
||||||
return desc.charAt(1) != ')';
|
return desc.charAt(1) != ')';
|
||||||
}
|
}
|
||||||
|
|
@ -40,12 +46,14 @@ public class Java {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ClassReader getClassReader(Object value) throws ClassNotFoundException {
|
public static ClassReader getClassReader(Object value) throws ClassNotFoundException {
|
||||||
ClassReader cr = null;
|
return getClassReader(value.getClass());
|
||||||
try {
|
}
|
||||||
cr = new ClassReader(value.getClass().getName());
|
|
||||||
} catch (IOException e) {
|
public static ClassReader getClassReader(Class<?> type) throws ClassNotFoundException {
|
||||||
throw new ClassNotFoundException(value.getClass().getName());
|
try {
|
||||||
|
return new ClassReader(type.getName());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ClassNotFoundException(type.getName());
|
||||||
}
|
}
|
||||||
return cr;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.tomap;
|
package com.github.shautvast.reflective.tomap;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
package nl.sander.reflective.tomap;
|
package com.github.shautvast.reflective.tomap;
|
||||||
|
|
||||||
|
|
||||||
import nl.sander.reflective.java.ByteClassLoader;
|
import com.github.shautvast.reflective.java.ByteClassLoader;
|
||||||
import nl.sander.reflective.java.Java;
|
import com.github.shautvast.reflective.java.Java;
|
||||||
import org.objectweb.asm.ClassReader;
|
import org.objectweb.asm.ClassReader;
|
||||||
import org.objectweb.asm.ClassWriter;
|
import org.objectweb.asm.ClassWriter;
|
||||||
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package nl.sander.reflective.tomap;
|
package com.github.shautvast.reflective.tomap;
|
||||||
|
|
||||||
import nl.sander.reflective.java.Java;
|
import com.github.shautvast.reflective.java.Java;
|
||||||
import org.objectweb.asm.ClassVisitor;
|
import org.objectweb.asm.ClassVisitor;
|
||||||
import org.objectweb.asm.MethodVisitor;
|
import org.objectweb.asm.MethodVisitor;
|
||||||
import org.objectweb.asm.tree.*;
|
import org.objectweb.asm.tree.*;
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.github.shautvast.reflective;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class ReflectiveTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test(){
|
||||||
|
assertEquals("java.lang.String",Reflective.getMetaForClass(String.class).getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
public class Shop {
|
public class Shop {
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
|
public enum Storage {
|
||||||
|
HIGH,
|
||||||
|
LOW
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package nl.sander.reflective.compare;
|
package com.github.shautvast.reflective.compare;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
package nl.sander.reflective.tomap;
|
package com.github.shautvast.reflective.tomap;
|
||||||
|
|
||||||
import nl.sander.reflective.compare.PlumBean;
|
import com.github.shautvast.reflective.compare.Shop;
|
||||||
import nl.sander.reflective.compare.Shop;
|
import com.github.shautvast.reflective.compare.Storage;
|
||||||
import nl.sander.reflective.compare.Storage;
|
import com.github.shautvast.reflective.compare.PlumBean;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
package nl.sander.reflective.compare;
|
|
||||||
|
|
||||||
public enum Storage {
|
|
||||||
HIGH,
|
|
||||||
LOW
|
|
||||||
}
|
|
||||||
Loading…
Add table
Reference in a new issue