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

Eric Milles commented on GROOVY-9074:
-------------------------------------

Here is one change that will assist 2 possible approaches.  As-is, the wildcard 
type "? extends Shape" will be returned for the parameter type, which will 
match Rectangle.  Thus, {{chooseBestMethod}} can return {{add(E)}} as a match 
and a check can be made later for the capture-of type.  *Or*, the wildcard 
check below can be uncommented and a capture-of type will be returned that will 
not match Rectangle.  Thus, an error "Cannot find matching method 
java.util.List#add(Rectangle)..." will be produced.

{code:java}
    private static Parameter[] makeRawTypes(Parameter[] params, 
Map<GenericsType, GenericsType> genericsPlaceholderAndTypeMap) {
        Parameter[] newParam = new Parameter[params.length];
        for (int i = 0; i < params.length; i++) {
            Parameter oldP = params[i];

            /* GRECLIPSE edit -- GROOVY-9074
            ClassNode actualType = 
GenericsUtils.findActualTypeByGenericsPlaceholderName(oldP.getType().getUnresolvedName(),
 genericsPlaceholderAndTypeMap);
            Parameter newP = new Parameter(makeRawType(null == actualType ? 
oldP.getType() : actualType), oldP.getName());
            newParam[i] = newP;
            */
            String name = oldP.getType().getUnresolvedName();
            Optional<GenericsType> value = 
genericsPlaceholderAndTypeMap.entrySet().stream().filter(e -> 
e.getKey().getName().equals(name)).findFirst().map(e -> e.getValue());
            ClassNode type = value.map(gt -> /*gt.isWildcard() ? 
ClassHelper.makeWithoutCaching("capture-of ?") :*/ 
gt.getType()).orElse(oldP.getType());
            newParam[i] = new Parameter(type, oldP.getName());
            // GRECLIPSE end
        }
        return newParam;
    }
{code}

> CompileStatic does not throw expected exception
> -----------------------------------------------
>
>                 Key: GROOVY-9074
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9074
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 2.5.5
>            Reporter: Robert Stagner
>            Assignee: Daniel Sun
>            Priority: Major
>
> While reading about Java Generics and wildcards (see 
> [https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html]), I 
> came across the following code in the referenced oracle document
> {code:java}
> Collection<?> c = new ArrayList<String>();
>  c.add(new Object()); // Compile time error{code}
> Now, I decided to wrap this in a class and try it out in Java and then in 
> Groovy.
> Here is the Java code
>  
> {code:java}
> import java.util.*;
> public class TestCollection {
>  static Collection<?> c = new ArrayList<String>();
>  public static void main(String[] args) {
>    c.add(new Object()); // Compile time error
>  }
> }
> {code}
>  
>   
> and here is the groovy code
>  
> {code:java}
> import java.util.*;
> import groovy.transform.CompileStatic;
> @CompileStatic
> public class TestCollection {
>  static Collection<?> c = new ArrayList<String>();
>  public static void main(String[] args) {
>    c.add(new Object()); // Compile time error
>    println c.first();
>  }
> }
> {code}
>  
> When I attempt to compile the Java code I get the following expected error
> {code:java}
> $ javac TestCollection.java
>  TestCollection.java:7: error: incompatible types: Object cannot be converted 
> to CAP#1
>  c.add(new Object()); // Compile time error
>  ^
>  where CAP#1 is a fresh type-variable:
>  CAP#1 extends Object from capture of ?{code}
> But, when I run the code through Groovy, I do not get a compile-time error.  
> Instead, I get the following output
> {code:java}
> java.lang.Object@41a0aa7d{code}



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

Reply via email to