Skip to content

Commit dbd2be8

Browse files
authored
Use JVMS 4.2.2 "Unqualified name" definition to validate identifier (#1829)
1 parent 0d899bc commit dbd2be8

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/InstrumentedType.java

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ public TypeDescription validated() {
16721672
String fieldName = fieldDescription.getName();
16731673
if (!fieldSignatureTokens.add(fieldDescription.asSignatureToken())) {
16741674
throw new IllegalStateException("Duplicate field definition for " + fieldDescription);
1675-
} else if (!isValidIdentifier(fieldName)) {
1675+
} else if (!isValidUnqualifiedNameIdentifier(fieldName)) {
16761676
throw new IllegalStateException("Illegal field name for " + fieldDescription);
16771677
} else if ((fieldDescription.getModifiers() & ~ModifierContributor.ForField.MASK) != EMPTY_MASK) {
16781678
throw new IllegalStateException("Illegal field modifiers " + fieldDescription.getModifiers() + " for " + fieldDescription);
@@ -1742,7 +1742,7 @@ public TypeDescription validated() {
17421742
} else if (!returnType.getDeclaredAnnotations().isEmpty()) {
17431743
throw new IllegalStateException("The void non-type must not be annotated for " + methodDescription);
17441744
}
1745-
} else if (!isValidIdentifier(methodDescription.getInternalName())) {
1745+
} else if (!isValidMethodIdentifier(methodDescription.getInternalName())) {
17461746
throw new IllegalStateException("Illegal method name " + returnType + " for " + methodDescription);
17471747
} else if (!returnType.accept(Generic.Visitor.Validator.METHOD_RETURN)) {
17481748
throw new IllegalStateException("Illegal return type " + returnType + " for " + methodDescription);
@@ -1765,7 +1765,7 @@ public TypeDescription validated() {
17651765
String parameterName = parameterDescription.getName();
17661766
if (!parameterNames.add(parameterName)) {
17671767
throw new IllegalStateException("Duplicate parameter name of " + parameterDescription + " for " + methodDescription);
1768-
} else if (!isValidIdentifier(parameterName)) {
1768+
} else if (!isValidUnqualifiedNameIdentifier(parameterName)) {
17691769
throw new IllegalStateException("Illegal parameter name of " + parameterDescription + " for " + methodDescription);
17701770
}
17711771
}
@@ -1860,6 +1860,59 @@ private static boolean isValidIdentifier(String identifier) {
18601860
}
18611861
return true;
18621862
}
1863+
1864+
/**
1865+
* Checks if an identifier is a valid "Unqualified Name" for a field, a local variable or a formal parameter,
1866+
* per JVMS 4.2.2.
1867+
*
1868+
* @param identifier The identifier to check for validity.
1869+
* @return {@code true} if the given identifier is valid.
1870+
*/
1871+
private static boolean isValidUnqualifiedNameIdentifier(String identifier) {
1872+
if (identifier.length() == 0)
1873+
return false;
1874+
1875+
for (int index = 0; index < identifier.length(); index++) {
1876+
char c = identifier.charAt(index);
1877+
switch (c) {
1878+
case '.':
1879+
case ';':
1880+
case '[':
1881+
case '/':
1882+
return false;
1883+
}
1884+
}
1885+
return true;
1886+
}
1887+
1888+
/**
1889+
* Checks if an identifier is a valid "Unqualified Name" for a method, per JVMS 4.2.2.
1890+
*
1891+
* @param identifier The identifier to check for validity.
1892+
* @return {@code true} if the given identifier is valid.
1893+
*/
1894+
private static boolean isValidMethodIdentifier(String identifier) {
1895+
if (identifier.length() == 0)
1896+
return false;
1897+
1898+
if (identifier.equals(MethodDescription.TYPE_INITIALIZER_INTERNAL_NAME) ||
1899+
identifier.equals(MethodDescription.CONSTRUCTOR_INTERNAL_NAME))
1900+
return true;
1901+
1902+
for (int index = 0; index < identifier.length(); index++) {
1903+
char c = identifier.charAt(index);
1904+
switch (c) {
1905+
case '.':
1906+
case ';':
1907+
case '[':
1908+
case '/':
1909+
case '<':
1910+
case '>':
1911+
return false;
1912+
}
1913+
}
1914+
return true;
1915+
}
18631916
}
18641917

18651918
/**

byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/InstrumentedTypeDefaultTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
public class InstrumentedTypeDefaultTest {
4343

44-
private static final String FOO = "foo", BAR = "bar", QUX = "qux", BAZ = "baz", ILLEGAL_NAME = "<>";
44+
private static final String FOO = "foo", BAR = "bar", QUX = "qux", BAZ = "baz", ILLEGAL_NAME = "the;name";
4545

4646
private static final int ILLEGAL_MODIFIERS = -1, OTHER_MODIFIERS = 42;
4747

0 commit comments

Comments
 (0)