chore: reorganize, add ProxyType enum

This commit is contained in:
zaaarf 2023-03-21 18:46:23 +01:00
parent 7e0b4e58dd
commit 17a0b4d017
No known key found for this signature in database
GPG key ID: 82240E075E31FA4C
10 changed files with 57 additions and 31 deletions

View file

@ -3,12 +3,16 @@ package ftbsc.lll.proxies;
import org.objectweb.asm.Type;
/**
* Abstract proxy class, implementing common aspects
* of {@link MethodProxy} and {@link FieldProxy}.
* Abstract proxy class, implementing common aspects.
* @since 0.3.0
*/
public abstract class AbstractProxy {
/**
* Which type of proxy this is.
*/
public final ProxyType proxyType;
/**
* The name of the corresponding element.
*/
@ -37,12 +41,14 @@ public abstract class AbstractProxy {
* @param descriptor the descriptor for the element
* @param modifiers the modifiers, as a packed int
* @param parent the FQN of the parent class
* @param proxyType the {@link ProxyType} being represented here
*/
protected AbstractProxy(String name, String descriptor, int modifiers, QualifiableProxy parent) {
protected AbstractProxy(String name, String descriptor, int modifiers, QualifiableProxy parent, ProxyType proxyType) {
this.name = name;
this.descriptor = descriptor;
this.modifiers = modifiers;
this.parent = parent;
this.proxyType = proxyType;
}
/**

View file

@ -0,0 +1,11 @@
package ftbsc.lll.proxies;
/**
* An enum listing the various proxies.
*/
public enum ProxyType {
FIELD,
METHOD,
TYPE,
PACKAGE
}

View file

@ -1,11 +1,7 @@
package ftbsc.lll.proxies;
import org.objectweb.asm.Type;
/**
* A container for information about an element which has a fully-qualified name.
* @see TypeProxy
* @see PackageProxy
* A proxy for elements who have a fully-qualified name.
* @since 0.4.0
*/
public abstract class QualifiableProxy extends AbstractProxy {
@ -26,9 +22,10 @@ public abstract class QualifiableProxy extends AbstractProxy {
* @param modifiers the modifiers, as a packed int
* @param parent the {@link QualifiableProxy} representing the parent of this element
* @param fullyQualifiedName the FQN of the element
* @param proxyType the {@link ProxyType} being represented here
*/
protected QualifiableProxy(String descriptor, int modifiers, QualifiableProxy parent, String fullyQualifiedName) {
super(extractSimpleNameFromFQN(fullyQualifiedName), descriptor, modifiers, parent);
protected QualifiableProxy(String descriptor, int modifiers, QualifiableProxy parent, String fullyQualifiedName, ProxyType proxyType) {
super(extractSimpleNameFromFQN(fullyQualifiedName), descriptor, modifiers, parent, proxyType);
this.fullyQualifiedName = fullyQualifiedName;
this.internalName = this.fullyQualifiedName.replace('.', '/');
}

View file

@ -1,5 +1,8 @@
package ftbsc.lll.proxies;
package ftbsc.lll.proxies.impl;
import ftbsc.lll.proxies.AbstractProxy;
import ftbsc.lll.proxies.ProxyType;
import ftbsc.lll.proxies.QualifiableProxy;
import org.objectweb.asm.Type;
import java.lang.reflect.Field;
@ -10,15 +13,6 @@ import java.lang.reflect.Field;
* @since 0.3.0
*/
public class FieldProxy extends AbstractProxy {
/**
* A public constructor, builds a proxy from a {@link Field}
* obtained from reflection.
* @param f the {@link Field} object corresponding to this.
*/
public FieldProxy(Field f) {
super(f.getName(), Type.getDescriptor(f.getType()), f.getModifiers(), TypeProxy.from(f.getDeclaringClass()));
}
/**
* Protected constructor, called only from the builder.
* @param name the name of the field
@ -27,7 +21,16 @@ public class FieldProxy extends AbstractProxy {
* @param parent the {@link QualifiableProxy} for the parent
*/
protected FieldProxy(String name, String descriptor, int modifiers, QualifiableProxy parent) {
super(name, descriptor, modifiers, parent);
super(name, descriptor, modifiers, parent, ProxyType.FIELD);
}
/**
* A public constructor, builds a proxy from a {@link Field}
* obtained from reflection.
* @param f the {@link Field} object corresponding to this.
*/
public FieldProxy(Field f) {
this(f.getName(), Type.getDescriptor(f.getType()), f.getModifiers(), TypeProxy.from(f.getDeclaringClass()));
}
/**

View file

@ -1,5 +1,8 @@
package ftbsc.lll.proxies;
package ftbsc.lll.proxies.impl;
import ftbsc.lll.proxies.AbstractProxy;
import ftbsc.lll.proxies.ProxyType;
import ftbsc.lll.proxies.QualifiableProxy;
import org.objectweb.asm.Type;
import java.lang.reflect.Method;
@ -35,7 +38,7 @@ public class MethodProxy extends AbstractProxy {
* @param returnType the return type of the method
*/
protected MethodProxy(String name, int modifiers, QualifiableProxy parent, Type[] parameters, Type returnType) {
super(name, Type.getMethodDescriptor(returnType, parameters), modifiers, parent);
super(name, Type.getMethodDescriptor(returnType, parameters), modifiers, parent, ProxyType.METHOD);
this.parameters = Arrays.stream(parameters)
.map(t -> TypeProxy.from(t, 0))
.toArray(TypeProxy[]::new);

View file

@ -1,4 +1,7 @@
package ftbsc.lll.proxies;
package ftbsc.lll.proxies.impl;
import ftbsc.lll.proxies.ProxyType;
import ftbsc.lll.proxies.QualifiableProxy;
/**
* A container for information about a package.
@ -17,7 +20,7 @@ public class PackageProxy extends QualifiableProxy {
* @param fqn the fully-qualified name of this package
*/
protected PackageProxy(PackageProxy parent, String fqn) {
super(null, 0, parent, fqn);
super(null, 0, parent, fqn, ProxyType.PACKAGE);
}
/**

View file

@ -1,5 +1,8 @@
package ftbsc.lll.proxies;
package ftbsc.lll.proxies.impl;
import ftbsc.lll.proxies.AbstractProxy;
import ftbsc.lll.proxies.ProxyType;
import ftbsc.lll.proxies.QualifiableProxy;
import org.objectweb.asm.Type;
import java.lang.reflect.Modifier;
@ -26,7 +29,7 @@ public class TypeProxy extends QualifiableProxy {
* @param primitive whether the proxy is a primitive
*/
protected TypeProxy(String name, String descriptor, int modifiers, String parent, boolean primitive) {
super(descriptor, modifiers, PackageProxy.from(parent), String.format("%s.%s", name, parent));
super(descriptor, modifiers, PackageProxy.from(parent), String.format("%s.%s", name, parent), ProxyType.TYPE);
this.primitive = primitive;
}
@ -39,7 +42,7 @@ public class TypeProxy extends QualifiableProxy {
* @param containerClass the FQN of the parent class of the class
*/
protected TypeProxy(String name, String descriptor, int modifiers, QualifiableProxy containerClass, boolean primitive) {
super(descriptor, modifiers, containerClass, String.format("%s$%s", name, containerClass.fullyQualifiedName));
super(descriptor, modifiers, containerClass, String.format("%s$%s", name, containerClass.fullyQualifiedName), ProxyType.TYPE);
this.primitive = primitive;
}

View file

@ -1,6 +1,6 @@
package ftbsc.lll.tools.nodes;
import ftbsc.lll.proxies.FieldProxy;
import ftbsc.lll.proxies.impl.FieldProxy;
import org.objectweb.asm.tree.FieldInsnNode;
/**

View file

@ -1,6 +1,6 @@
package ftbsc.lll.tools.nodes;
import ftbsc.lll.proxies.MethodProxy;
import ftbsc.lll.proxies.impl.MethodProxy;
import org.objectweb.asm.tree.MethodInsnNode;
/**

View file

@ -1,6 +1,6 @@
package ftbsc.lll.tools.nodes;
import ftbsc.lll.proxies.TypeProxy;
import ftbsc.lll.proxies.impl.TypeProxy;
import org.objectweb.asm.tree.TypeInsnNode;
/**