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
97
|
/*******************************************************************************
* Copyright (c) 2014 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma.mapping;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import javassist.bytecode.Descriptor;
public class NameValidator
{
private static final Pattern IdentifierPattern;
private static final Pattern ClassPattern;
private static final List<String> ReservedWords = 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
{
// java allows all kinds of weird characters...
StringBuilder startChars = new StringBuilder();
StringBuilder partChars = new StringBuilder();
for( int i = Character.MIN_CODE_POINT; i <= Character.MAX_CODE_POINT; i++ )
{
if( Character.isJavaIdentifierStart( i ) )
{
startChars.appendCodePoint( i );
}
if( Character.isJavaIdentifierPart( i ) )
{
partChars.appendCodePoint( i );
}
}
String identifierRegex = "[A-Za-z_<][A-Za-z0-9_>]*";
IdentifierPattern = Pattern.compile( identifierRegex );
ClassPattern = Pattern.compile( String.format( "^(%s(\\.|/))*(%s)$", identifierRegex, identifierRegex ) );
}
public static String validateClassName( String name, boolean packageRequired )
{
if( name == null )
{
return null;
}
if( !ClassPattern.matcher( name ).matches() || ReservedWords.contains( name ) )
{
throw new IllegalNameException( name, "This doesn't look like a legal class name" );
}
if( packageRequired && new ClassEntry( name ).getPackageName() == null )
{
throw new IllegalNameException( name, "Class must be in a package" );
}
return Descriptor.toJvmName( name );
}
public static String validateFieldName( String name )
{
if( name == null )
{
return null;
}
if( !IdentifierPattern.matcher( name ).matches() || ReservedWords.contains( name ) )
{
throw new IllegalNameException( name, "This doesn't look like a legal identifier" );
}
return name;
}
public static String validateMethodName( String name )
{
return validateFieldName( name );
}
public static String validateArgumentName( String name )
{
return validateFieldName( name );
}
}
|