feat: restored flat descriptors, implemented primitive proxies

This commit is contained in:
zaaarf 2023-03-21 18:24:29 +01:00
parent 884cefead9
commit ff47129cf3
No known key found for this signature in database
GPG key ID: 82240E075E31FA4C
8 changed files with 126 additions and 74 deletions

View file

@ -15,9 +15,9 @@ public abstract class AbstractProxy {
public final String name; public final String name;
/** /**
* The {@link Type} corresponding to this element. * The descriptor for this element.
*/ */
public final Type type; public final String descriptor;
/** /**
* The fully qualified name (i.e. java.lang.String) of * The fully qualified name (i.e. java.lang.String) of
@ -34,13 +34,13 @@ public abstract class AbstractProxy {
/** /**
* The private constructor, should be called by all classes extending this in theirs. * The private constructor, should be called by all classes extending this in theirs.
* @param name the name of the element * @param name the name of the element
* @param type the {@link Type} for the element * @param descriptor the descriptor for the element
* @param modifiers the modifiers, as a packed int * @param modifiers the modifiers, as a packed int
* @param parent the FQN of the parent class * @param parent the FQN of the parent class
*/ */
protected AbstractProxy(String name, Type type, int modifiers, QualifiableProxy parent) { protected AbstractProxy(String name, String descriptor, int modifiers, QualifiableProxy parent) {
this.name = name; this.name = name;
this.type = type; this.descriptor = descriptor;
this.modifiers = modifiers; this.modifiers = modifiers;
this.parent = parent; this.parent = parent;
} }
@ -57,7 +57,7 @@ public abstract class AbstractProxy {
return p.parent.equals(this.parent) return p.parent.equals(this.parent)
&& p.name.equals(this.name) && p.name.equals(this.name)
&& p.modifiers == this.modifiers && p.modifiers == this.modifiers
&& p.type.equals(this.type); && p.descriptor.equals(this.descriptor);
} else return false; } else return false;
} }
@ -83,9 +83,9 @@ public abstract class AbstractProxy {
protected QualifiableProxy parent; protected QualifiableProxy parent;
/** /**
* The {@link Type} corresponding to the element. * The descriptor of the element.
*/ */
protected Type type; protected String descriptor;
/** /**
* The constructor. * The constructor.
@ -123,22 +123,22 @@ public abstract class AbstractProxy {
return this; return this;
} }
/**
* @param type the {@link Type} corresponding to the element
* @return the current state of the builder
*/
public Builder<T> setType(Type type) {
this.type = type;
return this;
}
/** /**
* Sets {@link Type} for this element from the descriptor, passed as a {@link String}. * Sets {@link Type} for this element from the descriptor, passed as a {@link String}.
* @param descriptor the descriptor passed as a {@link String} * @param descriptor the descriptor passed as a {@link String}
* @return the builder's state after the change * @return the builder's state after the change
*/ */
public Builder<T> setDescriptor(String descriptor) { public Builder<T> setDescriptor(String descriptor) {
return this.setType(Type.getType(descriptor)); this.descriptor = descriptor;
return this;
}
/**
* @param type the {@link Type} corresponding to the element
* @return the current state of the builder
*/
public Builder<T> setType(Type type) {
return this.setDescriptor(type.getDescriptor());
} }
/** /**

View file

@ -16,18 +16,18 @@ public class FieldProxy extends AbstractProxy {
* @param f the {@link Field} object corresponding to this. * @param f the {@link Field} object corresponding to this.
*/ */
public FieldProxy(Field f) { public FieldProxy(Field f) {
super(f.getName(), Type.getType(f.getType()), f.getModifiers(), ClassProxy.from(f.getDeclaringClass())); super(f.getName(), Type.getDescriptor(f.getType()), f.getModifiers(), TypeProxy.from(f.getDeclaringClass()));
} }
/** /**
* Protected constructor, called only from the builder. * Protected constructor, called only from the builder.
* @param name the name of the field * @param name the name of the field
* @param type the {@link Type} of the field * @param descriptor the descriptor of the field
* @param modifiers the modifiers of the field * @param modifiers the modifiers of the field
* @param parent the {@link QualifiableProxy} for the parent * @param parent the {@link QualifiableProxy} for the parent
*/ */
protected FieldProxy(String name, Type type, int modifiers, QualifiableProxy parent) { protected FieldProxy(String name, String descriptor, int modifiers, QualifiableProxy parent) {
super(name, type, modifiers, parent); super(name, descriptor, modifiers, parent);
} }
/** /**
@ -68,7 +68,7 @@ public class FieldProxy extends AbstractProxy {
* @return the builder's state after the change * @return the builder's state after the change
*/ */
public Builder setParent(String parentFQN, int modifiers) { public Builder setParent(String parentFQN, int modifiers) {
super.setParent(ClassProxy.from(parentFQN, 0, modifiers)); super.setParent(TypeProxy.from(parentFQN, 0, modifiers));
return this; return this;
} }
@ -88,7 +88,7 @@ public class FieldProxy extends AbstractProxy {
*/ */
@Override @Override
public FieldProxy build() { public FieldProxy build() {
return new FieldProxy(this.name, this.type, this.modifiers, this.parent); return new FieldProxy(this.name, this.descriptor, this.modifiers, this.parent);
} }
} }
} }

