summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/translation/mapping/NameValidator.java
blob: 19473ead7e373219eb969b2ad23a1bfadafca9e5 (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
/*******************************************************************************
 * 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.translation.mapping;

import cuchaz.enigma.throwables.IllegalNameException;
import cuchaz.enigma.translation.representation.entry.ClassEntry;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public class NameValidator {
	private static final Pattern IDENTIFIER_PATTERN;
	private static final Pattern CLASS_PATTERN;
	private static final List<String> ILLEGAL_IDENTIFIERS = Arrays.asList(
			"abstract", "continue", "for", "new", "switch", "assert", "default", "goto", "package", "synchronized",
			"boolean", "do", "if", "private", "this", "break", "double", "implements", "protected", "throw", "byte",
			"else", "import", "public", "throws", "case", "enum", "instanceof", "return", "transient", "catch",
			"extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", "finally",
			"long", "strictfp", "volatile", "const", "float", "native", "super", "while"
	);

	static {
		String identifierRegex = "[A-Za-z_<][A-Za-z0-9_>]*";
		IDENTIFIER_PATTERN = Pattern.compile(identifierRegex);
		CLASS_PATTERN = Pattern.compile(String.format("^(%s(\\.|/))*(%s)$", identifierRegex, identifierRegex));
	}

	public static void validateClassName(String name, boolean packageRequired) {
		if (!CLASS_PATTERN.matcher(name).matches() || ILLEGAL_IDENTIFIERS.contains(name)) {
			throw new IllegalNameException(name, "This doesn't look like a legal class name");
		}
		if (packageRequired && ClassEntry.getPackageName(name) == null) {
			throw new IllegalNameException(name, "Class must be in a package");
		}
	}

	public static void validateIdentifier(String name) {
		if (!IDENTIFIER_PATTERN.matcher(name).matches() || ILLEGAL_IDENTIFIERS.contains(name)) {
			throw new IllegalNameException(name, "This doesn't look like a legal identifier");
		}
	}

	public static boolean isReserved(String name) {
		return ILLEGAL_IDENTIFIERS.contains(name);
	}
}