Skip to content
Permalink
Browse files
  • Loading branch information
eric-milles committed May 22, 2022
1 parent 3edc345 commit 2691fb0d2b745a7cc6647edd09a71a3831ffa460
Showing 3 changed files with 41 additions and 42 deletions.
@@ -24,9 +24,8 @@
import org.codehaus.groovy.ast.CompileUnit;
import org.codehaus.groovy.ast.MethodNode;


/**
* A context shared across generations of a class and its inner classes
* A context shared across generations of a class and its inner classes.
*/
public class GeneratorContext {

@@ -83,33 +82,31 @@ public String getNextConstructorReferenceSyntheticMethodName(MethodNode enclosin
+ syntheticMethodIdx++;
}

private static final int MIN_ENCODING = ' ';
private static final int MAX_ENCODING = ']';
private static final boolean[] CHARACTERS_TO_ENCODE = new boolean[MAX_ENCODING-MIN_ENCODING+1];
private static final boolean[] CHARACTERS_TO_ENCODE;
private static final int MIN_ENCODING, MAX_ENCODING;
static {
CHARACTERS_TO_ENCODE[' '-MIN_ENCODING] = true;
CHARACTERS_TO_ENCODE['!'-MIN_ENCODING] = true;
CHARACTERS_TO_ENCODE['/'-MIN_ENCODING] = true;
CHARACTERS_TO_ENCODE['.'-MIN_ENCODING] = true;
CHARACTERS_TO_ENCODE[';'-MIN_ENCODING] = true;
CHARACTERS_TO_ENCODE['$'-MIN_ENCODING] = true;
CHARACTERS_TO_ENCODE['<'-MIN_ENCODING] = true;
CHARACTERS_TO_ENCODE['>'-MIN_ENCODING] = true;
CHARACTERS_TO_ENCODE['['-MIN_ENCODING] = true;
CHARACTERS_TO_ENCODE[']'-MIN_ENCODING] = true;
CHARACTERS_TO_ENCODE[':'-MIN_ENCODING] = true;
CHARACTERS_TO_ENCODE['\\'-MIN_ENCODING] = true;
char[] chars = {' ', '!', '"', '#', '$', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '@', '[', '\\', ']', '^', '{', '}', '~'};

MIN_ENCODING = chars[0];
MAX_ENCODING = chars[chars.length - 1];
CHARACTERS_TO_ENCODE = new boolean[MAX_ENCODING - MIN_ENCODING + 1];

for (char c : chars) {
CHARACTERS_TO_ENCODE[c - MIN_ENCODING] = true;
}
}

public static String encodeAsValidClassName(String name) {
final int l = name.length();
StringBuilder b = null;
public static String encodeAsValidClassName(final String name) {
if (name.equals("module-info") || name.equals("package-info")) return name;

int lastEscape = -1;
for(int i = 0; i < l; ++i) {
final int encodeIndex = name.charAt(i) - MIN_ENCODING;
StringBuilder b = null;
final int n = name.length();
for (int i = 0; i < n; i += 1) {
int encodeIndex = name.charAt(i) - MIN_ENCODING;
if (encodeIndex >= 0 && encodeIndex < CHARACTERS_TO_ENCODE.length) {
if (CHARACTERS_TO_ENCODE[encodeIndex]) {
if(b == null) {
if (b == null) {
b = new StringBuilder(name.length() + 3);
b.append(name, 0, i);
} else {
@@ -120,9 +117,9 @@ public static String encodeAsValidClassName(String name) {
}
}
}
if(b == null) return name;
if (lastEscape == -1) throw new GroovyBugError("unexpected escape char control flow in "+name);
b.append(name, lastEscape + 1, l);
if (b == null) return name;
if (lastEscape == -1) throw new GroovyBugError("unexpected escape char control flow in " + name);
b.append(name, lastEscape + 1, n);
return b.toString();
}
}
@@ -28,11 +28,11 @@ import org.junit.Test
import static groovy.test.GroovyAssert.assertScript

/**
* Before Groovy 1.8, the structure of closure's inner classes
* was a bit different than it is now in 1.8+.
*
* This test checks that closure inner classes are direct child of their enclosing class,
* instead of being child of the outermost class.
* Before Groovy 1.8, the structure of closure's inner class was a bit different
* than it is now.
* <p>
* This test checks that closure inner class is direct child of its enclosing
* class, instead of being child of the outermost class.
*/
final class ClosureAndInnerClassNodeStructureTest {

@@ -97,16 +97,18 @@ final class ClosureAndInnerClassNodeStructureTest {
'''
}

@Test //GROOVY-7119, GROOVY-7120
void testIrregularMethodName() {
assertScript '''
class X {
def 'foo!bar'() {
return {}
@Test // GROOVY-7119, GROOVY-7120
void testIrregularMethodNames() {
[' ', '!', '"', '#', '$', '&', '\\\'', '(', ')', '*', '+', ',', '-', ':', '=', '@', '^', '{', '}', '~'].each { c ->
assertScript """
class X {
def 'foo${c}bar'() {
return {}
}
}
}
def str = new X().'foo!bar'().getClass().getName()
assert str == 'X$_foo_bar_closure1'
'''
def str = new X().'foo${c}bar'().getClass().getName()
assert str == 'X\$_foo_bar_closure1'
"""
}
}
}
@@ -45,7 +45,7 @@ public void testScriptStartingWithHash() {
ModuleNode mn = new ModuleNode((CompileUnit) null);
mn.setDescription("#script.groovy");

assertEquals("Dummy class name should not be empty", "#script", mn.getScriptClassDummy().getName());
assertEquals("Dummy class name should not be empty", "_script", mn.getScriptClassDummy().getName());
}

@Test // GROOVY-9577

0 comments on commit 2691fb0

Please sign in to comment.