You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2022/05/18 23:55:04 UTC
[groovy] branch GROOVY_3_0_X updated: GROOVY-10611: stubgen: static final field with integer or decimal value (port to 3_0_X)
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
new df6b19b7ee GROOVY-10611: stubgen: static final field with integer or decimal value (port to 3_0_X)
df6b19b7ee is described below
commit df6b19b7eec133cee6af00a378a1dbf6052879a2
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun May 1 13:47:14 2022 -0500
GROOVY-10611: stubgen: static final field with integer or decimal value (port to 3_0_X)
---
.../org/codehaus/groovy/classgen/Verifier.java | 6 +--
.../groovy/control/AnnotationConstantsVisitor.java | 48 +++++++++----------
.../groovy/tools/javac/JavaStubGenerator.java | 56 +++++++++++++---------
.../groovy/tools/stubgenerator/Groovy10611.groovy | 50 +++++++++++++++++++
4 files changed, 110 insertions(+), 50 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/classgen/Verifier.java b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
index e333fde17b..76dfebb5fc 100644
--- a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
@@ -1004,7 +1004,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
ConstructorCallExpression specialCtorCall = getFirstIfSpecialConstructorCall(firstStatement);
// in case of this(...) let the other constructor initialize
- if (specialCtorCall != null && (specialCtorCall.isThisCall())) return;
+ if (specialCtorCall != null && specialCtorCall.isThisCall()) return;
boolean isEnum = node.isEnum();
List<Statement> statements = new ArrayList<>();
@@ -1151,8 +1151,8 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
expression
));
if (fieldNode.isStatic()) {
- // GROOVY-3311: pre-defined constants added by groovy compiler for numbers/characters should be
- // initialized first so that code dependent on it does not see their values as empty
+ // GROOVY-3311: pre-defined constants added by compiler for numbers/characters should be
+ // initialized first so that code dependent on them does not see their values as empty
Expression initialValueExpression = fieldNode.getInitialValueExpression();
Expression transformed = transformInlineConstants(initialValueExpression, fieldNode.getType());
if (transformed instanceof ConstantExpression) {
diff --git a/src/main/java/org/codehaus/groovy/control/AnnotationConstantsVisitor.java b/src/main/java/org/codehaus/groovy/control/AnnotationConstantsVisitor.java
index a3785f81bd..7d8551931c 100644
--- a/src/main/java/org/codehaus/groovy/control/AnnotationConstantsVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/AnnotationConstantsVisitor.java
@@ -35,34 +35,36 @@ import java.math.BigDecimal;
import static org.apache.groovy.ast.tools.ExpressionUtils.transformInlineConstants;
/**
- * Visitor to resolve constants in annotation definitions.
+ * Resolves constants in annotation definitions.
*/
public class AnnotationConstantsVisitor extends ClassCodeVisitorSupport {
- private SourceUnit source;
- private boolean inAnnotationDef;
- public void visitClass(ClassNode node, SourceUnit source) {
- this.source = source;
- this.inAnnotationDef = node.isAnnotationDefinition();
- super.visitClass(node);
- this.inAnnotationDef = false;
- }
+ private boolean annotationDef;
+ private SourceUnit sourceUnit;
@Override
- protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
- if (!inAnnotationDef) return;
- visitStatement(node.getFirstStatement(), node.getReturnType());
+ protected SourceUnit getSourceUnit() {
+ return sourceUnit;
}
- private static void visitStatement(Statement statement, ClassNode returnType) {
- if (statement instanceof ReturnStatement) {
- // normal path
- ReturnStatement rs = (ReturnStatement) statement;
- rs.setExpression(transformConstantExpression(rs.getExpression(), returnType));
- } else if (statement instanceof ExpressionStatement) {
- // path for JavaStubGenerator
- ExpressionStatement es = (ExpressionStatement) statement;
- es.setExpression(transformConstantExpression(es.getExpression(), returnType));
+ public void visitClass(final ClassNode classNode, final SourceUnit sourceUnit) {
+ this.sourceUnit = sourceUnit;
+ this.annotationDef = classNode.isAnnotationDefinition();
+ super.visitClass(classNode);
+ this.annotationDef = false;
+ }
+
+ @Override
+ protected void visitConstructorOrMethod(final MethodNode node, final boolean isConstructor) {
+ if (annotationDef) {
+ Statement statement = node.getFirstStatement();
+ if (statement instanceof ReturnStatement) {
+ ReturnStatement rs = (ReturnStatement) statement;
+ rs.setExpression(transformConstantExpression(rs.getExpression(), node.getReturnType()));
+ } else if (statement instanceof ExpressionStatement) {
+ ExpressionStatement es = (ExpressionStatement) statement;
+ es.setExpression(transformConstantExpression(es.getExpression(), node.getReturnType()));
+ }
}
}
@@ -126,8 +128,4 @@ public class AnnotationConstantsVisitor extends ClassCodeVisitorSupport {
result.setSourcePosition(orig);
return result;
}
-
- protected SourceUnit getSourceUnit() {
- return source;
- }
}
diff --git a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
index 4bf8e1bff6..a7280961d7 100644
--- a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -82,9 +82,16 @@ import java.util.Set;
import java.util.stream.Stream;
import static org.apache.groovy.ast.tools.ConstructorNodeUtils.getFirstIfSpecialConstructorCall;
+import static org.codehaus.groovy.ast.ClassHelper.BigDecimal_TYPE;
+import static org.codehaus.groovy.ast.ClassHelper.boolean_TYPE;
+import static org.codehaus.groovy.ast.ClassHelper.int_TYPE;
+import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveType;
+import static org.codehaus.groovy.ast.ClassHelper.isStaticConstantInitializerType;
import static org.codehaus.groovy.ast.tools.GenericsUtils.correctToGenericsSpec;
import static org.codehaus.groovy.ast.tools.GenericsUtils.correctToGenericsSpecRecurse;
import static org.codehaus.groovy.ast.tools.GenericsUtils.createGenericsSpec;
+import static org.codehaus.groovy.ast.tools.WideningCategories.isFloatingCategory;
+import static org.codehaus.groovy.ast.tools.WideningCategories.isLongCategory;
public class JavaStubGenerator {
private final boolean java5;
@@ -228,10 +235,10 @@ public class JavaStubGenerator {
}
@Override
- public void visitProperty(PropertyNode node) {
+ public void visitProperty(PropertyNode pn) {
// GROOVY-8233 skip static properties for traits since they don't make the interface
- if (!node.isStatic() || !Traits.isTrait(node.getDeclaringClass())) {
- super.visitProperty(node);
+ if (!pn.isStatic() || !Traits.isTrait(pn.getDeclaringClass())) {
+ super.visitProperty(pn);
}
}
@@ -371,8 +378,7 @@ public class JavaStubGenerator {
private void printMethods(PrintWriter out, ClassNode classNode, boolean isEnum) {
if (!isEnum) printConstructors(out, classNode);
- @SuppressWarnings("unchecked")
- List<MethodNode> methods = (List<MethodNode>) propertyMethods.clone();
+ List<MethodNode> methods = new ArrayList<>(propertyMethods);
methods.addAll(classNode.getMethods());
for (MethodNode method : methods) {
if (isEnum && method.isSynthetic()) {
@@ -499,17 +505,23 @@ public class JavaStubGenerator {
out.print(fieldNode.getName());
if (isInterface || fieldNode.isFinal()) {
out.print(" = ");
- Expression valueExpr = fieldNode.getInitialValueExpression();
- if (valueExpr instanceof ConstantExpression) {
- valueExpr = Verifier.transformToPrimitiveConstantIfPossible((ConstantExpression) valueExpr);
+ if (fieldNode.isStatic()) {
+ Expression value = fieldNode.getInitialValueExpression();
+ value = ExpressionUtils.transformInlineConstants(value, type);
+ if (value instanceof ConstantExpression) {
+ value = Verifier.transformToPrimitiveConstantIfPossible((ConstantExpression) value);
+ if ((type.equals(value.getType()) // GROOVY-10611: integer/decimal value
+ || (isLongCategory(type) && value.getType().equals(int_TYPE))
+ || (isFloatingCategory(type) && BigDecimal_TYPE.equals(value.getType())))
+ && (type.equals(boolean_TYPE) || isStaticConstantInitializerType(type))) {
+ printValue(out, (ConstantExpression) value);
+ out.println(';');
+ return;
+ }
+ }
}
- if (valueExpr instanceof ConstantExpression
- && fieldNode.isStatic() && fieldNode.isFinal()
- && fieldNode.getType().equals(valueExpr.getType())
- && (ClassHelper.isStaticConstantInitializerType(valueExpr.getType()) || valueExpr.getType().equals(ClassHelper.boolean_TYPE))) {
- printValue(out, (ConstantExpression) valueExpr);
- } else if (ClassHelper.isPrimitiveType(type)) {
- if (type.equals(ClassHelper.boolean_TYPE)) {
+ if (isPrimitiveType(type)) {
+ if (type.equals(boolean_TYPE)) {
out.print("false");
} else {
out.print('(');
@@ -799,7 +811,7 @@ public class JavaStubGenerator {
out.print(ce.getText());
out.print('L');
} else {
- if (type != ClassHelper.int_TYPE && type != ClassHelper.boolean_TYPE) {
+ if (type != int_TYPE && type != boolean_TYPE && !type.equals(BigDecimal_TYPE)) {
out.print('(');
printType(out, type);
out.print(')');
@@ -817,13 +829,13 @@ public class JavaStubGenerator {
}
private void printDefaultValue(final PrintWriter out, final ClassNode type) {
- if (type != null && !type.equals(ClassHelper.boolean_TYPE)) {
+ if (type != null && !type.equals(boolean_TYPE)) {
out.print("(");
printType(out, type);
out.print(")");
}
- if (type != null && ClassHelper.isPrimitiveType(type)) {
- if (type.equals(ClassHelper.boolean_TYPE)) {
+ if (type != null && isPrimitiveType(type)) {
+ if (type.equals(boolean_TYPE)) {
out.print("false");
} else {
out.print("0");
@@ -845,14 +857,14 @@ public class JavaStubGenerator {
}
private void printTypeName(PrintWriter out, ClassNode type) {
- if (ClassHelper.isPrimitiveType(type)) {
- if (type.equals(ClassHelper.boolean_TYPE)) {
+ if (isPrimitiveType(type)) {
+ if (type.equals(boolean_TYPE)) {
out.print("boolean");
} else if (type.equals(ClassHelper.byte_TYPE)) {
out.print("byte");
} else if (type.equals(ClassHelper.char_TYPE)) {
out.print("char");
- } else if (type.equals(ClassHelper.int_TYPE)) {
+ } else if (type.equals(int_TYPE)) {
out.print("int");
} else if (type.equals(ClassHelper.long_TYPE)) {
out.print("long");
diff --git a/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy10611.groovy b/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy10611.groovy
new file mode 100644
index 0000000000..f2cca7f3f6
--- /dev/null
+++ b/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy10611.groovy
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.codehaus.groovy.tools.stubgenerator
+
+final class Groovy10611 extends StringSourcesStubTestCase {
+
+ @Override
+ Map<String, String> provideSources() {
+ [
+ 'C.groovy': '''
+ class C {
+ public static final long LONG = 123_456
+ public static final float FLOAT = 78.90
+ public static final String STRING = 'x'+'y'
+ }
+ ''',
+ 'Main.java': '''
+ public class Main {
+ public static void main(String[] args) {
+ new C();
+ }
+ }
+ ''',
+ ]
+ }
+
+ @Override
+ void verifyStubs() {
+ String stub = stubJavaSourceFor('C')
+ assert stub.contains('final long LONG = 123456;');
+ assert stub.contains('final float FLOAT = 78.90;');
+ assert stub.contains('final java.lang.String STRING = "xy";');
+ }
+}