|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package groovy.bugs |
| 20 | + |
| 21 | +import org.codehaus.groovy.control.CompilerConfiguration |
| 22 | +import org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit |
| 23 | +import org.junit.Test |
| 24 | + |
| 25 | +import static groovy.test.GroovyAssert.assertScript |
| 26 | + |
| 27 | +final class Groovy9873 { |
| 28 | + |
| 29 | + @Test |
| 30 | + void testCoerceClosure1() { |
| 31 | + assertScript ''' |
| 32 | + @Grab('io.vavr:vavr:0.10.4;transitive=false') |
| 33 | + import io.vavr.control.Try |
| 34 | + class C { } |
| 35 | + C resolve() {new C()} |
| 36 | + Try.of(this::resolve) |
| 37 | + ''' |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void testCoerceClosure2() { |
| 42 | + def config = new CompilerConfiguration().tap { |
| 43 | + jointCompilationOptions = [memStub: true] |
| 44 | + targetDirectory = File.createTempDir() |
| 45 | + } |
| 46 | + File parentDir = File.createTempDir() |
| 47 | + try { |
| 48 | + def c = new File(parentDir, 'C.groovy') |
| 49 | + c.write ''' |
| 50 | + class C<T> { |
| 51 | + private T t |
| 52 | + C(T item) { |
| 53 | + t = item |
| 54 | + } |
| 55 | + static <U> C<U> of(U item) { |
| 56 | + new C<U>(item) |
| 57 | + } |
| 58 | + def <V> C<V> map(F<? super T, ? super V> func) { |
| 59 | + new C<V>(func.apply(t)) |
| 60 | + } |
| 61 | + } |
| 62 | + ''' |
| 63 | + def d = new File(parentDir, 'D.groovy') |
| 64 | + d.write ''' |
| 65 | + class D { |
| 66 | + static <W> Set<W> wrap(W o) { |
| 67 | + Collections.singleton(o) |
| 68 | + } |
| 69 | + } |
| 70 | + ''' |
| 71 | + def f = new File(parentDir, 'F.groovy') |
| 72 | + f.write ''' |
| 73 | + interface F<X,Y> { |
| 74 | + Y apply(X x) |
| 75 | + } |
| 76 | + ''' |
| 77 | + def g = new File(parentDir, 'G.groovy') |
| 78 | + g.write ''' |
| 79 | + def c = C.of(123) |
| 80 | + def d = c.map(D.&wrap) |
| 81 | + def e = d.map(x -> x.first().intValue()) |
| 82 | + ''' |
| 83 | + |
| 84 | + def loader = new GroovyClassLoader(this.class.classLoader) |
| 85 | + def cu = new JavaAwareCompilationUnit(config, loader) |
| 86 | + cu.addSources(c, d, f, g) |
| 87 | + cu.compile() |
| 88 | + |
| 89 | + loader.loadClass('G').main() |
| 90 | + } finally { |
| 91 | + parentDir.deleteDir() |
| 92 | + config.targetDirectory.deleteDir() |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void testCoerceClosure3() { |
| 98 | + def config = new CompilerConfiguration().tap { |
| 99 | + jointCompilationOptions = [memStub: true] |
| 100 | + targetDirectory = File.createTempDir() |
| 101 | + } |
| 102 | + File parentDir = File.createTempDir() |
| 103 | + try { |
| 104 | + def f = new File(parentDir, 'F.groovy') |
| 105 | + f.write ''' |
| 106 | + class FInfo extends EventObject { |
| 107 | + FInfo() { super(null) } |
| 108 | + } |
| 109 | + interface FListener extends EventListener { |
| 110 | + void somethingHappened(FInfo i) |
| 111 | + } |
| 112 | + ''' |
| 113 | + def g = new File(parentDir, 'G.groovy') |
| 114 | + g.write ''' |
| 115 | + class H { |
| 116 | + void addFListener(FListener f) { |
| 117 | + f.somethingHappened(null) |
| 118 | + } |
| 119 | + void removeFListener(FListener f) { |
| 120 | + } |
| 121 | + } |
| 122 | +
|
| 123 | + new H().somethingHappened = { info -> } |
| 124 | + ''' |
| 125 | + |
| 126 | + def loader = new GroovyClassLoader(this.class.classLoader) |
| 127 | + def cu = new JavaAwareCompilationUnit(config, loader) |
| 128 | + cu.addSources(f, g) |
| 129 | + cu.compile() |
| 130 | + |
| 131 | + loader.loadClass('G').main() |
| 132 | + } finally { |
| 133 | + parentDir.deleteDir() |
| 134 | + config.targetDirectory.deleteDir() |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments