renamed stuff and added generic reflection WIP

This commit is contained in:
Sander Hautvast 2023-07-27 14:22:09 +02:00
parent 927611d44b
commit 7bc4b96fd7
36 changed files with 198 additions and 46 deletions

View file

@ -2,7 +2,7 @@
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>nl.sander</groupId>
<groupId>com.github.shautvast</groupId>
<artifactId>reflective</artifactId>
<version>1.0.0</version>
<name>reflective</name>

View file

@ -0,0 +1,6 @@
package com.github.shautvast.reflective;
public abstract class AbstractMetaClassFactory {
public abstract MetaClass create();
}

View 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));
}
}

View file

@ -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;
}
}

View file

@ -0,0 +1,10 @@
package com.github.shautvast.reflective;
public class MetaField {
private final String name;
public MetaField(String name) {
this.name = name;
}
}

View file

@ -0,0 +1,9 @@
package com.github.shautvast.reflective;
public class MetaMethod {
private final String name;
public MetaMethod(String methodname) {
this.name = methodname;
}
}

View file

@ -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);
}
}
}

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
public abstract class AbstractComparator {

View file

@ -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.MethodVisitor;
import org.objectweb.asm.tree.*;
@ -14,7 +14,6 @@ class ComparatorFactory extends ClassVisitor {
public static final String SUPER = Java.internalName(AbstractComparator.class);
private boolean isRecord = false;
public ComparatorFactory() {

View file

@ -1,8 +1,8 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import nl.sander.reflective.java.ByteClassLoader;
import nl.sander.reflective.java.Java;
import nl.sander.reflective.tomap.ToMap;
import com.github.shautvast.reflective.java.ByteClassLoader;
import com.github.shautvast.reflective.java.Java;
import com.github.shautvast.reflective.tomap.ToMap;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import java.util.Arrays;
import java.util.List;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.java;
package com.github.shautvast.reflective.java;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.java;
package com.github.shautvast.reflective.java;
import org.objectweb.asm.ClassReader;
@ -16,9 +16,15 @@ public class Java {
public static String internalName(String className) {
return className.replaceAll("\\.", "/");
}
public static String internalName(Class<?> type) {
return internalName(type.getName());
}
public static String externalName(String internalName){
return internalName.replaceAll("/",".");
}
public static boolean hasArgs(String desc) {
return desc.charAt(1) != ')';
}
@ -40,12 +46,14 @@ public class Java {
}
public static ClassReader getClassReader(Object value) throws ClassNotFoundException {
ClassReader cr = null;
try {
cr = new ClassReader(value.getClass().getName());
} catch (IOException e) {
throw new ClassNotFoundException(value.getClass().getName());
return getClassReader(value.getClass());
}
public static ClassReader getClassReader(Class<?> type) throws ClassNotFoundException {
try {
return new ClassReader(type.getName());
} catch (IOException e) {
throw new ClassNotFoundException(type.getName());
}
return cr;
}
}

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.tomap;
package com.github.shautvast.reflective.tomap;
import java.util.HashMap;
import java.util.Map;

View file

@ -1,8 +1,8 @@
package nl.sander.reflective.tomap;
package com.github.shautvast.reflective.tomap;
import nl.sander.reflective.java.ByteClassLoader;
import nl.sander.reflective.java.Java;
import com.github.shautvast.reflective.java.ByteClassLoader;
import com.github.shautvast.reflective.java.Java;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;

View file

@ -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.MethodVisitor;
import org.objectweb.asm.tree.*;

View file

@ -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());
}
}

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import org.junit.jupiter.api.Test;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import org.junit.jupiter.api.Test;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import org.junit.jupiter.api.Test;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import org.junit.jupiter.api.Test;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import org.junit.jupiter.api.Test;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import org.junit.jupiter.api.Test;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import org.junit.jupiter.api.Test;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import org.junit.jupiter.api.Test;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import org.junit.jupiter.api.Test;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import org.junit.jupiter.api.Test;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import java.util.List;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
public class Shop {
private final String name;

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import org.junit.jupiter.api.Test;

View file

@ -0,0 +1,6 @@
package com.github.shautvast.reflective.compare;
public enum Storage {
HIGH,
LOW
}

View file

@ -1,4 +1,4 @@
package nl.sander.reflective.compare;
package com.github.shautvast.reflective.compare;
import org.junit.jupiter.api.Test;

View file

@ -1,8 +1,8 @@
package nl.sander.reflective.tomap;
package com.github.shautvast.reflective.tomap;
import nl.sander.reflective.compare.PlumBean;
import nl.sander.reflective.compare.Shop;
import nl.sander.reflective.compare.Storage;
import com.github.shautvast.reflective.compare.Shop;
import com.github.shautvast.reflective.compare.Storage;
import com.github.shautvast.reflective.compare.PlumBean;
import org.junit.jupiter.api.Test;
import java.util.List;

View file

@ -1,6 +0,0 @@
package nl.sander.reflective.compare;
public enum Storage {
HIGH,
LOW
}