Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions jcodemodel/src/main/java/com/helger/jcodemodel/JDefinedClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public JCodeModel owner ()
}
};

protected JDefinedClass (@NonNull final IJClassContainer <?> aParent,
JDefinedClass (@NonNull final IJClassContainer <?> aParent,
final int nMods,
@Nullable final String sName,
@NonNull final EClassType eClassType)
Expand All @@ -195,7 +195,7 @@ protected JDefinedClass (@NonNull final IJClassContainer <?> aParent,
* @param sName
* Name of this class
*/
protected JDefinedClass (@NonNull final JCodeModel aOwner, final int nMods, @Nullable final String sName)
JDefinedClass (@NonNull final JCodeModel aOwner, final int nMods, @Nullable final String sName)
{
this (aOwner, null, nMods, EClassType.CLASS, sName);
}
Expand All @@ -214,7 +214,7 @@ protected JDefinedClass (@NonNull final JCodeModel aOwner, final int nMods, @Nul
* @param sName
* Name of this class
*/
private JDefinedClass (@NonNull final JCodeModel aOwner,
JDefinedClass (@NonNull final JCodeModel aOwner,
@Nullable final IJClassContainer <?> aOuter,
final int nMods,
@NonNull final EClassType eClassType,
Expand All @@ -226,24 +226,8 @@ private JDefinedClass (@NonNull final JCodeModel aOwner,
{
ValueEnforcer.notEmpty (sName, "Name");

if (!Character.isJavaIdentifierStart (sName.charAt (0)))
{
final String msg = "JDefinedClass name " +
sName +
" contains illegal character" +
" for beginning of identifier: " +
sName.charAt (0);
throw new IllegalArgumentException (msg);
}
for (int i = 1; i < sName.length (); i++)
{
final char c = sName.charAt (i);
if (!Character.isJavaIdentifierPart (c))
{
final String msg = "JDefinedClass name " + sName + " contains illegal character " + c;
throw new IllegalArgumentException (msg);
}
}
if (!JJavaName.isTypeIdentifier (sName))
throw new IllegalArgumentException ("JDefinedClass name " + sName + " is invalid type name");
}

if (isInterface ())
Expand Down
14 changes: 14 additions & 0 deletions jcodemodel/src/main/java/com/helger/jcodemodel/JJavaName.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ public static boolean isJavaIdentifier (@NonNull final String sStr)
return true;
}

/// verifies that a name is valid type identifier
///
/// https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.8
///
/// > A type identifier is an identifier that is not the character sequence var.
///
/// > Type identifiers are used in certain contexts involving the declaration or use of types. For
/// > example, the name of a class must be a TypeIdentifier, so it is illegal to declare a class
/// > named var.
public static boolean isTypeIdentifier (@NonNull final String sStr)
{
return isJavaIdentifier (sStr) && !"var".equals (sStr);
}

/**
* Checks if the given string is a valid fully qualified name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,16 @@ public void testEMods () throws JCodeModelException
Assert.assertTrue (jdc.isEMod (EMod.SEALED, EMod.PRIVATE));
}

@Test
public void testTypeNameVar () throws JCodeModelException
{
JCodeModel jcm = new JCodeModel ();
Assert.assertThrows (IllegalArgumentException.class, () -> new JDefinedClass (jcm, JMod.NONE, "double"));
Assert.assertThrows (IllegalArgumentException.class, () -> new JDefinedClass (jcm, JMod.NONE, "package"));
Assert.assertThrows (IllegalArgumentException.class, () -> new JDefinedClass (jcm, JMod.NONE, "var"));
new JDefinedClass (jcm, JMod.NONE, "Double");
new JDefinedClass (jcm, JMod.NONE, "Package");
new JDefinedClass (jcm, JMod.NONE, "Var");
}

}