diff options
Diffstat (limited to 'src/cuchaz/enigma/mapping/NameValidator.java')
| -rw-r--r-- | src/cuchaz/enigma/mapping/NameValidator.java | 52 |
1 files changed, 35 insertions, 17 deletions
diff --git a/src/cuchaz/enigma/mapping/NameValidator.java b/src/cuchaz/enigma/mapping/NameValidator.java index 30982dc..a8421fa 100644 --- a/src/cuchaz/enigma/mapping/NameValidator.java +++ b/src/cuchaz/enigma/mapping/NameValidator.java | |||
| @@ -10,12 +10,28 @@ | |||
| 10 | ******************************************************************************/ | 10 | ******************************************************************************/ |
| 11 | package cuchaz.enigma.mapping; | 11 | package cuchaz.enigma.mapping; |
| 12 | 12 | ||
| 13 | import java.util.Arrays; | ||
| 14 | import java.util.List; | ||
| 13 | import java.util.regex.Pattern; | 15 | import java.util.regex.Pattern; |
| 14 | 16 | ||
| 17 | import javassist.bytecode.Descriptor; | ||
| 18 | |||
| 15 | public class NameValidator | 19 | public class NameValidator |
| 16 | { | 20 | { |
| 17 | private static final String IdentifierPattern; | 21 | private static final Pattern IdentifierPattern; |
| 18 | private static final Pattern ClassPattern; | 22 | private static final Pattern ClassPattern; |
| 23 | private static final List<String> ReservedWords = Arrays.asList( | ||
| 24 | "abstract", "continue", "for", "new", "switch", | ||
| 25 | "assert", "default", "goto", "package", "synchronized", | ||
| 26 | "boolean", "do", "if", "private", "this", | ||
| 27 | "break", "double", "implements", "protected", "throw", | ||
| 28 | "byte", "else", "import", "public", "throws", | ||
| 29 | "case", "enum", "instanceof", "return", "transient", | ||
| 30 | "catch", "extends", "int", "short", "try", | ||
| 31 | "char", "final", "interface", "static", "void", | ||
| 32 | "class", "finally", "long", "strictfp", "volatile", | ||
| 33 | "const", "float", "native", "super", "while" | ||
| 34 | ); | ||
| 19 | 35 | ||
| 20 | static | 36 | static |
| 21 | { | 37 | { |
| @@ -34,34 +50,36 @@ public class NameValidator | |||
| 34 | } | 50 | } |
| 35 | } | 51 | } |
| 36 | 52 | ||
| 37 | IdentifierPattern = String.format( "[\\Q%s\\E][\\Q%s\\E]*", startChars.toString(), partChars.toString() ); | 53 | String identifierRegex = "[A-Za-z_<][A-Za-z0-9_>]*"; |
| 38 | ClassPattern = Pattern.compile( String.format( "^(%s(\\.|/))*(%s)$", IdentifierPattern, IdentifierPattern ) ); | 54 | IdentifierPattern = Pattern.compile( identifierRegex ); |
| 55 | ClassPattern = Pattern.compile( String.format( "^(%s(\\.|/))*(%s)$", identifierRegex, identifierRegex ) ); | ||
| 39 | } | 56 | } |
| 40 | 57 | ||
| 41 | public String validateClassName( String name ) | 58 | public static String validateClassName( String name ) |
| 42 | { | 59 | { |
| 43 | if( !ClassPattern.matcher( name ).matches() ) | 60 | if( name == null || !ClassPattern.matcher( name ).matches() || ReservedWords.contains( name ) ) |
| 44 | { | 61 | { |
| 45 | throw new IllegalArgumentException( "Illegal name: " + name ); | 62 | throw new IllegalNameException( name ); |
| 46 | } | 63 | } |
| 47 | 64 | return Descriptor.toJvmName( name ); | |
| 48 | return classNameToJavaName( name ); | ||
| 49 | } | 65 | } |
| 50 | 66 | ||
| 51 | public static String fileNameToClassName( String fileName ) | 67 | public static String validateFieldName( String name ) |
| 52 | { | 68 | { |
| 53 | final String suffix = ".class"; | 69 | if( name == null || !IdentifierPattern.matcher( name ).matches() || ReservedWords.contains( name ) ) |
| 54 | |||
| 55 | if( !fileName.endsWith( suffix ) ) | ||
| 56 | { | 70 | { |
| 57 | return null; | 71 | throw new IllegalNameException( name ); |
| 58 | } | 72 | } |
| 59 | 73 | return name; | |
| 60 | return fileName.substring( 0, fileName.length() - suffix.length() ).replace( "/", "." ); | 74 | } |
| 75 | |||
| 76 | public static String validateMethodName( String name ) | ||
| 77 | { | ||
| 78 | return validateFieldName( name ); | ||
| 61 | } | 79 | } |
| 62 | 80 | ||
| 63 | public static String classNameToJavaName( String className ) | 81 | public static String validateArgumentName( String name ) |
| 64 | { | 82 | { |
| 65 | return className.replace( ".", "/" ); | 83 | return validateFieldName( name ); |
| 66 | } | 84 | } |
| 67 | } | 85 | } |