diff options
author | zaaarf <zaaarf@proton.me> | 2023-09-01 12:09:26 +0200 |
---|---|---|
committer | zaaarf <zaaarf@proton.me> | 2023-09-01 12:09:26 +0200 |
commit | fa6c73faf0ebdaac59a309f7c4bc114a3df99a24 (patch) | |
tree | 2fa03aa5e2be18e44725057e8288ef4007621d8f /src/main/java/ftbsc/lll/mapper/tools | |
parent | ea030e54b999aba4d8c3409c1507047f0a8aa8c5 (diff) |
feat: rewrote the mapper api to make more sense
Diffstat (limited to 'src/main/java/ftbsc/lll/mapper/tools')
-rw-r--r-- | src/main/java/ftbsc/lll/mapper/tools/Mapper.java | 78 | ||||
-rw-r--r-- | src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java | 11 | ||||
-rw-r--r-- | src/main/java/ftbsc/lll/mapper/tools/data/ClassData.java | 4 |
3 files changed, 85 insertions, 8 deletions
diff --git a/src/main/java/ftbsc/lll/mapper/tools/Mapper.java b/src/main/java/ftbsc/lll/mapper/tools/Mapper.java new file mode 100644 index 0000000..2b59d17 --- /dev/null +++ b/src/main/java/ftbsc/lll/mapper/tools/Mapper.java @@ -0,0 +1,78 @@ +package ftbsc.lll.mapper.tools; + +import ftbsc.lll.exceptions.MappingNotFoundException; +import ftbsc.lll.mapper.tools.data.ClassData; +import ftbsc.lll.mapper.tools.data.FieldData; +import ftbsc.lll.mapper.tools.data.MethodData; + +import java.util.HashMap; +import java.util.Map; + +/** + * An object containing parsed mapping data, which can + * apply a conversion as requested. + */ +public class Mapper { + /** + * A {@link Map} tying each plain class name to its class data. + */ + protected final Map<String, ClassData> mappings = new HashMap<>(); + + /** + * Gets the {@link ClassData} given the plain name. + * @param name the plain internal name of the desired class + * @return the mapped name of the class + * @throws MappingNotFoundException if no mapping is found + */ + public ClassData getClassData(String name) throws MappingNotFoundException { + ClassData data = this.mappings.get(name.replace('.', '/')); + if(data == null) + throw new MappingNotFoundException("class", name); + else return data; + } + + /** + * Gets the mapped name of a method + * @param parent the plain internal name of the parent class + * @param name the plain method name + * @param descriptor the descriptor of the method + * @return the mapped name of the given member + * @throws MappingNotFoundException if no mapping is found + */ + public MethodData getMethodData(String parent, String name, String descriptor) throws MappingNotFoundException { + return this.getClassData(parent).mapMethod(name, descriptor); + } + + /** + * Gets the mapped name of a field. + * @param parent the plain internal name of the parent class + * @param name the field's plain name + * @return the mapped name of the requested field + * @throws MappingNotFoundException if no mapping is found + */ + public FieldData getFieldData(String parent, String name) throws MappingNotFoundException { + return this.getClassData(parent).mapField(name); + } + + /** + * Gets the "raw mappings". + * @return a {@link Map} tying each {@link ClassData} to the class' plain name + */ + public Map<String, ClassData> getRawMappings() { + return this.mappings; + } + + /** + * Builds a new {@link Mapper} that functions in reverse to this one (i.e. one that + * considers as "mapped" what this one considers plain, and vice versa). + * @return the inverted mapper + */ + public Mapper getInverted() { + Mapper inverted = new Mapper(); + this.mappings.forEach((name, data) -> { + ClassData reverse = data.generateReverseMappings(this); + inverted.mappings.put(data.nameMapped, reverse); + }); + return inverted; + } +} diff --git a/src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java b/src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java index fcc5525..fc121dd 100644 --- a/src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java +++ b/src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java @@ -1,7 +1,6 @@ package ftbsc.lll.mapper.tools; import ftbsc.lll.exceptions.MappingNotFoundException; -import ftbsc.lll.mapper.IMapper; import ftbsc.lll.tools.DescriptorBuilder; import org.objectweb.asm.Type; @@ -14,11 +13,11 @@ public class MappingUtils { /** * Maps a method descriptor, replacing its class references with their mapped counterparts. * @param descriptor a {@link String} containing the descriptor - * @param mapper the {@link IMapper} to use for the process + * @param mapper the {@link Mapper} to use for the process * @param reverse if true it uses the inverted mapper rather than the normal one * @return the mapped descriptor */ - public static String mapMethodDescriptor(String descriptor, IMapper mapper, boolean reverse) { + public static String mapMethodDescriptor(String descriptor, Mapper mapper, boolean reverse) { Type method = Type.getMethodType(descriptor); Type[] arguments = method.getArgumentTypes(); Type returnType = method.getReturnType(); @@ -31,13 +30,13 @@ public class MappingUtils { } /** - * Given a {@link Type} and a valid {@link IMapper} it returns its mapped counterpart. + * Given a {@link Type} and a valid {@link Mapper} it returns its mapped counterpart. * @param type the type in question - * @param mapper the {@link IMapper} to use for the process + * @param mapper the {@link Mapper} to use for the process * @param reverse if true it uses the inverted mapper rather than the normal one * @return the mapped type */ - public static Type mapType(Type type, IMapper mapper, boolean reverse) { + public static Type mapType(Type type, Mapper mapper, boolean reverse) { //unwrap arrays Type unwrapped = type; int arrayLevel = 0; diff --git a/src/main/java/ftbsc/lll/mapper/tools/data/ClassData.java b/src/main/java/ftbsc/lll/mapper/tools/data/ClassData.java index 784d0e0..4b4f58e 100644 --- a/src/main/java/ftbsc/lll/mapper/tools/data/ClassData.java +++ b/src/main/java/ftbsc/lll/mapper/tools/data/ClassData.java @@ -1,7 +1,7 @@ package ftbsc.lll.mapper.tools.data; import ftbsc.lll.exceptions.MappingNotFoundException; -import ftbsc.lll.mapper.IMapper; +import ftbsc.lll.mapper.tools.Mapper; import ftbsc.lll.mapper.tools.MappingUtils; import java.util.HashMap; @@ -85,7 +85,7 @@ public class ClassData { * @param mapper the mapper that generated this data * @return a ClassData representing the inverted mappings */ - public ClassData generateReverseMappings(IMapper mapper) { + public ClassData generateReverseMappings(Mapper mapper) { ClassData reverse = new ClassData(this.nameMapped, this.name); this.methods.forEach((signature, data) -> reverse.addMethod(nameMapped, signature.name, MappingUtils.mapMethodDescriptor(signature.descriptor, mapper, false))); |