GROOVY-6229: Some minor changes required regarding unary overloading operators and numbers (only covers the "println 1.unaryPlus()" case)
diff --git a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 2535c16..549c021 100644
--- a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -11183,6 +11183,18 @@
return NumberMath.unaryMinus(left);
}
+ /**
+ * Returns the number, effectively being a noop for numbers.
+ * Operator overloaded form of the '+' operator when it preceeds
+ * a single operand, i.e. <code>+10</code>
+ *
+ * @param left a Number
+ * @return the number
+ * @since 2.2.0
+ */
+ public static Number unaryPlus(Number left) {
+ return NumberMath.unaryPlus(left);
+ }
/**
* Executes the closure this many times, starting from zero. The current
diff --git a/src/main/org/codehaus/groovy/runtime/typehandling/BigDecimalMath.java b/src/main/org/codehaus/groovy/runtime/typehandling/BigDecimalMath.java
index a777924..4ed11a4 100644
--- a/src/main/org/codehaus/groovy/runtime/typehandling/BigDecimalMath.java
+++ b/src/main/org/codehaus/groovy/runtime/typehandling/BigDecimalMath.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2010 the original author or authors.
+ * Copyright 2003-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -77,4 +77,8 @@
protected Number unaryMinusImpl(Number left) {
return toBigDecimal(left).negate();
}
+
+ protected Number unaryPlusImpl(Number left) {
+ return toBigDecimal(left);
+ }
}
diff --git a/src/main/org/codehaus/groovy/runtime/typehandling/BigIntegerMath.java b/src/main/org/codehaus/groovy/runtime/typehandling/BigIntegerMath.java
index d652153..1217188 100644
--- a/src/main/org/codehaus/groovy/runtime/typehandling/BigIntegerMath.java
+++ b/src/main/org/codehaus/groovy/runtime/typehandling/BigIntegerMath.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2007 the original author or authors.
+ * Copyright 2003-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -62,6 +62,10 @@
return toBigInteger(left).negate();
}
+ protected Number unaryPlusImpl(Number left) {
+ return toBigInteger(left);
+ }
+
protected Number bitwiseNegateImpl(Number left) {
return toBigInteger(left).not();
}
diff --git a/src/main/org/codehaus/groovy/runtime/typehandling/FloatingPointMath.java b/src/main/org/codehaus/groovy/runtime/typehandling/FloatingPointMath.java
index 73e16ff..c6d1423 100644
--- a/src/main/org/codehaus/groovy/runtime/typehandling/FloatingPointMath.java
+++ b/src/main/org/codehaus/groovy/runtime/typehandling/FloatingPointMath.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2007 the original author or authors.
+ * Copyright 2003-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -58,4 +58,8 @@
protected Number unaryMinusImpl(Number left) {
return new Double(-left.doubleValue());
}
+
+ protected Number unaryPlusImpl(Number left) {
+ return new Double(left.doubleValue());
+ }
}
diff --git a/src/main/org/codehaus/groovy/runtime/typehandling/IntegerMath.java b/src/main/org/codehaus/groovy/runtime/typehandling/IntegerMath.java
index 5c5ec7f..5b30af8 100644
--- a/src/main/org/codehaus/groovy/runtime/typehandling/IntegerMath.java
+++ b/src/main/org/codehaus/groovy/runtime/typehandling/IntegerMath.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2007 the original author or authors.
+ * Copyright 2003-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -76,6 +76,10 @@
return Integer.valueOf(-left.intValue());
}
+ protected Number unaryPlusImpl(Number left) {
+ return Integer.valueOf(left.intValue());
+ }
+
protected Number bitwiseNegateImpl(Number left) {
return Integer.valueOf(~left.intValue());
}
diff --git a/src/main/org/codehaus/groovy/runtime/typehandling/LongMath.java b/src/main/org/codehaus/groovy/runtime/typehandling/LongMath.java
index 39435f8..bf68174 100644
--- a/src/main/org/codehaus/groovy/runtime/typehandling/LongMath.java
+++ b/src/main/org/codehaus/groovy/runtime/typehandling/LongMath.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2007 the original author or authors.
+ * Copyright 2003-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -63,7 +63,11 @@
protected Number unaryMinusImpl(Number left) {
return Long.valueOf(-left.longValue());
}
-
+
+ protected Number unaryPlusImpl(Number left) {
+ return Long.valueOf(left.longValue());
+ }
+
protected Number bitwiseNegateImpl(Number left) {
return Long.valueOf(~left.longValue());
}
diff --git a/src/main/org/codehaus/groovy/runtime/typehandling/NumberMath.java b/src/main/org/codehaus/groovy/runtime/typehandling/NumberMath.java
index bab8add..ea7b413 100644
--- a/src/main/org/codehaus/groovy/runtime/typehandling/NumberMath.java
+++ b/src/main/org/codehaus/groovy/runtime/typehandling/NumberMath.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2007 the original author or authors.
+ * Copyright 2003-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -132,6 +132,10 @@
return getMath(left).unaryMinusImpl(left);
}
+ public static Number unaryPlus(Number left) {
+ return getMath(left).unaryPlusImpl(left);
+ }
+
public static boolean isFloatingPoint(Number number) {
return number instanceof Double || number instanceof Float;
}
@@ -216,6 +220,7 @@
public abstract Number divideImpl(Number left, Number right);
public abstract int compareToImpl(Number left, Number right);
protected abstract Number unaryMinusImpl(Number left);
+ protected abstract Number unaryPlusImpl(Number left);
protected Number orImpl(Number left, Number right) {
diff --git a/src/test/groovy/operator/IntegerOperatorsTest.groovy b/src/test/groovy/operator/IntegerOperatorsTest.groovy
index 21750d7..fa0fac0 100644
--- a/src/test/groovy/operator/IntegerOperatorsTest.groovy
+++ b/src/test/groovy/operator/IntegerOperatorsTest.groovy
@@ -5,7 +5,7 @@
def x
def y
def z
-
+
void testPlus() {
x = 2 + 2
assert x == 4
@@ -16,7 +16,13 @@
z = y + x + 1 + 2
assert z == 12
}
-
+
+ void testUnaryPlus() {
+ x = 3
+ y = +x
+ assert y == 3
+ }
+
void testCharacterPlus() {
Character c1 = 1
Character c2 = 2
@@ -53,7 +59,13 @@
y = x - 1
assert y == 3
}
-
+
+ void testUnaryMinus() {
+ x = 3
+ y = -x
+ assert y == -3
+ }
+
void testCharacterMinus() {
Character c1 = 1
Character c2 = 2
diff --git a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/completion/ReflectionCompletorTest.groovy b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/completion/ReflectionCompletorTest.groovy
index 4dc0646..68e17ca 100644
--- a/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/completion/ReflectionCompletorTest.groovy
+++ b/subprojects/groovy-groovysh/src/test/groovy/org/codehaus/groovy/tools/shell/completion/ReflectionCompletorTest.groovy
@@ -17,7 +17,6 @@
package org.codehaus.groovy.tools.shell.completion
import org.codehaus.groovy.antlr.parser.GroovyLexer
-import org.codehaus.groovy.tools.shell.completion.ReflectionCompletor
import org.codehaus.groovy.tools.shell.Interpreter
import static org.codehaus.groovy.tools.shell.completion.TokenUtilTest.tokenList
@@ -69,9 +68,9 @@
assertTrue(result.toString(), "notify()" in result)
assertTrue("bitCount(" in result)
result = ReflectionCompletor.getPublicFieldsAndMethods(3, "una")
- assertEquals(["unaryMinus()"], result)
+ assertEquals(["unaryMinus()", "unaryPlus()"], result)
result = ReflectionCompletor.getPublicFieldsAndMethods(Integer, "una")
- assertEquals(["unaryMinus()"], result)
+ assertEquals(["unaryMinus()", "unaryPlus()"], result)
result = ReflectionCompletor.getPublicFieldsAndMethods(Integer, "MA")
assertEquals(["MAX_VALUE"], result)
result = ReflectionCompletor.getPublicFieldsAndMethods(Integer, "getI")