Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/main/java/groovy/transform/Pure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 groovy.transform;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Mark the return value of "pure" method only comes from expressions involving constants or other pure methods
*
* @since 4.0.0
*/
@java.lang.annotation.Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Pure {
}
19 changes: 16 additions & 3 deletions src/main/java/org/codehaus/groovy/runtime/GStringImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@

import groovy.lang.GString;
import groovy.lang.GroovyObject;
import groovy.transform.Pure;
import org.apache.groovy.ast.tools.ImmutablePropertyUtils;

import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Locale;

Expand Down Expand Up @@ -56,7 +58,7 @@ public class GStringImpl extends GString {
* @param strings the string parts
*/
public GStringImpl(Object[] values, String[] strings) {
this(values, strings, checkValuesImmutable(values), null, false);
this(values, strings, checkValuesStringConstant(values), null, false);
}

/**
Expand Down Expand Up @@ -351,15 +353,26 @@ public String toString() {
return str;
}

private static boolean checkValuesImmutable(Object[] values) {
private static boolean checkValuesStringConstant(Object[] values) {
for (Object value : values) {
if (null == value) continue;
if (!(ImmutablePropertyUtils.builtinOrMarkedImmutableClass(value.getClass())
if (!(toStringMarkedPure(value.getClass())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably swap the order of this clause and the next since this one is probably less common than next line.

|| ImmutablePropertyUtils.builtinOrMarkedImmutableClass(value.getClass())
|| (value instanceof GStringImpl && ((GStringImpl) value).cacheable))) {
return false;
}
}

return true;
}

private static boolean toStringMarkedPure(Class<?> clazz) {
Method toStringMethod;
try {
toStringMethod = clazz.getMethod("toString");
} catch (NoSuchMethodException | SecurityException e) {
return false;
}
return toStringMethod.isAnnotationPresent(Pure.class);
}
}
31 changes: 31 additions & 0 deletions src/test/groovy/GStringTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package groovy

import groovy.test.GroovyTestCase
import groovy.transform.Pure

class GStringTest extends GroovyTestCase {

Expand Down Expand Up @@ -646,6 +647,10 @@ class GStringTest extends GroovyTestCase {
def gstr14 = "a${new JcipImmutableValue()}b"
assert 'a234b' == gstr14
assert gstr14.toString() === gstr14.toString()

def gstr15 = "a${new PureValue()}b"
assert 'a345b' == gstr15
assert gstr15.toString() === gstr15.toString()
}

void testGStringLiteral2() {
Expand All @@ -669,6 +674,27 @@ class GStringTest extends GroovyTestCase {
'''
}

// GROOVY-9946
void testGStringLiteral3() {
assertScript '''
final class Test {
private static final class Item {
private int toStringInvocationCount = 0
@Override
@groovy.transform.Pure
synchronized String toString() {
toStringInvocationCount++
return "item"
}
}
static void main(String[] args) {
Item item = new Item()
new StringBuilder().append("item: ${item}")
assert 1 == item.toStringInvocationCount
}
}
'''
}

@groovy.transform.Immutable
static class GroovyImmutableValue {
Expand All @@ -682,4 +708,9 @@ class GStringTest extends GroovyTestCase {
String toString() { v }
}

static final class PureValue {
private final String v = '345'
@Pure
String toString() { v }
}
}