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

View file

@ -115,4 +115,20 @@ public class ClassData {
throw new MappingNotFoundException("field", String.format("%s.%s", this.name, fieldName));
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;
}
}