summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/mapping/entry/ReferencedEntryPool.java
blob: 12b395543b1316d12f674e218f302484f79a78f3 (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
/*******************************************************************************
 * Copyright (c) 2015 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public
 * License v3.0 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Contributors:
 * Jeff Martin - initial API and implementation
 ******************************************************************************/

package cuchaz.enigma.mapping.entry;

import cuchaz.enigma.mapping.MethodDescriptor;
import cuchaz.enigma.mapping.TypeDescriptor;

import java.util.HashMap;
import java.util.Map;

public class ReferencedEntryPool {
	private final Map<String, ClassEntry> classEntries = new HashMap<>();
	private final Map<String, Map<String, MethodEntry>> methodEntries = new HashMap<>();
	private final Map<String, Map<String, FieldEntry>> fieldEntries = new HashMap<>();

	public ClassEntry getClass(String name) {
		// TODO: FIXME - I'm a hack!
		if ("[T".equals(name) || "[[T".equals(name) || "[[[T".equals(name)) {
			name = name.replaceAll("T", "Ljava/lang/Object;");
		}

		final String computeName = name;
		return this.classEntries.computeIfAbsent(name, s -> new ClassEntry(computeName));
	}

	public MethodEntry getMethod(ClassEntry ownerEntry, String name, String desc) {
		return getMethod(ownerEntry, name, new MethodDescriptor(desc));
	}

	public MethodEntry getMethod(ClassEntry ownerEntry, String name, MethodDescriptor desc) {
		String key = name + desc.toString();
		return getClassMethods(ownerEntry.getName()).computeIfAbsent(key, s -> new MethodEntry(ownerEntry, name, desc));
	}

	public FieldEntry getField(ClassEntry ownerEntry, String name, String desc) {
		return getField(ownerEntry, name, new TypeDescriptor(desc));
	}

	public FieldEntry getField(ClassEntry ownerEntry, String name, TypeDescriptor desc) {
		return getClassFields(ownerEntry.getName()).computeIfAbsent(name, s -> new FieldEntry(ownerEntry, name, desc));
	}

	private Map<String, MethodEntry> getClassMethods(String name) {
		return methodEntries.computeIfAbsent(name, s -> new HashMap<>());
	}

	private Map<String, FieldEntry> getClassFields(String name) {
		return fieldEntries.computeIfAbsent(name, s -> new HashMap<>());
	}
}