feat: added getRawMappings(), rewritten MultiMapper for efficiency

This commit is contained in:
zaaarf 2023-08-27 01:55:04 +02:00
parent 539be77980
commit 787fea3dd1
No known key found for this signature in database
GPG key ID: 6445A5CD15E5B40C
4 changed files with 58 additions and 50 deletions

View file

@ -58,4 +58,9 @@ public abstract class AbstractMapper implements IMapper {
public FieldData getFieldData(String parent, String name) throws MappingNotFoundException { public FieldData getFieldData(String parent, String name) throws MappingNotFoundException {
return this.getClassData(parent).mapField(name); return this.getClassData(parent).mapField(name);
} }
@Override
public Map<String, ClassData> getRawMappings() {
return this.mappings;
}
} }

View file

@ -77,4 +77,10 @@ public interface IMapper {
* @throws MappingNotFoundException if no mapping is found * @throws MappingNotFoundException if no mapping is found
*/ */
FieldData getFieldData(String parent, String name) throws MappingNotFoundException; FieldData getFieldData(String parent, String name) throws MappingNotFoundException;
/**
* Fetches the "raw mappings" from an {@link IMapper}.
* @return a map tying each {@link ClassData} to the class' plain name
*/
Map<String, ClassData> getRawMappings();
} }

View file

@ -2,16 +2,13 @@ package ftbsc.lll.mapper.impl;
import com.google.auto.service.AutoService; import com.google.auto.service.AutoService;
import ftbsc.lll.exceptions.MalformedMappingsException; import ftbsc.lll.exceptions.MalformedMappingsException;
import ftbsc.lll.exceptions.MappingNotFoundException; import ftbsc.lll.mapper.AbstractMapper;
import ftbsc.lll.mapper.IMapper; import ftbsc.lll.mapper.IMapper;
import ftbsc.lll.mapper.MapperProvider; import ftbsc.lll.mapper.MapperProvider;
import ftbsc.lll.mapper.tools.MappingUtils; import ftbsc.lll.mapper.tools.MappingUtils;
import ftbsc.lll.mapper.tools.data.ClassData; import ftbsc.lll.mapper.tools.data.ClassData;
import ftbsc.lll.mapper.tools.data.FieldData;
import ftbsc.lll.mapper.tools.data.MethodData;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@ -19,13 +16,7 @@ import java.util.List;
* sequence of mappers applied one after the other. * sequence of mappers applied one after the other.
*/ */
@AutoService(IMapper.class) @AutoService(IMapper.class)
public class MultiMapper implements IMapper { public class MultiMapper extends AbstractMapper {
/**
* The list of mappers.
*/
private final List<IMapper> mapperList = new ArrayList<>();
@Override @Override
public boolean claim(List<String> lines) { public boolean claim(List<String> lines) {
return lines.get(0).equals("lll multimapper"); return lines.get(0).equals("lll multimapper");
@ -33,52 +24,42 @@ public class MultiMapper implements IMapper {
@Override @Override
public void populate(List<String> lines, boolean ignoreErrors) throws MalformedMappingsException { public void populate(List<String> lines, boolean ignoreErrors) throws MalformedMappingsException {
List<IMapper> mapperList = new ArrayList<>();
for(int i = 1; i < lines.size(); i++) { for(int i = 1; i < lines.size(); i++) {
List<String> data = MapperProvider.fetchFromLocalOrRemote(lines.get(i)); List<String> data = MapperProvider.fetchFromLocalOrRemote(lines.get(i));
IMapper mapper = MapperProvider.getMapper(data); IMapper mapper = MapperProvider.getMapper(data);
mapper.populate(data, ignoreErrors); mapper.populate(data, ignoreErrors);
this.mapperList.add(mapper); mapperList.add(mapper);
}
} }
@Override mapperList.get(0).getRawMappings().forEach((name, data) -> {
public IMapper getInverted() { ClassData finalData = data;
MultiMapper reverse = new MultiMapper(); for(int i = 1; i < mapperList.size(); i++)
this.mapperList.forEach(m -> reverse.mapperList.add(m.getInverted())); finalData = mapperList.get(i).getClassData(finalData.nameMapped);
Collections.reverse(reverse.mapperList);
return reverse;
}
@Override ClassData sumData = new ClassData(data.name, finalData.nameMapped);
public void reset() {
this.mapperList.forEach(IMapper::reset);
this.mapperList.clear();
}
@Override data.getMethods().forEach((signature, methodData) -> {
public ClassData getClassData(String name) throws MappingNotFoundException { for(int i = 1; i < mapperList.size(); i++) {
ClassData classData = this.mapperList.get(0).getClassData(name); IMapper mapper = mapperList.get(i);
for(int i = 1; i < this.mapperList.size(); i++)
classData = this.mapperList.get(i).getClassData(classData.nameMapped);
return classData;
}
@Override
public MethodData getMethodData(String parent, String name, String descriptor) throws MappingNotFoundException {
MethodData methodData = this.mapperList.get(0).getMethodData(parent, name, descriptor);
for(int i = 1; i < this.mapperList.size(); i++) {
IMapper mapper = this.mapperList.get(i);
methodData = mapper.getMethodData(methodData.parentClass.nameMapped, methodData.nameMapped, methodData = mapper.getMethodData(methodData.parentClass.nameMapped, methodData.nameMapped,
MappingUtils.mapMethodDescriptor(methodData.signature.descriptor, mapper, false)); MappingUtils.mapMethodDescriptor(methodData.signature.descriptor, mapper, false));
} }
return methodData; sumData.addMethod(signature.name, methodData.nameMapped, signature.descriptor);
});
data.getFields().forEach((fieldName, fieldData) -> {
for(int i = 1; i < mapperList.size(); i++)
fieldData = mapperList.get(i).getFieldData(fieldData.parentClass.nameMapped, fieldData.nameMapped);
sumData.addField(fieldName, fieldData.nameMapped);
});
this.mappings.put(sumData.name, sumData);
});
} }
@Override @Override
public FieldData getFieldData(String parent, String name) throws MappingNotFoundException { protected AbstractMapper newInstance() {
FieldData fieldData = this.mapperList.get(0).getFieldData(parent, name); return new MultiMapper();
for(int i = 1; i < this.mapperList.size(); i++)
fieldData = this.mapperList.get(i).getFieldData(fieldData.parentClass.nameMapped, fieldData.nameMapped);
return fieldData;
} }
} }

View file

@ -115,4 +115,20 @@ public class ClassData {
throw new MappingNotFoundException("field", String.format("%s.%s", this.name, fieldName)); throw new MappingNotFoundException("field", String.format("%s.%s", this.name, fieldName));
else return data; else return data;
} }
/**
* Gets the underlying {@link Map} for {@link MethodData}.
* @return a {@link Map} tying each {@link MethodSignature} to its {@link MethodData}
*/
public Map<MethodSignature, MethodData> getMethods() {
return this.methods;
}
/**
* Gets the underlying {@link Map} for {@link FieldData}.
* @return a {@link Map} tying each field name to its {@link FieldData}
*/
public Map<String, FieldData> getFields() {
return this.fields;
}
} }