[jira] [Commented] (GROOVY-7975) Use of static final field in an annotation element causes compile errors

2018-11-04 Thread ASF GitHub Bot (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-7975?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16674627#comment-16674627
 ] 

ASF GitHub Bot commented on GROOVY-7975:


Github user paulk-asert commented on a diff in the pull request:

https://github.com/apache/groovy/pull/819#discussion_r230627859
  
--- Diff: src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java ---
@@ -18,20 +18,205 @@
  */
 package org.apache.groovy.ast.tools;
 
+import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.FieldNode;
+import org.codehaus.groovy.ast.expr.BinaryExpression;
 import org.codehaus.groovy.ast.expr.ClassExpression;
 import org.codehaus.groovy.ast.expr.ConstantExpression;
 import org.codehaus.groovy.ast.expr.Expression;
 import org.codehaus.groovy.ast.expr.ListExpression;
 import org.codehaus.groovy.ast.expr.PropertyExpression;
+import org.codehaus.groovy.ast.expr.VariableExpression;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+
+import static org.codehaus.groovy.syntax.Types.DIVIDE;
+import static org.codehaus.groovy.syntax.Types.MINUS;
+import static org.codehaus.groovy.syntax.Types.MULTIPLY;
+import static org.codehaus.groovy.syntax.Types.PLUS;
 
 public class ExpressionUtils {
+private static ArrayList handledTypes = new 
ArrayList();
+
 private ExpressionUtils() {
 
 }
 
-// resolve constant-looking expressions statically (do here as gets 
transformed away later)
+static {
+handledTypes.add(PLUS);
+handledTypes.add(MINUS);
+handledTypes.add(MULTIPLY);
+handledTypes.add(DIVIDE);
+}
+
+public static ConstantExpression 
transformBinaryConstantExpression(BinaryExpression be, ClassNode targetType) {
+if (isTypeOrArrayOfType(targetType, ClassHelper.STRING_TYPE, 
false)) {
+if (be.getOperation().getType() == PLUS) {
+Expression left = 
transformInlineConstants(be.getLeftExpression(), targetType);
+Expression right = 
transformInlineConstants(be.getRightExpression(), targetType);
+if (left instanceof ConstantExpression && right instanceof 
ConstantExpression) {
+ConstantExpression newExp = new 
ConstantExpression((String) ((ConstantExpression) left).getValue() +
+((ConstantExpression) right).getValue());
+newExp.setSourcePosition(be);
+return newExp;
+}
+}
+} else if (isTypeOrArrayOfType(targetType, 
ClassHelper.Integer_TYPE, false) || isTypeOrArrayOfType(targetType, 
ClassHelper.int_TYPE, false)) {
+int type = be.getOperation().getType();
+if (handledTypes.contains(type)) {
+Expression left = 
transformInlineConstants(be.getLeftExpression(), targetType);
+Expression right = 
transformInlineConstants(be.getRightExpression(), targetType);
+if (left instanceof ConstantExpression && right instanceof 
ConstantExpression) {
+Integer newVal = null;
+switch(type) {
+case PLUS:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() +
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+case MINUS:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() -
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+case MULTIPLY:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() *
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+case DIVIDE:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() /
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+}
+if (newVal != null) {
+ConstantExpression newExp = new 
ConstantExpression(newVal, true);
+newExp.setSourcePosition(be);
+return newExp;
+}
+}
+}
+} else if (isTypeOrArrayOfType(targetType, 

[GitHub] groovy pull request #819: GROOVY-7975/GROOVY-3278/GROOVY-7854: improved acce...

2018-11-04 Thread paulk-asert
Github user paulk-asert commented on a diff in the pull request:

https://github.com/apache/groovy/pull/819#discussion_r230627859
  
--- Diff: src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java ---
@@ -18,20 +18,205 @@
  */
 package org.apache.groovy.ast.tools;
 
+import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.FieldNode;
+import org.codehaus.groovy.ast.expr.BinaryExpression;
 import org.codehaus.groovy.ast.expr.ClassExpression;
 import org.codehaus.groovy.ast.expr.ConstantExpression;
 import org.codehaus.groovy.ast.expr.Expression;
 import org.codehaus.groovy.ast.expr.ListExpression;
 import org.codehaus.groovy.ast.expr.PropertyExpression;
+import org.codehaus.groovy.ast.expr.VariableExpression;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+
+import static org.codehaus.groovy.syntax.Types.DIVIDE;
+import static org.codehaus.groovy.syntax.Types.MINUS;
+import static org.codehaus.groovy.syntax.Types.MULTIPLY;
+import static org.codehaus.groovy.syntax.Types.PLUS;
 
 public class ExpressionUtils {
+private static ArrayList handledTypes = new 
ArrayList();
+
 private ExpressionUtils() {
 
 }
 
-// resolve constant-looking expressions statically (do here as gets 
transformed away later)
+static {
+handledTypes.add(PLUS);
+handledTypes.add(MINUS);
+handledTypes.add(MULTIPLY);
+handledTypes.add(DIVIDE);
+}
+
+public static ConstantExpression 
transformBinaryConstantExpression(BinaryExpression be, ClassNode targetType) {
+if (isTypeOrArrayOfType(targetType, ClassHelper.STRING_TYPE, 
false)) {
+if (be.getOperation().getType() == PLUS) {
+Expression left = 
transformInlineConstants(be.getLeftExpression(), targetType);
+Expression right = 
transformInlineConstants(be.getRightExpression(), targetType);
+if (left instanceof ConstantExpression && right instanceof 
ConstantExpression) {
+ConstantExpression newExp = new 
ConstantExpression((String) ((ConstantExpression) left).getValue() +
+((ConstantExpression) right).getValue());
+newExp.setSourcePosition(be);
+return newExp;
+}
+}
+} else if (isTypeOrArrayOfType(targetType, 
ClassHelper.Integer_TYPE, false) || isTypeOrArrayOfType(targetType, 
ClassHelper.int_TYPE, false)) {
+int type = be.getOperation().getType();
+if (handledTypes.contains(type)) {
+Expression left = 
transformInlineConstants(be.getLeftExpression(), targetType);
+Expression right = 
transformInlineConstants(be.getRightExpression(), targetType);
+if (left instanceof ConstantExpression && right instanceof 
ConstantExpression) {
+Integer newVal = null;
+switch(type) {
+case PLUS:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() +
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+case MINUS:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() -
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+case MULTIPLY:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() *
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+case DIVIDE:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() /
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+}
+if (newVal != null) {
+ConstantExpression newExp = new 
ConstantExpression(newVal, true);
+newExp.setSourcePosition(be);
+return newExp;
+}
+}
+}
+} else if (isTypeOrArrayOfType(targetType, 
ClassHelper.Double_TYPE, false) || isTypeOrArrayOfType(targetType, 
ClassHelper.double_TYPE, false)) {
--- End diff --

Already refactored - thanks for the suggestion.


---


[jira] [Commented] (GROOVY-7647) Incorrect line information for debug

2018-11-04 Thread Eric Milles (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-7647?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16674570#comment-16674570
 ] 

Eric Milles commented on GROOVY-7647:
-

If I make the following additions to StatementWriter -- a call to 
{{mv.visitLineNumber}} immediately following each {{mv.visitLabel}} -- the end 
brace is the next step line after the condition line in case of false.  Can 
anyone confirm this is the right approach?

{code:java}
public void writeIfElse(IfStatement ifElse) {
controller.getAcg().onLineNumber(ifElse,"visitIfElse");
writeStatementLabel(ifElse);

MethodVisitor mv = controller.getMethodVisitor();

ifElse.getBooleanExpression().visit(controller.getAcg());
Label l0 = controller.getOperandStack().jump(IFEQ);

// if-else is here handled as a special version
// of a boolean expression
controller.getCompileStack().pushBooleanExpression();
ifElse.getIfBlock().visit(controller.getAcg());
controller.getCompileStack().pop();

if (ifElse.getElseBlock()==EmptyStatement.INSTANCE) {
mv.visitLabel(l0);
// GRECLIPSE add
mv.visitLineNumber(ifElse.getLastLineNumber(), l0);
// GRECLIPSE end
} else {
Label l1 = new Label();
mv.visitJumpInsn(GOTO, l1);
mv.visitLabel(l0);
// GRECLIPSE add
mv.visitLineNumber(ifElse.getIfBlock().getLastLineNumber(), l0);
// GRECLIPSE end

controller.getCompileStack().pushBooleanExpression();
ifElse.getElseBlock().visit(controller.getAcg());
controller.getCompileStack().pop();

mv.visitLabel(l1);
// GRECLIPSE add
mv.visitLineNumber(ifElse.getLastLineNumber(), l1);
// GRECLIPSE end
} 
}
{code}

> Incorrect line information for debug
> 
>
> Key: GROOVY-7647
> URL: https://issues.apache.org/jira/browse/GROOVY-7647
> Project: Groovy
>  Issue Type: Bug
>  Components: class generator, Compiler
>Affects Versions: 2.4.5
>Reporter: Egor Ushakov
>Priority: Major
>
> Try to debug the code:
> {code}
> boolean boolVar = false; //line 15
> if (boolVar) { // set breakpoint here and step over //line 16
> print "ok"; //line 17
> } // end of file //line 18
> {code}
> It steps over to the line with print even though it is not executed.
> Seems that compiler does not generate line info for the last block (line 18):
> {code}
>  public java.lang.Object run();
> descriptor: ()Ljava/lang/Object;
> flags: ACC_PUBLIC
> Code:
>   stack=3, locals=3, args_size=1
>  0: invokestatic  #18 // Method 
> $getCallSiteArray:()[Lorg/codehaus/groovy/runtime/callsite/CallSite;
>  3: astore_1
>  4: iconst_0
>  5: istore_2
>  6: iload_2
>  7: pop
>  8: iload_2
>  9: ifeq  28
> 12: aload_1
> 13: ldc   #41 // int 1
> 15: aaload
> 16: aload_0
> 17: ldc   #43 // String ok
> 19: invokeinterface #47,  3   // InterfaceMethod 
> org/codehaus/groovy/runtime/callsite/CallSite.callCurrent:(Lgroovy/lang/GroovyObject;Ljava/lang/Object;)Ljava/lang/Object;
> 24: areturn
> 25: goto  30
> 28: aconst_null
> 29: areturn
> 30: aconst_null
> 31: areturn
>   LocalVariableTable:
> Start  Length  Slot  Name   Signature
> 0  30 0  this   Lsdfklajsdlkjas/test;
> 6  24 2 boolVar   Z
>   LineNumberTable:
> line 15: 4
> line 16: 8
> line 17: 12
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Comment Edited] (GROOVY-4063) Debugger Step Into doesn't work in Groovy-compiled classes when stepping filters are applied

2018-11-04 Thread Eric Milles (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-4063?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16674555#comment-16674555
 ] 

Eric Milles edited comment on GROOVY-4063 at 11/4/18 11:29 PM:
---

GROOVY-8742 seems related.  Line number information in class file is the reason 
the breakpoint does not hit.  When compiling without "indy", there is a 
fast-path conditional added and this generates two paths through the code and 
both receive line number information for the visible source.  This confuses the 
debugger.


was (Author: emilles):
GROOVY-8472 seems related.  Line number information in class file is the reason 
the breakpoint does not hit.  When compiling without "indy", there is a 
fast-path conditional added and this generates two paths through the code and 
both receive line number information for the visible source.  This confuses the 
debugger.

> Debugger Step Into doesn't work in Groovy-compiled classes when stepping 
> filters are applied
> 
>
> Key: GROOVY-4063
> URL: https://issues.apache.org/jira/browse/GROOVY-4063
> Project: Groovy
>  Issue Type: Bug
>  Components: class generator
>Affects Versions: 1.6.7, 1.7.0
>Reporter: Peter Gromov
>Priority: Major
> Attachments: debug-test.tar.gz
>
>
> Reproduced in IntelliJ IDEA 9, GroovyEclipse 2.0 and JSWAT 4.5, therefore the 
> problem is probably in Groovy itself.
> Stepping class exclusion filters are set to ignore everything from Java and 
> Groovy internals: groovy.*,org.codehaus.groovy.*,java.*,sun.*,org.apache.*
> {code}
> class Some {
>   static void meth() {
>   println "Method is called."
>   }
>   public static void main(String[] args) {
>   meth()  // breakpoint here
>   }
> }
> {code}
> Run to the breakpoint in 'main' method. 'Step into'. There's a very long 
> pause (presumably JVM applies the filters to all the underlying groovy 
> internals), the message is printed and the program exits. Breakpoint is not 
> hit.
> Now put the breakpoint to the 'println' line in 'meth' and repeat stepping 
> into. This time the debugger stops at it.
> Stepping worked in earlier Groovy versions (e.g. 1.5) and stopped in 1.6. 
> Therefore I suspect it to be somehow mystically related to class generation 
> (or the libraries used, whatever)



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Commented] (GROOVY-4063) Debugger Step Into doesn't work in Groovy-compiled classes when stepping filters are applied

2018-11-04 Thread Eric Milles (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-4063?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16674555#comment-16674555
 ] 

Eric Milles commented on GROOVY-4063:
-

GROOVY-8472 seems related.  Line number information in class file is the reason 
the breakpoint does not hit.  When compiling without "indy", there is a 
fast-path conditional added and this generates two paths through the code and 
both receive line number information for the visible source.  This confuses the 
debugger.

> Debugger Step Into doesn't work in Groovy-compiled classes when stepping 
> filters are applied
> 
>
> Key: GROOVY-4063
> URL: https://issues.apache.org/jira/browse/GROOVY-4063
> Project: Groovy
>  Issue Type: Bug
>  Components: class generator
>Affects Versions: 1.6.7, 1.7.0
>Reporter: Peter Gromov
>Priority: Major
> Attachments: debug-test.tar.gz
>
>
> Reproduced in IntelliJ IDEA 9, GroovyEclipse 2.0 and JSWAT 4.5, therefore the 
> problem is probably in Groovy itself.
> Stepping class exclusion filters are set to ignore everything from Java and 
> Groovy internals: groovy.*,org.codehaus.groovy.*,java.*,sun.*,org.apache.*
> {code}
> class Some {
>   static void meth() {
>   println "Method is called."
>   }
>   public static void main(String[] args) {
>   meth()  // breakpoint here
>   }
> }
> {code}
> Run to the breakpoint in 'main' method. 'Step into'. There's a very long 
> pause (presumably JVM applies the filters to all the underlying groovy 
> internals), the message is printed and the program exits. Breakpoint is not 
> hit.
> Now put the breakpoint to the 'println' line in 'meth' and repeat stepping 
> into. This time the debugger stops at it.
> Stepping worked in earlier Groovy versions (e.g. 1.5) and stopped in 1.6. 
> Therefore I suspect it to be somehow mystically related to class generation 
> (or the libraries used, whatever)



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Commented] (GROOVY-7647) Incorrect line information for debug

2018-11-04 Thread Eric Milles (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-7647?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16674552#comment-16674552
 ] 

Eric Milles commented on GROOVY-7647:
-

I have seen the same issue for methods that rely on default return null and 
sometimes for try/catch constructions as well.
{code:groovy}
def m(boolean condition) {
  if (condition) {
"something"
  }
}
{code}

Debugger moves from if line to "something" line for true or false.  This is 
because the last line number information for the method is for the "something" 
line.  There is nothing for either of the closing braces, so the debugger 
associates all the bytecodes to the something line.

> Incorrect line information for debug
> 
>
> Key: GROOVY-7647
> URL: https://issues.apache.org/jira/browse/GROOVY-7647
> Project: Groovy
>  Issue Type: Bug
>  Components: class generator, Compiler
>Affects Versions: 2.4.5
>Reporter: Egor Ushakov
>Priority: Major
>
> Try to debug the code:
> {code}
> boolean boolVar = false; //line 15
> if (boolVar) { // set breakpoint here and step over //line 16
> print "ok"; //line 17
> } // end of file //line 18
> {code}
> It steps over to the line with print even though it is not executed.
> Seems that compiler does not generate line info for the last block (line 18):
> {code}
>  public java.lang.Object run();
> descriptor: ()Ljava/lang/Object;
> flags: ACC_PUBLIC
> Code:
>   stack=3, locals=3, args_size=1
>  0: invokestatic  #18 // Method 
> $getCallSiteArray:()[Lorg/codehaus/groovy/runtime/callsite/CallSite;
>  3: astore_1
>  4: iconst_0
>  5: istore_2
>  6: iload_2
>  7: pop
>  8: iload_2
>  9: ifeq  28
> 12: aload_1
> 13: ldc   #41 // int 1
> 15: aaload
> 16: aload_0
> 17: ldc   #43 // String ok
> 19: invokeinterface #47,  3   // InterfaceMethod 
> org/codehaus/groovy/runtime/callsite/CallSite.callCurrent:(Lgroovy/lang/GroovyObject;Ljava/lang/Object;)Ljava/lang/Object;
> 24: areturn
> 25: goto  30
> 28: aconst_null
> 29: areturn
> 30: aconst_null
> 31: areturn
>   LocalVariableTable:
> Start  Length  Slot  Name   Signature
> 0  30 0  this   Lsdfklajsdlkjas/test;
> 6  24 2 boolVar   Z
>   LineNumberTable:
> line 15: 4
> line 16: 8
> line 17: 12
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Commented] (GROOVY-7975) Use of static final field in an annotation element causes compile errors

2018-11-04 Thread ASF GitHub Bot (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-7975?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16674410#comment-16674410
 ] 

ASF GitHub Bot commented on GROOVY-7975:


Github user danielsun1106 commented on a diff in the pull request:

https://github.com/apache/groovy/pull/819#discussion_r230589376
  
--- Diff: src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java ---
@@ -18,20 +18,205 @@
  */
 package org.apache.groovy.ast.tools;
 
+import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.FieldNode;
+import org.codehaus.groovy.ast.expr.BinaryExpression;
 import org.codehaus.groovy.ast.expr.ClassExpression;
 import org.codehaus.groovy.ast.expr.ConstantExpression;
 import org.codehaus.groovy.ast.expr.Expression;
 import org.codehaus.groovy.ast.expr.ListExpression;
 import org.codehaus.groovy.ast.expr.PropertyExpression;
+import org.codehaus.groovy.ast.expr.VariableExpression;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+
+import static org.codehaus.groovy.syntax.Types.DIVIDE;
+import static org.codehaus.groovy.syntax.Types.MINUS;
+import static org.codehaus.groovy.syntax.Types.MULTIPLY;
+import static org.codehaus.groovy.syntax.Types.PLUS;
 
 public class ExpressionUtils {
+private static ArrayList handledTypes = new 
ArrayList();
+
 private ExpressionUtils() {
 
 }
 
-// resolve constant-looking expressions statically (do here as gets 
transformed away later)
+static {
+handledTypes.add(PLUS);
+handledTypes.add(MINUS);
+handledTypes.add(MULTIPLY);
+handledTypes.add(DIVIDE);
+}
+
+public static ConstantExpression 
transformBinaryConstantExpression(BinaryExpression be, ClassNode targetType) {
+if (isTypeOrArrayOfType(targetType, ClassHelper.STRING_TYPE, 
false)) {
+if (be.getOperation().getType() == PLUS) {
+Expression left = 
transformInlineConstants(be.getLeftExpression(), targetType);
+Expression right = 
transformInlineConstants(be.getRightExpression(), targetType);
+if (left instanceof ConstantExpression && right instanceof 
ConstantExpression) {
+ConstantExpression newExp = new 
ConstantExpression((String) ((ConstantExpression) left).getValue() +
+((ConstantExpression) right).getValue());
+newExp.setSourcePosition(be);
+return newExp;
+}
+}
+} else if (isTypeOrArrayOfType(targetType, 
ClassHelper.Integer_TYPE, false) || isTypeOrArrayOfType(targetType, 
ClassHelper.int_TYPE, false)) {
+int type = be.getOperation().getType();
+if (handledTypes.contains(type)) {
+Expression left = 
transformInlineConstants(be.getLeftExpression(), targetType);
+Expression right = 
transformInlineConstants(be.getRightExpression(), targetType);
+if (left instanceof ConstantExpression && right instanceof 
ConstantExpression) {
+Integer newVal = null;
+switch(type) {
+case PLUS:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() +
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+case MINUS:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() -
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+case MULTIPLY:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() *
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+case DIVIDE:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() /
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+}
+if (newVal != null) {
+ConstantExpression newExp = new 
ConstantExpression(newVal, true);
+newExp.setSourcePosition(be);
+return newExp;
+}
+}
+}
+} else if (isTypeOrArrayOfType(targetType, 

[GitHub] groovy pull request #819: GROOVY-7975/GROOVY-3278/GROOVY-7854: improved acce...

2018-11-04 Thread danielsun1106
Github user danielsun1106 commented on a diff in the pull request:

https://github.com/apache/groovy/pull/819#discussion_r230589376
  
--- Diff: src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java ---
@@ -18,20 +18,205 @@
  */
 package org.apache.groovy.ast.tools;
 
+import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.FieldNode;
+import org.codehaus.groovy.ast.expr.BinaryExpression;
 import org.codehaus.groovy.ast.expr.ClassExpression;
 import org.codehaus.groovy.ast.expr.ConstantExpression;
 import org.codehaus.groovy.ast.expr.Expression;
 import org.codehaus.groovy.ast.expr.ListExpression;
 import org.codehaus.groovy.ast.expr.PropertyExpression;
+import org.codehaus.groovy.ast.expr.VariableExpression;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+
+import static org.codehaus.groovy.syntax.Types.DIVIDE;
+import static org.codehaus.groovy.syntax.Types.MINUS;
+import static org.codehaus.groovy.syntax.Types.MULTIPLY;
+import static org.codehaus.groovy.syntax.Types.PLUS;
 
 public class ExpressionUtils {
+private static ArrayList handledTypes = new 
ArrayList();
+
 private ExpressionUtils() {
 
 }
 
-// resolve constant-looking expressions statically (do here as gets 
transformed away later)
+static {
+handledTypes.add(PLUS);
+handledTypes.add(MINUS);
+handledTypes.add(MULTIPLY);
+handledTypes.add(DIVIDE);
+}
+
+public static ConstantExpression 
transformBinaryConstantExpression(BinaryExpression be, ClassNode targetType) {
+if (isTypeOrArrayOfType(targetType, ClassHelper.STRING_TYPE, 
false)) {
+if (be.getOperation().getType() == PLUS) {
+Expression left = 
transformInlineConstants(be.getLeftExpression(), targetType);
+Expression right = 
transformInlineConstants(be.getRightExpression(), targetType);
+if (left instanceof ConstantExpression && right instanceof 
ConstantExpression) {
+ConstantExpression newExp = new 
ConstantExpression((String) ((ConstantExpression) left).getValue() +
+((ConstantExpression) right).getValue());
+newExp.setSourcePosition(be);
+return newExp;
+}
+}
+} else if (isTypeOrArrayOfType(targetType, 
ClassHelper.Integer_TYPE, false) || isTypeOrArrayOfType(targetType, 
ClassHelper.int_TYPE, false)) {
+int type = be.getOperation().getType();
+if (handledTypes.contains(type)) {
+Expression left = 
transformInlineConstants(be.getLeftExpression(), targetType);
+Expression right = 
transformInlineConstants(be.getRightExpression(), targetType);
+if (left instanceof ConstantExpression && right instanceof 
ConstantExpression) {
+Integer newVal = null;
+switch(type) {
+case PLUS:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() +
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+case MINUS:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() -
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+case MULTIPLY:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() *
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+case DIVIDE:
+newVal = (Integer) ((ConstantExpression) 
left).getValue() /
+(Integer) ((ConstantExpression) 
right).getValue();
+break;
+}
+if (newVal != null) {
+ConstantExpression newExp = new 
ConstantExpression(newVal, true);
+newExp.setSourcePosition(be);
+return newExp;
+}
+}
+}
+} else if (isTypeOrArrayOfType(targetType, 
ClassHelper.Double_TYPE, false) || isTypeOrArrayOfType(targetType, 
ClassHelper.double_TYPE, false)) {
--- End diff --

looks like some template code, which is duplicated now. It's better to 
refactor it IMO


---


[jira] [Commented] (GROOVY-7975) Use of static final field in an annotation element causes compile errors

2018-11-04 Thread ASF GitHub Bot (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-7975?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16674366#comment-16674366
 ] 

ASF GitHub Bot commented on GROOVY-7975:


GitHub user paulk-asert opened a pull request:

https://github.com/apache/groovy/pull/819

GROOVY-7975/GROOVY-3278/GROOVY-7854: improved accessing of constants …

…for annotation attributes

You can merge this pull request into a Git repository by running:

$ git pull https://github.com/paulk-asert/groovy annotationConstantFixes

Alternatively you can review and apply these changes as the patch at:

https://github.com/apache/groovy/pull/819.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

This closes #819


commit 236e4d5c06cec60840209e0b3c83512a90badfda
Author: Paul King 
Date:   2018-11-04T10:10:43Z

GROOVY-7975/GROOVY-3278/GROOVY-7854: improved accessing of constants for 
annotation attributes




> Use of static final field in an annotation element causes compile errors
> 
>
> Key: GROOVY-7975
> URL: https://issues.apache.org/jira/browse/GROOVY-7975
> Project: Groovy
>  Issue Type: Bug
>  Components: Compiler
>Affects Versions: 2.4.7
>Reporter: Eric Milles
>Priority: Major
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> Using a class constant (static final field) in an annotation causes compile 
> errors.  This works in Java and the fix is pretty small.
> Ex:
> {code}
> class C {
> public static final String VALUE = 'rawtypes'
> @SuppressWarnings(VALUE)
> def method() {
> }
> }
> {code}
> This is a bit contrived to be concise.  But we have examples in our code 
> where Callable impls are tagged with a name, which is defined as a static 
> constant on each class.
> The fix appears to be pretty minor.  In 
> ResolveVisitor.transformInlineConstants, a case for VariableExpression does 
> the trick for me.
> {code}
> } else if (exp instanceof VariableExpression) {
> VariableExpression ve = (VariableExpression) exp;
> if (ve.getAccessedVariable() instanceof FieldNode) {
> FieldNode fn = (FieldNode) ve.getAccessedVariable();
> if (!fn.isEnum() && fn.isStatic() && fn.isFinal() &&
> fn.getInitialValueExpression() instanceof 
> ConstantExpression) {
> return fn.getInitialValueExpression();
> }
> }
> 
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[GitHub] groovy pull request #819: GROOVY-7975/GROOVY-3278/GROOVY-7854: improved acce...

2018-11-04 Thread paulk-asert
GitHub user paulk-asert opened a pull request:

https://github.com/apache/groovy/pull/819

GROOVY-7975/GROOVY-3278/GROOVY-7854: improved accessing of constants …

…for annotation attributes

You can merge this pull request into a Git repository by running:

$ git pull https://github.com/paulk-asert/groovy annotationConstantFixes

Alternatively you can review and apply these changes as the patch at:

https://github.com/apache/groovy/pull/819.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

This closes #819


commit 236e4d5c06cec60840209e0b3c83512a90badfda
Author: Paul King 
Date:   2018-11-04T10:10:43Z

GROOVY-7975/GROOVY-3278/GROOVY-7854: improved accessing of constants for 
annotation attributes




---