View file

@ -17,14 +17,14 @@ import static ftbsc.lll.tools.DescriptorBuilder.nameToDescriptor;
public class MethodProxy extends AbstractProxy { public class MethodProxy extends AbstractProxy {
/** /**
* An array of {@link ClassProxy} each representing the parameters of the method. * An array of {@link TypeProxy} each representing the parameters of the method.
*/ */
public final ClassProxy[] parameters; public final TypeProxy[] parameters;
/** /**
* The {@link ClassProxy} for the return type of the method. * The {@link TypeProxy} for the return type of the method.
*/ */
public final ClassProxy returnType; public final TypeProxy returnType;
/** /**
* A protected constructor, called only from the builder. * A protected constructor, called only from the builder.
@ -35,11 +35,11 @@ public class MethodProxy extends AbstractProxy {
* @param returnType the return type of the method * @param returnType the return type of the method
*/ */
protected MethodProxy(String name, int modifiers, QualifiableProxy parent, Type[] parameters, Type returnType) { protected MethodProxy(String name, int modifiers, QualifiableProxy parent, Type[] parameters, Type returnType) {
super(name, Type.getMethodType(returnType, parameters), modifiers, parent); super(name, Type.getMethodDescriptor(returnType, parameters), modifiers, parent);
this.parameters = Arrays.stream(parameters) this.parameters = Arrays.stream(parameters)
.map(t -> ClassProxy.from(t, 0)) .map(t -> TypeProxy.from(t, 0))
.toArray(ClassProxy[]::new); .toArray(TypeProxy[]::new);
this.returnType = ClassProxy.from(returnType, 0); this.returnType = TypeProxy.from(returnType, 0);
} }
/** /**
@ -50,7 +50,7 @@ public class MethodProxy extends AbstractProxy {
public MethodProxy(Method m) { public MethodProxy(Method m) {
this(m.getName(), this(m.getName(),
m.getModifiers(), m.getModifiers(),
ClassProxy.from(m.getDeclaringClass()), TypeProxy.from(m.getDeclaringClass()),
Type.getArgumentTypes(m), Type.getArgumentTypes(m),
Type.getReturnType(m) Type.getReturnType(m)
); );
@ -142,7 +142,7 @@ public class MethodProxy extends AbstractProxy {
* @return the builder's state after the change * @return the builder's state after the change
*/ */
public Builder setParent(String parentFQN, int modifiers) { public Builder setParent(String parentFQN, int modifiers) {
super.setParent(ClassProxy.from(parentFQN, 0, modifiers)); super.setParent(TypeProxy.from(parentFQN, 0, modifiers));
return this; return this;
} }

View file

@ -4,7 +4,7 @@ import org.objectweb.asm.Type;
/** /**
* A container for information about an element which has a fully-qualified name. * A container for information about an element which has a fully-qualified name.
* @see ClassProxy * @see TypeProxy
* @see PackageProxy * @see PackageProxy
* @since 0.4.0 * @since 0.4.0
*/ */
@ -27,8 +27,8 @@ public abstract class QualifiableProxy extends AbstractProxy {
* @param parent the {@link QualifiableProxy} representing the parent of this element * @param parent the {@link QualifiableProxy} representing the parent of this element
* @param fullyQualifiedName the FQN of the element * @param fullyQualifiedName the FQN of the element
*/ */
protected QualifiableProxy(Type type, int modifiers, QualifiableProxy parent, String fullyQualifiedName) { protected QualifiableProxy(String descriptor, int modifiers, QualifiableProxy parent, String fullyQualifiedName) {
super(extractSimpleNameFromFQN(fullyQualifiedName), type, modifiers, parent); super(extractSimpleNameFromFQN(fullyQualifiedName), descriptor, modifiers, parent);
this.fullyQualifiedName = fullyQualifiedName; this.fullyQualifiedName = fullyQualifiedName;
this.internalName = this.fullyQualifiedName.replace('.', '/'); this.internalName = this.fullyQualifiedName.replace('.', '/');
} }

View file

@ -11,81 +11,93 @@ import static ftbsc.lll.tools.DescriptorBuilder.nameToDescriptor;
* in ASM patching. * in ASM patching.
* @since 0.4.0 * @since 0.4.0
*/ */
public class ClassProxy extends QualifiableProxy { public class TypeProxy extends QualifiableProxy {
/**
* Whether this proxy represents a primitive.
*/
public final boolean primitive;
/** /**
* Protected constructor, called only from the builder. * Protected constructor, called only from the builder.
* @param name the name of the class * @param name the name of the class
* @param type the {@link Type} of the class * @param descriptor the descriptor of the class
* @param modifiers the modifiers of the class * @param modifiers the modifiers of the class
* @param parent the package containing this class * @param parent the package containing this class
*/ */
protected ClassProxy(String name, Type type, int modifiers, String parent) { protected TypeProxy(String name, String descriptor, int modifiers, String parent, boolean primitive) {
super(type, modifiers, PackageProxy.from(parent), String.format("%s.%s", name, parent)); super(descriptor, modifiers, PackageProxy.from(parent), String.format("%s.%s", name, parent));
this.primitive = primitive;
} }
/** /**
* Protected constructor, called only from the builder. * Protected constructor, called only from the builder.
* @param name the name of the class * @param name the name of the class
* @param type the {@link Type} of the class * @param descriptor the descriptor of the element
* @param modifiers the modifiers of the class * @param modifiers the modifiers of the class
* @param containerClass the FQN of the parent class of the class * @param containerClass the FQN of the parent class of the class
*/ */
protected ClassProxy(String name, Type type, int modifiers, QualifiableProxy containerClass) { protected TypeProxy(String name, String descriptor, int modifiers, QualifiableProxy containerClass, boolean primitive) {
super(type, modifiers, containerClass, String.format("%s$%s", name, containerClass.fullyQualifiedName)); super(descriptor, modifiers, containerClass, String.format("%s$%s", name, containerClass.fullyQualifiedName));
this.primitive = primitive;
} }
/** /**
* Builds a {@link ClassProxy} from a {@link Type} and modifiers. * Builds a {@link TypeProxy} from a {@link Type} and modifiers.
* @param type the {@link Type} representing this Class * @param type the {@link Type} representing this Class
* @param modifiers the modifiers of the class * @param modifiers the modifiers of the class
*/ */
public static ClassProxy from(Type type, int modifiers) { public static TypeProxy from(Type type, int modifiers) {
while(type.getSort() == Type.ARRAY)
type = type.getElementType();
String fqn = type.getInternalName().replace('/', '.'); String fqn = type.getInternalName().replace('/', '.');
String simpleName = extractSimpleNameFromFQN(fqn); String simpleName = extractSimpleNameFromFQN(fqn);
String parent = extractParentFromFQN(fqn); String parent = extractParentFromFQN(fqn);
boolean primitive = type.getSort() < Type.ARRAY;
if(fqn.contains("$")) if(fqn.contains("$"))
return new ClassProxy(simpleName, type, modifiers, from(parent, 0, Modifier.PUBLIC)); return new TypeProxy(simpleName, type.getDescriptor(), modifiers, from(type, Modifier.PUBLIC), primitive);
else return new ClassProxy(simpleName, type, modifiers, parent); else return new TypeProxy(simpleName, type.getDescriptor(), modifiers, parent, primitive);
} }
/** /**
* Builds a {@link ClassProxy} given only the fully-qualified name and modifiers. * Builds a {@link TypeProxy} given only the fully-qualified name and modifiers.
* @param fqn the fully qualified name of the desired class * @param fqn the fully qualified name of the desired class
* @param arrayLevel the array level for this type * @param arrayLevel the array level for this type
* @param modifiers the access modifiers of the desired class * @param modifiers the access modifiers of the desired class
* @implNote If present, parent classes will be assumed to have {@code public} as * @implNote If present, parent classes will be assumed to have {@code public} as
* their only modifier. * their only modifier.
* @return the built {@link ClassProxy} * @return the built {@link TypeProxy}
*/ */
protected static ClassProxy from(String fqn, int arrayLevel, int modifiers) { protected static TypeProxy from(String fqn, int arrayLevel, int modifiers) {
return from(Type.getObjectType(nameToDescriptor(fqn, arrayLevel)), modifiers); return from(Type.getObjectType(nameToDescriptor(fqn, arrayLevel)), modifiers);
} }
/** /**
* Builds a {@link ClassProxy} from a {@link Class} object. * Builds a {@link TypeProxy} from a {@link Class} object.
* @param clazz the {@link Class} object representing the target class * @param clazz the {@link Class} object representing the target class
* @return the built {@link ClassProxy} * @return the built {@link TypeProxy}
*/ */
public static ClassProxy from(Class<?> clazz) { public static TypeProxy from(Class<?> clazz) {
Class<?> parentClass = clazz.getEnclosingClass(); Class<?> parentClass = clazz.getEnclosingClass();
if(parentClass == null) if(parentClass == null)
return new ClassProxy( return new TypeProxy(
clazz.getSimpleName(), clazz.getSimpleName(),
Type.getType(clazz), Type.getDescriptor(clazz),
clazz.getModifiers(), clazz.getModifiers(),
clazz.getPackage().getName() clazz.getPackage().getName(),
clazz.isPrimitive()
); );
else else
return new ClassProxy( return new TypeProxy(
clazz.getSimpleName(), clazz.getSimpleName(),
Type.getType(clazz), Type.getDescriptor(clazz),
clazz.getModifiers(), clazz.getModifiers(),
from(parentClass) from(parentClass),
clazz.isPrimitive()
); );
} }
/** /**
* Returns a new instance of {@link ClassProxy.Builder}. * Returns a new instance of {@link TypeProxy.Builder}.
* @param name the name of the class * @param name the name of the class
* @return the builder object for class proxies * @return the builder object for class proxies
*/ */
@ -100,13 +112,18 @@ public class ClassProxy extends QualifiableProxy {
*/ */
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
return obj instanceof ClassProxy && super.equals(obj); return obj instanceof TypeProxy && super.equals(obj);
} }
/** /**
* A builder object for {@link ClassProxy}. * A builder object for {@link TypeProxy}.
*/ */
public static class Builder extends AbstractProxy.Builder<ClassProxy> { public static class Builder extends AbstractProxy.Builder<TypeProxy> {
/**
* Whether the proxy represents a primitive.
*/
private boolean primitive;
/** /**
* The constructor of the builder, used only internally. * The constructor of the builder, used only internally.
@ -114,6 +131,7 @@ public class ClassProxy extends QualifiableProxy {
*/ */
Builder(String name) { Builder(String name) {
super(name); super(name);
this.primitive = false;
} }
/** /**
@ -124,12 +142,12 @@ public class ClassProxy extends QualifiableProxy {
* @return the builder's state after the change * @return the builder's state after the change
*/ */
public Builder setParent(Class<?> containerClass) { public Builder setParent(Class<?> containerClass) {
super.setParent(ClassProxy.from(containerClass)); super.setParent(TypeProxy.from(containerClass));
return this; return this;
} }
/** /**
* Sets this class as an inner class and builds a {@link ClassProxy} * Sets this class as an inner class and builds a {@link TypeProxy}
* from the given parent and modifiers. * from the given parent and modifiers.
* @param parentFQN the fully qualified name of the parent * @param parentFQN the fully qualified name of the parent
* @param modifiers the modifiers of the parent (if it's a class) * @param modifiers the modifiers of the parent (if it's a class)
@ -137,12 +155,12 @@ public class ClassProxy extends QualifiableProxy {
* @return the builder's state after the change * @return the builder's state after the change
*/ */
public Builder setParent(String parentFQN, int modifiers, boolean isParentPackage) { public Builder setParent(String parentFQN, int modifiers, boolean isParentPackage) {
super.setParent(isParentPackage ? PackageProxy.from(parentFQN) : ClassProxy.from(parentFQN, 0, modifiers)); super.setParent(isParentPackage ? PackageProxy.from(parentFQN) : TypeProxy.from(parentFQN, 0, modifiers));
return this; return this;
} }
/** /**
* Sets this class as an inner class and builds a {@link ClassProxy} * Sets this class as an inner class and builds a {@link TypeProxy}
* from the given parent. * from the given parent.
* @param parentFQN the fully qualified name of the parent * @param parentFQN the fully qualified name of the parent
* @param isParentPackage whether this parent should be interpreted as a package or class * @param isParentPackage whether this parent should be interpreted as a package or class
@ -153,12 +171,23 @@ public class ClassProxy extends QualifiableProxy {
} }
/** /**
* Builds a {@link ClassProxy} of the given kind. * Sets the primitive flag to true or false, to signal that the type here specified
* @return the built {@link ClassProxy} * is a primitive.
* @param primitive the new state of the primitive flag
* @return the builder's state after the change
*/
public Builder setPrimitive(boolean primitive) {
this.primitive = primitive;
return this;
}
/**
* Builds a {@link TypeProxy} of the given kind.
* @return the built {@link TypeProxy}
*/ */
@Override @Override
public ClassProxy build() { public TypeProxy build() {
return new ClassProxy(this.name, this.type, this.modifiers, this.parent); return new TypeProxy(this.name, this.descriptor, this.modifiers, this.parent, this.primitive);
} }
} }
} }

View file

@ -17,6 +17,6 @@ public class FieldProxyInsnNode extends FieldInsnNode {
* @param f a {@link FieldProxy} representing the field to call * @param f a {@link FieldProxy} representing the field to call
*/ */
public FieldProxyInsnNode(int opcode, FieldProxy f) { public FieldProxyInsnNode(int opcode, FieldProxy f) {
super(opcode, f.parent.internalName, f.name, f.type.getDescriptor()); super(opcode, f.parent.internalName, f.name, f.descriptor);
} }
} }

View file

@ -18,6 +18,6 @@ public class MethodProxyInsnNode extends MethodInsnNode {
* @param m a {@link MethodProxy} representing the method to call * @param m a {@link MethodProxy} representing the method to call
*/ */
public MethodProxyInsnNode(int opcode, MethodProxy m) { public MethodProxyInsnNode(int opcode, MethodProxy m) {
super(opcode, m.parent.internalName, m.name, m.type.getDescriptor()); super(opcode, m.parent.internalName, m.name, m.descriptor);
} }
} }

View file

@ -0,0 +1,23 @@
package ftbsc.lll.tools.nodes;
import ftbsc.lll.proxies.TypeProxy;
import org.objectweb.asm.tree.TypeInsnNode;
/**
* Overrides the {@link TypeInsnNode} to add a constructor
* taking in a {@link TypeProxy}.
* @since 0.4.0
*/
public class TypeProxyInsnNode extends TypeInsnNode {
/**
* Constructs a new {@link TypeInsnNode} starting from a
* {@link TypeProxy}. The user should ensure that the TypeInsnNode
* represents a declared type before calling this.
* @param opcode the opcode, must be one of NEW, ANEWARRAY,
* CHECKCAST or INSTANCEOF
* @param t a {@link TypeProxy} representing the type to call
*/
public TypeProxyInsnNode(int opcode, TypeProxy t) {
super(opcode, t.internalName);
}
}