summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/mapping/BehaviorSignature.java
blob: a6371f8558202a8ad85f3bcdc8dd5567a2d8c65c (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
85
86
87
88
89
90
91
92
93
94
95
96
package cuchaz.enigma.mapping;

import java.util.List;

import com.beust.jcommander.internal.Lists;

public class BehaviorSignature {
	
	public static interface ClassReplacer {
		ClassEntry replace(ClassEntry entry);
	}
	
	private List<Type> m_argumentTypes;
	private Type m_returnType;
	
	public BehaviorSignature(String signature) {
		m_argumentTypes = Lists.newArrayList();
		int i=0;
		while (i<signature.length()) {
			char c = signature.charAt(i);
			if (c == '(') {
				assert(m_argumentTypes.isEmpty());
				assert(m_returnType == null);
				i++;
			} else if (c == ')') {
				i++;
				break;
			} else {
				String type = Type.parseFirst(signature.substring(i));
				m_argumentTypes.add(new Type(type));
				i += type.length();
			}
		}
		m_returnType = new Type(Type.parseFirst(signature.substring(i)));
	}
	
	public BehaviorSignature(BehaviorSignature other, ClassReplacer replacer) {
		m_argumentTypes = Lists.newArrayList(other.m_argumentTypes);
		for (int i=0; i<m_argumentTypes.size(); i++) {
			Type type = m_argumentTypes.get(i);
			if (type.isClass()) {
				ClassEntry newClassEntry = replacer.replace(type.getClassEntry());
				if (newClassEntry != null) {
					m_argumentTypes.set(i, new Type(newClassEntry));
				}
			}
		}
		m_returnType = other.m_returnType;
		if (other.m_returnType.isClass()) {
			ClassEntry newClassEntry = replacer.replace(m_returnType.getClassEntry());
			if (newClassEntry != null) {
				m_returnType = new Type(newClassEntry);
			}
		}
	}
	
	public List<Type> getArgumentTypes() {
		return m_argumentTypes;
	}
	
	public Type getReturnType() {
		return m_returnType;
	}
	
	@Override
	public String toString() {
		StringBuilder buf = new StringBuilder();
		buf.append("(");
		for (int i=0; i<m_argumentTypes.size(); i++) {
			if (i > 0) {
				buf.append(",");
			}
			buf.append(m_argumentTypes.get(i).toString());
		}
		buf.append(")");
		buf.append(m_returnType.toString());
		return buf.toString();
	}
	
	public Iterable<Type> types() {
		List<Type> types = Lists.newArrayList();
		types.addAll(m_argumentTypes);
		types.add(m_returnType);
		return types;
	}
	
	public Iterable<ClassEntry> classes() {
		List<ClassEntry> out = Lists.newArrayList();
		for (Type type : types()) {
			if (type.isClass()) {
				out.add(type.getClassEntry());
			}
		}
		return out;
	}
}