summaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/lll/mapper/impl/MultiMapper.java
blob: db12235cd63a3ec3b942ac9024f75fdcf4a1e79b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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.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;

/**
 * Special mapper type that actually resolves to an ordered
 * 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<>();

   @Override
   public boolean claim(List<String> lines) {
      return lines.get(0).equals("lll multimapper");
   }

   @Override
   public void populate(List<String> lines, boolean ignoreErrors) throws MalformedMappingsException {
      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);
      }
   }

   @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;
   }
}