[GitHub] groovy pull request #432: GROOVY-7909 Calling parents method from trait usin...
Github user jwagenleitner commented on a diff in the pull request: https://github.com/apache/groovy/pull/432#discussion_r82406821 --- Diff: src/test/groovy/bugs/Groovy7909Bug.groovy --- @@ -0,0 +1,57 @@ +package groovy.bugs --- End diff -- Could you add the license header to this file, should be able to grab it from another file in the same package. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[jira] [Commented] (GROOVY-7909) Calling parents method from trait using Parent.super.method() fail depending on trait declaration order
[ https://issues.apache.org/jira/browse/GROOVY-7909?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=1320#comment-1320 ] ASF GitHub Bot commented on GROOVY-7909: Github user jwagenleitner commented on a diff in the pull request: https://github.com/apache/groovy/pull/432#discussion_r82406821 --- Diff: src/test/groovy/bugs/Groovy7909Bug.groovy --- @@ -0,0 +1,57 @@ +package groovy.bugs --- End diff -- Could you add the license header to this file, should be able to grab it from another file in the same package. > Calling parents method from trait using Parent.super.method() fail depending > on trait declaration order > --- > > Key: GROOVY-7909 > URL: https://issues.apache.org/jira/browse/GROOVY-7909 > Project: Groovy > Issue Type: Bug > Components: Compiler >Affects Versions: 2.4.7 > Environment: Grails 3.1.9 >Reporter: Sébastien Collin >Priority: Critical > > I'm using trait for multiple inheritance and I faced an issue when compiling > my project (using Grails 3.1.9, so with the gradle task CompileGroovy). > My traits are sharing a common method ({{def method()}}) and I'm referencing > parent's version through ParentClassName.super.method() > I'm seeing this error: > {quote} > > BUG! exception in phase 'semantic analysis' in source unit 'xxx.groovy' > > ClassNode#getTypeClass for YYY is called before the type class is set > {quote} > When testing different cases it seems that this problem appears because > classes are not compiled in the right order. > For example, a script with > {code} > trait Three implements One, Two { > def postMake() { > One.super.postMake() > Two.super.postMake() > println "Three" > } > } > trait One { > def postMake() { println "One"} > } > trait Two { > def postMake() { println "Two"} > } > class Four implements Three { > def make() { > Three.super.postMake() > println "All done?" > } > } > Four f = new Four() > f.make() > {code} > will fail, whereas putting Three method after One and Two will succeed. > Beside, keeping this trait's declaration order but changing method names to > be unique (and removing {{XXX.super}}) will compile and execute correctly. > I hope I'm clear enough... -- This message was sent by Atlassian JIRA (v6.3.4#6332)
[jira] [Commented] (GROOVY-7951) MethodCallExpression.transformExpression does not copy generic types
[ https://issues.apache.org/jira/browse/GROOVY-7951?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=1271#comment-1271 ] ASF GitHub Bot commented on GROOVY-7951: GitHub user jwagenleitner opened a pull request: https://github.com/apache/groovy/pull/441 GROOVY-7951: MethodCallExpression.transformExpression does not copy generic types PR created from patch supplied in [GROOVY-7951](https://issues.apache.org/jira/browse/GROOVY-7951). It seems like a reasonable change to make, though I did have trouble thinking up a way to test it that would be meaningful. If anyone can nudge me in the right direction I can try to write up a test if you think it's warranted in this case. I considered whether `genericsTypes.clone()` should be used, but it didn't seem to be common and most uses of the array seem to be read only. You can merge this pull request into a Git repository by running: $ git pull https://github.com/jwagenleitner/groovy GROOVY-7951 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/groovy/pull/441.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 #441 commit 76f9cdabd6e48baf5721b6b6dbff21a109f164a7 Author: John Wagenleitner Date: 2016-10-04T22:29:59Z GROOVY-7951: MethodCallExpression.transformExpression does not copy generic types > MethodCallExpression.transformExpression does not copy generic types > > > Key: GROOVY-7951 > URL: https://issues.apache.org/jira/browse/GROOVY-7951 > Project: Groovy > Issue Type: Bug > Components: ast builder >Affects Versions: 2.4.7 >Reporter: Eric Milles >Priority: Minor > > org.codehaus.groovy.ast.expr.MethodCallExpression.transformExpression(ExpressionTransformer) > is not copying generics info. Downstream visitors are unable to check > generics on static calls like Collections.emptyList(). > My patched version: > {code:java} > public Expression transformExpression(ExpressionTransformer transformer) { > MethodCallExpression answer = > new MethodCallExpression(transformer.transform(objectExpression), > transformer.transform(method), transformer.transform(arguments)); > answer.setSafe(safe); > answer.setSpreadSafe(spreadSafe); > answer.setImplicitThis(implicitThis); > // GRECLIPSE add > answer.setGenericsTypes(genericsTypes); > // GRECLIPSE end > answer.setMethodTarget(target); > answer.setSourcePosition(this); > answer.copyNodeMetaData(this); > return answer; > } > {code} -- This message was sent by Atlassian JIRA (v6.3.4#6332)
[GitHub] groovy pull request #441: GROOVY-7951: MethodCallExpression.transformExpress...
GitHub user jwagenleitner opened a pull request: https://github.com/apache/groovy/pull/441 GROOVY-7951: MethodCallExpression.transformExpression does not copy generic types PR created from patch supplied in [GROOVY-7951](https://issues.apache.org/jira/browse/GROOVY-7951). It seems like a reasonable change to make, though I did have trouble thinking up a way to test it that would be meaningful. If anyone can nudge me in the right direction I can try to write up a test if you think it's warranted in this case. I considered whether `genericsTypes.clone()` should be used, but it didn't seem to be common and most uses of the array seem to be read only. You can merge this pull request into a Git repository by running: $ git pull https://github.com/jwagenleitner/groovy GROOVY-7951 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/groovy/pull/441.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 #441 commit 76f9cdabd6e48baf5721b6b6dbff21a109f164a7 Author: John Wagenleitner Date: 2016-10-04T22:29:59Z GROOVY-7951: MethodCallExpression.transformExpression does not copy generic types --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user jwagenleitner commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r82402368 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -601,4 +280,966 @@ public String toString() { } } +/** + * Creates a builder for various options that can be set to alter the + * generated JSON. After setting the options a call to + * {@link Options#createGenerator()} will return a fully configured + * {@link JsonOutput.Generator} object and the {@code toJson} methods + * can be used. + * + * @return a builder for building a JsonOutput.Generator + * with the specified options set. + * @since 2.5 + */ +public static Options options() { +return new Options(); +} + +/** + * A builder used to construct a {@link JsonOutput.Generator} instance that allows + * control over the serialized JSON output. If you do not need to customize the + * output it is recommended to use the static {@code JsonOutput.toJson} methods. + * + * + * Example: + * + * def generator = groovy.json.JsonOutput.options() + * .excludeNulls() + * .dateFormat('') + * .excludeFieldsByName('bar', 'baz') + * .excludeFieldsByType(java.sql.Date) + * .createGenerator() + * + * def input = [foo: null, lastUpdated: Date.parse('-MM-dd', '2014-10-24'), + * bar: 'foo', baz: 'foo', systemDate: new java.sql.Date(new Date().getTime())] + * + * assert generator.toJson(input) == '{"lastUpdated":"2014"}' + * + * + * @since 2.5 + */ +public static class Options { + +private boolean excludeNulls; + +private boolean disableUnicodeEscaping; + +private String dateFormat = JsonOutput.JSON_DATE_FORMAT; + +private Locale dateLocale = JsonOutput.JSON_DATE_FORMAT_LOCALE; + +private TimeZone timezone = TimeZone.getTimeZone(JsonOutput.DEFAULT_TIMEZONE); + +private final Set converters = new LinkedHashSet(); + +private final Set excludedFieldNames = new HashSet(); + +private final Set> excludedFieldTypes = new HashSet>(); + +private Options() {} + +/** + * Do not serialize {@code null} values. + * + * @return a reference to this {@code Options} instance + */ +public Options excludeNulls() { +excludeNulls = true; +return this; +} + +/** + * Disables the escaping of Unicode characters in JSON String values. + * + * @return a reference to this {@code Options} instance + */ +public Options disableUnicodeEscaping() { +disableUnicodeEscaping = true; +return this; +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat} and the + * date formatter will be constructed with the default locale of {@link Locale#US}. + * + * @param format date format pattern used to serialize dates + * @return a reference to this {@code Options} instance + * @exception NullPointerException if the given pattern is null + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format) { +return dateFormat(format, JsonOutput.JSON_DATE_FORMAT_LOCALE); +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat}. + * + * @param format date format pattern used to serialize dates + * @param locale the locale whose date format symbols will be used + * @return a reference to this {@code Options} instance + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format, Locale locale) { +// validate date format pattern +new SimpleDateFormat(format, locale); +dateFormat = format; +dateLocale = locale; +return this; +} + +/** + * Sets the ti
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user graemerocher commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r82400528 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -601,4 +280,966 @@ public String toString() { } } +/** + * Creates a builder for various options that can be set to alter the + * generated JSON. After setting the options a call to + * {@link Options#createGenerator()} will return a fully configured + * {@link JsonOutput.Generator} object and the {@code toJson} methods + * can be used. + * + * @return a builder for building a JsonOutput.Generator + * with the specified options set. + * @since 2.5 + */ +public static Options options() { +return new Options(); +} + +/** + * A builder used to construct a {@link JsonOutput.Generator} instance that allows + * control over the serialized JSON output. If you do not need to customize the + * output it is recommended to use the static {@code JsonOutput.toJson} methods. + * + * + * Example: + * + * def generator = groovy.json.JsonOutput.options() + * .excludeNulls() + * .dateFormat('') + * .excludeFieldsByName('bar', 'baz') + * .excludeFieldsByType(java.sql.Date) + * .createGenerator() + * + * def input = [foo: null, lastUpdated: Date.parse('-MM-dd', '2014-10-24'), + * bar: 'foo', baz: 'foo', systemDate: new java.sql.Date(new Date().getTime())] + * + * assert generator.toJson(input) == '{"lastUpdated":"2014"}' + * + * + * @since 2.5 + */ +public static class Options { + +private boolean excludeNulls; + +private boolean disableUnicodeEscaping; + +private String dateFormat = JsonOutput.JSON_DATE_FORMAT; + +private Locale dateLocale = JsonOutput.JSON_DATE_FORMAT_LOCALE; + +private TimeZone timezone = TimeZone.getTimeZone(JsonOutput.DEFAULT_TIMEZONE); + +private final Set converters = new LinkedHashSet(); + +private final Set excludedFieldNames = new HashSet(); + +private final Set> excludedFieldTypes = new HashSet>(); + +private Options() {} + +/** + * Do not serialize {@code null} values. + * + * @return a reference to this {@code Options} instance + */ +public Options excludeNulls() { +excludeNulls = true; +return this; +} + +/** + * Disables the escaping of Unicode characters in JSON String values. + * + * @return a reference to this {@code Options} instance + */ +public Options disableUnicodeEscaping() { +disableUnicodeEscaping = true; +return this; +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat} and the + * date formatter will be constructed with the default locale of {@link Locale#US}. + * + * @param format date format pattern used to serialize dates + * @return a reference to this {@code Options} instance + * @exception NullPointerException if the given pattern is null + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format) { +return dateFormat(format, JsonOutput.JSON_DATE_FORMAT_LOCALE); +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat}. + * + * @param format date format pattern used to serialize dates + * @param locale the locale whose date format symbols will be used + * @return a reference to this {@code Options} instance + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format, Locale locale) { +// validate date format pattern +new SimpleDateFormat(format, locale); +dateFormat = format; +dateLocale = locale; +return this; +} + +/** + * Sets the tim
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user jwagenleitner commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r8233 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -492,7 +171,7 @@ private static void writeIterator(Iterator iterator, CharBuf buffer) { */ public static String prettyPrint(String jsonPayload) { int indentSize = 0; -// Just a guess that the pretty view will take 20 percent more than original. +// Just a guess that the pretty view will take a 20 percent more than original. --- End diff -- Thanks for pointing that out I'll remove the `a`. I missed that when fixing up the merge conflict caused by commit 7e8211fa314526ce. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user jwagenleitner commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r82399398 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -601,4 +280,966 @@ public String toString() { } } +/** + * Creates a builder for various options that can be set to alter the + * generated JSON. After setting the options a call to + * {@link Options#createGenerator()} will return a fully configured + * {@link JsonOutput.Generator} object and the {@code toJson} methods + * can be used. + * + * @return a builder for building a JsonOutput.Generator + * with the specified options set. + * @since 2.5 + */ +public static Options options() { +return new Options(); +} + +/** + * A builder used to construct a {@link JsonOutput.Generator} instance that allows + * control over the serialized JSON output. If you do not need to customize the + * output it is recommended to use the static {@code JsonOutput.toJson} methods. + * + * + * Example: + * + * def generator = groovy.json.JsonOutput.options() + * .excludeNulls() + * .dateFormat('') + * .excludeFieldsByName('bar', 'baz') + * .excludeFieldsByType(java.sql.Date) + * .createGenerator() + * + * def input = [foo: null, lastUpdated: Date.parse('-MM-dd', '2014-10-24'), + * bar: 'foo', baz: 'foo', systemDate: new java.sql.Date(new Date().getTime())] + * + * assert generator.toJson(input) == '{"lastUpdated":"2014"}' + * + * + * @since 2.5 + */ +public static class Options { + +private boolean excludeNulls; + +private boolean disableUnicodeEscaping; + +private String dateFormat = JsonOutput.JSON_DATE_FORMAT; + +private Locale dateLocale = JsonOutput.JSON_DATE_FORMAT_LOCALE; + +private TimeZone timezone = TimeZone.getTimeZone(JsonOutput.DEFAULT_TIMEZONE); + +private final Set converters = new LinkedHashSet(); + +private final Set excludedFieldNames = new HashSet(); + +private final Set> excludedFieldTypes = new HashSet>(); + +private Options() {} + +/** + * Do not serialize {@code null} values. + * + * @return a reference to this {@code Options} instance + */ +public Options excludeNulls() { +excludeNulls = true; +return this; +} + +/** + * Disables the escaping of Unicode characters in JSON String values. + * + * @return a reference to this {@code Options} instance + */ +public Options disableUnicodeEscaping() { +disableUnicodeEscaping = true; +return this; +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat} and the + * date formatter will be constructed with the default locale of {@link Locale#US}. + * + * @param format date format pattern used to serialize dates + * @return a reference to this {@code Options} instance + * @exception NullPointerException if the given pattern is null + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format) { +return dateFormat(format, JsonOutput.JSON_DATE_FORMAT_LOCALE); +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat}. + * + * @param format date format pattern used to serialize dates + * @param locale the locale whose date format symbols will be used + * @return a reference to this {@code Options} instance + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format, Locale locale) { +// validate date format pattern +new SimpleDateFormat(format, locale); +dateFormat = format; +dateLocale = locale; +return this; +} + +/** + * Sets the ti
[GitHub] groovy pull request #439: WIP Add groovy-macro docs
Github user bsideup commented on a diff in the pull request: https://github.com/apache/groovy/pull/439#discussion_r82380664 --- Diff: src/spec/test/metaprogramming/ASTMatcherTestingTest.groovy --- @@ -0,0 +1,123 @@ +/* + * 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 metaprogramming + +import groovy.transform.CompileDynamic +import groovy.transform.CompileStatic +import org.codehaus.groovy.ast.ASTNode +import org.codehaus.groovy.ast.ClassNode +import org.codehaus.groovy.ast.MethodNode +import org.codehaus.groovy.ast.Parameter +import org.codehaus.groovy.ast.expr.BinaryExpression +import org.codehaus.groovy.ast.expr.Expression +import org.codehaus.groovy.ast.stmt.BlockStatement +import org.codehaus.groovy.control.CompilePhase +import org.codehaus.groovy.control.SourceUnit +import org.codehaus.groovy.macro.matcher.ASTMatcher +import org.codehaus.groovy.macro.transform.MacroClass +import org.codehaus.groovy.transform.AbstractASTTransformation +import org.codehaus.groovy.transform.GroovyASTTransformation +import org.codehaus.groovy.transform.GroovyASTTransformationClass + +import java.lang.annotation.ElementType +import java.lang.annotation.Retention +import java.lang.annotation.RetentionPolicy +import java.lang.annotation.Target + +import static org.codehaus.groovy.ast.ClassHelper.Integer_TYPE +import static org.codehaus.groovy.ast.tools.GeneralUtils.* + +/** + * Created by dev on 6/30/16. + */ +class ASTMatcherTestingTest extends GroovyTestCase { + +// tag::testexpression[] +void testTestingSumExpression() { +use(ASTMatcher) { // <1> +TwiceASTTransformation sample = new TwiceASTTransformation() +Expression referenceNode = macro { +a + a // <2> +}.withConstraints { // <3> +placeholder 'a' // <4> +} + +assert sample +.sumExpression +.matches(referenceNode) // <5> +} +} +// end::testexpression[] + +// tag::executiontesting[] +void testASTBehavior() { +assertScript ''' +package metaprogramming + +@Twice +class AAA { + +} + +assert new AAA().giveMeTwo(1) == 2 +''' +} +// end::executiontesting[] +} + +@Retention(RetentionPolicy.SOURCE) +@Target([ElementType.TYPE]) +@GroovyASTTransformationClass(["metaprogramming.TwiceASTTransformation"]) +@interface Twice { } + +// tag::twiceasttransformation[] +@GroovyASTTransformation(phase = CompilePhase.INSTRUCTION_SELECTION) +class TwiceASTTransformation extends AbstractASTTransformation { + +static final String VAR_X = 'x' + +@Override +void visit(ASTNode[] nodes, SourceUnit source) { +ClassNode classNode = (ClassNode) nodes[1] +MethodNode giveMeTwo = getTemplateClass(sumExpression) +.methods +.find { it.name == 'giveMeTwo' } --- End diff -- can be replaced with `getTemplateClass(sumExpression).getDeclaredMethods('giveMeTwo').first()` --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] groovy pull request #439: WIP Add groovy-macro docs
Github user bsideup commented on a diff in the pull request: https://github.com/apache/groovy/pull/439#discussion_r82379710 --- Diff: src/spec/test/metaprogramming/ASTMatcherFilteringTest.groovy --- @@ -0,0 +1,103 @@ +/* + * 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 metaprogramming + +import groovy.transform.CompileStatic +import org.codehaus.groovy.ast.ASTNode +import org.codehaus.groovy.ast.ClassCodeExpressionTransformer +import org.codehaus.groovy.ast.MethodNode +import org.codehaus.groovy.ast.expr.Expression +import org.codehaus.groovy.control.CompilePhase +import org.codehaus.groovy.control.SourceUnit +import org.codehaus.groovy.macro.matcher.ASTMatcher +import org.codehaus.groovy.transform.AbstractASTTransformation +import org.codehaus.groovy.transform.GroovyASTTransformation +import org.codehaus.groovy.transform.GroovyASTTransformationClass + +import java.lang.annotation.ElementType +import java.lang.annotation.Retention +import java.lang.annotation.RetentionPolicy +import java.lang.annotation.Target + +/** + * Created by dev on 6/30/16. + */ +class ASTMatcherFilteringTest extends GroovyTestCase { + +void testFilteringNodes() { +assertScript ''' +// tag::jokingexample[] +package metaprogramming + +class Something { +@Joking +Integer getResult() { +return 1 + 1 +} +} + +assert new Something().result == 3 +// end::jokingexample[] +''' +} +} + +// tag::jokingannotation[] +@Retention(RetentionPolicy.SOURCE) +@Target([ElementType.METHOD]) +@GroovyASTTransformationClass(["metaprogramming.JokingASTTransformation"]) +@interface Joking { } +// end::jokingannotation[] + +// tag::jokingtransformation[] +@CompileStatic +@GroovyASTTransformation(phase = CompilePhase.INSTRUCTION_SELECTION) +class JokingASTTransformation extends AbstractASTTransformation { +@Override +void visit(ASTNode[] nodes, SourceUnit source) { +MethodNode methodNode = (MethodNode) nodes[1] + +methodNode +.getCode() +.visit(new ConvertToOne(source)) // <1> +} +} +// end::jokingtransformation[] + +// tag::jokingexpressiontransformer[] +class ConvertToOne extends ClassCodeExpressionTransformer { +SourceUnit sourceUnit + +ConvertToOne(SourceUnit sourceUnit1) { --- End diff -- Minor cosmetics: `sourceUnit1` -> `sourceUnit` ? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] groovy pull request #439: WIP Add groovy-macro docs
Github user bsideup commented on a diff in the pull request: https://github.com/apache/groovy/pull/439#discussion_r82380154 --- Diff: src/spec/test/metaprogramming/ASTMatcherFilteringTest.groovy --- @@ -0,0 +1,103 @@ +/* + * 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 metaprogramming + +import groovy.transform.CompileStatic +import org.codehaus.groovy.ast.ASTNode +import org.codehaus.groovy.ast.ClassCodeExpressionTransformer +import org.codehaus.groovy.ast.MethodNode +import org.codehaus.groovy.ast.expr.Expression +import org.codehaus.groovy.control.CompilePhase +import org.codehaus.groovy.control.SourceUnit +import org.codehaus.groovy.macro.matcher.ASTMatcher +import org.codehaus.groovy.transform.AbstractASTTransformation +import org.codehaus.groovy.transform.GroovyASTTransformation +import org.codehaus.groovy.transform.GroovyASTTransformationClass + +import java.lang.annotation.ElementType +import java.lang.annotation.Retention +import java.lang.annotation.RetentionPolicy +import java.lang.annotation.Target + +/** + * Created by dev on 6/30/16. + */ +class ASTMatcherFilteringTest extends GroovyTestCase { + +void testFilteringNodes() { +assertScript ''' +// tag::jokingexample[] +package metaprogramming + +class Something { +@Joking +Integer getResult() { +return 1 + 1 +} +} + +assert new Something().result == 3 +// end::jokingexample[] +''' +} +} + +// tag::jokingannotation[] +@Retention(RetentionPolicy.SOURCE) +@Target([ElementType.METHOD]) +@GroovyASTTransformationClass(["metaprogramming.JokingASTTransformation"]) +@interface Joking { } +// end::jokingannotation[] + +// tag::jokingtransformation[] +@CompileStatic +@GroovyASTTransformation(phase = CompilePhase.INSTRUCTION_SELECTION) +class JokingASTTransformation extends AbstractASTTransformation { +@Override +void visit(ASTNode[] nodes, SourceUnit source) { +MethodNode methodNode = (MethodNode) nodes[1] + +methodNode +.getCode() +.visit(new ConvertToOne(source)) // <1> +} +} +// end::jokingtransformation[] + +// tag::jokingexpressiontransformer[] +class ConvertToOne extends ClassCodeExpressionTransformer { +SourceUnit sourceUnit + +ConvertToOne(SourceUnit sourceUnit1) { +this.sourceUnit = sourceUnit1 +} + +@Override +Expression transform(Expression exp) { +Expression ref = macro { 1 + 1 } // <1> + +if (ASTMatcher.matches(ref, exp)) { // <2> --- End diff -- Now I feel maybe we should implement `exp.matches(macro { 1 + 1 })`, or even `exp.matchesMacro { 1 + 1 }`, feels a bit more groovier :) --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] groovy pull request #439: WIP Add groovy-macro docs
Github user bsideup commented on a diff in the pull request: https://github.com/apache/groovy/pull/439#discussion_r82379792 --- Diff: src/spec/test/metaprogramming/ASTMatcherFilteringTest.groovy --- @@ -0,0 +1,103 @@ +/* + * 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 metaprogramming + +import groovy.transform.CompileStatic +import org.codehaus.groovy.ast.ASTNode +import org.codehaus.groovy.ast.ClassCodeExpressionTransformer +import org.codehaus.groovy.ast.MethodNode +import org.codehaus.groovy.ast.expr.Expression +import org.codehaus.groovy.control.CompilePhase +import org.codehaus.groovy.control.SourceUnit +import org.codehaus.groovy.macro.matcher.ASTMatcher +import org.codehaus.groovy.transform.AbstractASTTransformation +import org.codehaus.groovy.transform.GroovyASTTransformation +import org.codehaus.groovy.transform.GroovyASTTransformationClass + +import java.lang.annotation.ElementType +import java.lang.annotation.Retention +import java.lang.annotation.RetentionPolicy +import java.lang.annotation.Target + +/** + * Created by dev on 6/30/16. + */ +class ASTMatcherFilteringTest extends GroovyTestCase { + +void testFilteringNodes() { +assertScript ''' +// tag::jokingexample[] +package metaprogramming + +class Something { +@Joking +Integer getResult() { +return 1 + 1 +} +} + +assert new Something().result == 3 +// end::jokingexample[] +''' +} +} + +// tag::jokingannotation[] +@Retention(RetentionPolicy.SOURCE) +@Target([ElementType.METHOD]) +@GroovyASTTransformationClass(["metaprogramming.JokingASTTransformation"]) +@interface Joking { } +// end::jokingannotation[] + +// tag::jokingtransformation[] +@CompileStatic +@GroovyASTTransformation(phase = CompilePhase.INSTRUCTION_SELECTION) +class JokingASTTransformation extends AbstractASTTransformation { +@Override +void visit(ASTNode[] nodes, SourceUnit source) { +MethodNode methodNode = (MethodNode) nodes[1] + +methodNode +.getCode() +.visit(new ConvertToOne(source)) // <1> +} +} +// end::jokingtransformation[] + +// tag::jokingexpressiontransformer[] +class ConvertToOne extends ClassCodeExpressionTransformer { +SourceUnit sourceUnit + +ConvertToOne(SourceUnit sourceUnit1) { +this.sourceUnit = sourceUnit1 +} + +@Override +Expression transform(Expression exp) { --- End diff -- this is just brilliant :D awesome example! :+1: --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user paulk-asert commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r82374539 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -601,4 +280,966 @@ public String toString() { } } +/** + * Creates a builder for various options that can be set to alter the + * generated JSON. After setting the options a call to + * {@link Options#createGenerator()} will return a fully configured + * {@link JsonOutput.Generator} object and the {@code toJson} methods + * can be used. + * + * @return a builder for building a JsonOutput.Generator + * with the specified options set. + * @since 2.5 + */ +public static Options options() { +return new Options(); +} + +/** + * A builder used to construct a {@link JsonOutput.Generator} instance that allows + * control over the serialized JSON output. If you do not need to customize the + * output it is recommended to use the static {@code JsonOutput.toJson} methods. + * + * + * Example: + * + * def generator = groovy.json.JsonOutput.options() + * .excludeNulls() + * .dateFormat('') + * .excludeFieldsByName('bar', 'baz') + * .excludeFieldsByType(java.sql.Date) + * .createGenerator() + * + * def input = [foo: null, lastUpdated: Date.parse('-MM-dd', '2014-10-24'), + * bar: 'foo', baz: 'foo', systemDate: new java.sql.Date(new Date().getTime())] + * + * assert generator.toJson(input) == '{"lastUpdated":"2014"}' + * + * + * @since 2.5 + */ +public static class Options { + +private boolean excludeNulls; + +private boolean disableUnicodeEscaping; + +private String dateFormat = JsonOutput.JSON_DATE_FORMAT; + +private Locale dateLocale = JsonOutput.JSON_DATE_FORMAT_LOCALE; + +private TimeZone timezone = TimeZone.getTimeZone(JsonOutput.DEFAULT_TIMEZONE); + +private final Set converters = new LinkedHashSet(); + +private final Set excludedFieldNames = new HashSet(); + +private final Set> excludedFieldTypes = new HashSet>(); + +private Options() {} + +/** + * Do not serialize {@code null} values. + * + * @return a reference to this {@code Options} instance + */ +public Options excludeNulls() { +excludeNulls = true; +return this; +} + +/** + * Disables the escaping of Unicode characters in JSON String values. + * + * @return a reference to this {@code Options} instance + */ +public Options disableUnicodeEscaping() { +disableUnicodeEscaping = true; +return this; +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat} and the + * date formatter will be constructed with the default locale of {@link Locale#US}. + * + * @param format date format pattern used to serialize dates + * @return a reference to this {@code Options} instance + * @exception NullPointerException if the given pattern is null + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format) { +return dateFormat(format, JsonOutput.JSON_DATE_FORMAT_LOCALE); +} + +/** + * Sets the date format that will be used to serialize {@code Date} objects. + * This must be a valid pattern for {@link java.text.SimpleDateFormat}. + * + * @param format date format pattern used to serialize dates + * @param locale the locale whose date format symbols will be used + * @return a reference to this {@code Options} instance + * @exception IllegalArgumentException if the given pattern is invalid + */ +public Options dateFormat(String format, Locale locale) { +// validate date format pattern +new SimpleDateFormat(format, locale); +dateFormat = format; +dateLocale = locale; +return this; +} + +/** + * Sets the time
[GitHub] groovy pull request #371: Serialization options for JsonOutput
Github user paulk-asert commented on a diff in the pull request: https://github.com/apache/groovy/pull/371#discussion_r82341071 --- Diff: subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java --- @@ -492,7 +171,7 @@ private static void writeIterator(Iterator iterator, CharBuf buffer) { */ public static String prettyPrint(String jsonPayload) { int indentSize = 0; -// Just a guess that the pretty view will take 20 percent more than original. +// Just a guess that the pretty view will take a 20 percent more than original. --- End diff -- why is 'a' added in above comment? Should it be 'about' or just a spurious typo? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[jira] [Commented] (GROOVY-7956) Allow @DelegatesTo on named arguments
[ https://issues.apache.org/jira/browse/GROOVY-7956?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15554368#comment-15554368 ] Paul King commented on GROOVY-7956: --- It's also interesting to examine some of Groovy's own examples of map params, e.g. {{groovy.sql.Sql}} has several examples including {{newInstance}}: {code} def sql = Sql.newInstance( url: 'jdbc:hsqldb:mem:testDB', user: 'sa', password: '', driver: 'org.hsqldb.jdbc.JDBCDriver', cacheStatements: true, resultSetConcurrency: CONCUR_READ_ONLY ) {code} Some of the arguments are required and the method also supports an optional 'properties' named argument which can't be used in combination with some of the other arguments. The first 4 of these are picked out and determine which {{getConnection}} method will be called under the covers. The last two cause appropriate calls to the various setters of the created connection. It is interesting to think ahead for how we might get type assistance for this case in the future even if we don't implement that straight away. > Allow @DelegatesTo on named arguments > - > > Key: GROOVY-7956 > URL: https://issues.apache.org/jira/browse/GROOVY-7956 > Project: Groovy > Issue Type: New Feature > Components: GEP >Reporter: Graeme Rocher > > In order to aid static compilation for builders we have {{@DelegatesTo}} > which allows statically compiled code to know what the delegate of a closure > is. > This proposal is to allow {{@DelegatesTo}} on {{Map}} types such that IDEs > and the static compiler can resolve the target type the named arguments are > to be used on. > For example: > {code} > class Farm { > void animal(@DelegatesTo(Animal) Map arguments, > @DelegatesTo(AnimalBuilder) Closure callable) { > def animal = new Animal(arguments) > // handle closure > } > } > class Animal { String name } > {code} > The following code would then fail to compile : > {code} > def farm = new Farm() > // compilation failure, no name property on Animal > farm.animal(nam: "Dog") { > } > {code} > It would then be down to IDEs to also provide support for code completion etc. -- This message was sent by Atlassian JIRA (v6.3.4#6332)