Skip to content

Commit f229a1b

Browse files
committed
GROOVY-11680: Java stubs use deprecated constant constructors in some cases
1 parent ae4df63 commit f229a1b

4 files changed

Lines changed: 72 additions & 3 deletions

File tree

src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ private void printField(final PrintWriter out, final FieldNode field, final bool
476476

477477
// GROOVY-5150, GROOVY-10902, GROOVY-10928, GROOVY-11019: dummy value that prevents inlining
478478
if (isPrimitiveType(type) || isStringType(type)) {
479-
out.print("new " + getWrapper(type) + "(");
479+
out.print(getWrapper(type) + ".valueOf(");
480480
printValue(out, type, defaultValueX(type));
481481
out.print(')');
482482
} else {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.codehaus.groovy.tools.stubgenerator
20+
21+
/**
22+
* Checks the stub generator defines initialization expressions for primitive fields.
23+
*/
24+
final class ClassWithPrimitiveFieldsStubTest extends StringSourcesStubTestCase {
25+
26+
Map<String, String> provideSources() {
27+
[
28+
'Dummy.java': '''
29+
public class Dummy {
30+
}
31+
''',
32+
33+
'SomeClass.groovy': '''
34+
class SomeClass {
35+
public static final String s
36+
public static final Integer foo
37+
public static final double d
38+
public static final long l
39+
public static final int bar
40+
public static final short baz
41+
public static final char c
42+
public static final byte b
43+
public static final boolean flag
44+
public static final Boolean flag2
45+
}
46+
'''
47+
]
48+
}
49+
50+
void verifyStubs() {
51+
def stubSource = stubJavaSourceFor('SomeClass')
52+
assert stubSource.contains('java.lang.String s = java.lang.String.valueOf((java.lang.String)null)')
53+
assert stubSource.contains('java.lang.Integer foo = null')
54+
assert stubSource.contains('double d = java.lang.Double.valueOf(0.0d)')
55+
assert stubSource.contains('final long l = java.lang.Long.valueOf(0L)')
56+
assert stubSource.contains('int bar = java.lang.Integer.valueOf(0)')
57+
assert stubSource.contains('short baz = java.lang.Short.valueOf(')
58+
assert stubSource.contains("char c = java.lang.Character.valueOf('\0')")
59+
assert stubSource.contains('byte b = java.lang.Byte.valueOf((byte)0)')
60+
assert stubSource.contains('boolean flag = java.lang.Boolean.valueOf(false)')
61+
assert stubSource.contains('java.lang.Boolean flag2 = null')
62+
}
63+
}

src/test/groovy/org/codehaus/groovy/tools/stubgenerator/Groovy10902.groovy

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ final class Groovy10902 extends StringSourcesStubTestCase {
2626
'G.groovy': '''
2727
class G {
2828
public static final int DYNAMIC_CONSTANT = (9.9).intValue()
29+
public static final String DYNAMIC_STRING = "a" + "b".toUpperCase()
2930
}
3031
''',
3132
'J.java': '''
3233
public class J {
3334
int m() {
3435
return G.DYNAMIC_CONSTANT;
3536
}
37+
String n() {
38+
return G.DYNAMIC_STRING;
39+
}
3640
}
3741
''',
3842
]
@@ -41,9 +45,11 @@ final class Groovy10902 extends StringSourcesStubTestCase {
4145
@Override
4246
void verifyStubs() {
4347
String stub = stubJavaSourceFor('G')
44-
assert stub.contains("public static final int DYNAMIC_CONSTANT = new java.lang.Integer(0);")
48+
assert stub.contains("public static final int DYNAMIC_CONSTANT = java.lang.Integer.valueOf(0);")
49+
assert stub.contains("public static final java.lang.String DYNAMIC_STRING = java.lang.String.valueOf((java.lang.String)null);")
4550

4651
Object pojo = loader.loadClass('J').getDeclaredConstructor().newInstance()
4752
assert pojo.m() == 9
53+
assert pojo.n() == 'aB'
4854
}
4955
}

src/test/groovy/org/codehaus/groovy/tools/stubgenerator/Groovy11019.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class Groovy11019 extends StringSourcesStubTestCase {
4444
@Override
4545
void verifyStubs() {
4646
String stub = stubJavaSourceFor('G')
47-
assert stub.contains("public static final java.lang.String STATIC_STRING = new java.lang.String((java.lang.String)null);")
47+
assert stub.contains("public static final java.lang.String STATIC_STRING = java.lang.String.valueOf((java.lang.String)null);")
4848

4949
Object pojo = loader.loadClass('J').getDeclaredConstructor().newInstance()
5050
assert pojo.m() == 'hello world'

0 commit comments

Comments
 (0)