-
Notifications
You must be signed in to change notification settings - Fork 35
Closed
Labels
Description
Issue
The JDefinedClass constructor checks the validity of the class name, starting here :
The JPackage constructor, does not, here :
Proposal
I propose to make the JPackage make a verification test
https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html
according to this :
- sName must not be a reserved keyword
- sName must not contain non-alphanumerical value besides the underscore
- sName must not start with a number.
- (optional) sName must not contain an upper-case character.
Implementation
Here is how I personally do :
Reserved keyword
I keep a list from https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
/** java keywords we can't use as a name for a package or a class */
public static final Set<String> JAVA_RESERVED_KEYWORDS = Collections.unmodifiableSet(new HashSet<>(
Arrays.asList("abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else", "extends", "false", "final", "finally", "float", "for", "goto",
"if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package",
"private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized",
"this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while")));
Then I just check if the name is contained in that set.
All alphanumerical, start with non-numerical
public static final Pattern VALID_PACKAGE_NAME = Pattern.compile("[A-Za-z_][A-Za-z0-9_]*");
Then I just check if sName matches this pattern. It can accept "_" as a package name, not sure if correct.
Optional check lowercase
Add a boolean option in JCodeModel::ensurePackagesLowerCase(). default value should be false.
Then create a new Pattern to match the package name
public static final Pattern VALID_LOWERCASE_PACKAGE_NAME = Pattern.compile("[a-z_][a-z0-9_]*");
To test the name against.