aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/lll/mapper/AbstractMapper.java
blob: bd71f907a4a7d46eecf22d21dffb476cdfbd6c93 (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
package ftbsc.lll.mapper;

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;

/**
 * A default implementation of {@link IMapper} meant to
 * recycle as much code as possible.
 */
public abstract class AbstractMapper implements IMapper {

   /**
    * A {@link Map} tying each plain class name to its class data.
    */
   protected final Map<String, ClassData> mappings = new HashMap<>();

   @Override
   public void reset() {
      this.mappings.clear();
   }

   @Override
   public IMapper getInverted() {
      AbstractMapper inverted = newInstance();
      this.mappings.forEach((name, data) -> {
         ClassData reverse = data.generateReverseMappings(this);
         inverted.mappings.put(data.nameMapped, reverse);
      });
      return inverted;
   }

   /**
    * Creates a new instance of this type of mapper.
    * @return the new, empty instance
    */
   protected abstract AbstractMapper newInstance();


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

   @Override
   public MethodData getMethodData(String parent, String name, String descriptor) throws MappingNotFoundException {
      return this.getClassData(parent).mapMethod(name, descriptor);
   }

   @Override
   public FieldData getFieldData(String parent, String name) throws MappingNotFoundException {
      return this.getClassData(parent).mapField(name);
   }
}