summaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/lll/mapper/tools/Mapper.java
blob: 2b59d177f1287d7926940bfad5c8d1e90c0577b1 (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
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;
   }
}