Commits

Tim Vernum committed d78f959

Drop wildcard type bounds from generated anonymous classes

Comments (0)

Files changed (6)

 ^[a-z]*/.*\.iml
 ^lib/
 \.class$
+^wiki/
 
-

convert/source/java/main/org/adjective/syntactic/convert/j7to8/Convert7To8Visitor.java

         final ASTMethodDeclaration method = makeMethodDeclaration(forType, lambda.getParameters(), lambda.getBody());
         final ASTMember member = new ASTMember(modifiers, method);
         ASTClassOrInterfaceBody body = new ASTClassOrInterfaceBody(new ASTClassOrInterfaceBodyElement(member));
-        return new ASTAllocationExpression(forType, new ASTArguments(), body);
+        return new ASTAllocationExpression(withoutWildcard(forType), new ASTArguments(), body);
+    }
+
+    private JavaType withoutWildcard(final JavaType forType)
+    {
+        if (!hasWildcard(forType))
+        {
+            return forType;
+        }
+        final ParameterizedName[] inputName = forType.getParameterizedTypeName();
+        final ParameterizedName[] outputName = new ParameterizedName[inputName.length];
+        for (int i = 0; i < inputName.length; i++)
+        {
+            ParameterizedName parameterizedName = inputName[i];
+            final TypeParameter[] inputParameters = parameterizedName.getParameters();
+            if (inputParameters.length == 0)
+            {
+                outputName[i] = parameterizedName;
+                continue;
+            }
+            final TypeParameter[] outputParameters = new TypeParameter[inputParameters.length];
+            for (int j = 0; j < outputParameters.length; j++)
+            {
+                outputParameters[j] = new SimpleTypeParameter(TypeParameter.Kind.EXACT, inputParameters[j].getType());
+            }
+            outputName[i] = new SimpleParameterizedName(inputName[i].getName(), outputParameters);
+        }
+        return new SimpleJavaType(outputName, forType.getArrayDepth());
+    }
+
+    private boolean hasWildcard(final JavaType forType)
+    {
+        final TypeParameter[] parameters = last(forType.getParameterizedTypeName()).getParameters();
+        for (int i = 0; i < parameters.length; i++)
+        {
+            if (parameters[i].getKind() != TypeParameter.Kind.EXACT)
+            {
+                return true;
+            }
+        }
+        return false;
     }
 
     private ASTMethodDeclaration makeMethodDeclaration(final JavaType forType, final ASTLambdaParameters parameters,

parser/source/java/parser/org/adjective/syntactic/parser/util/ParameterizedName.java

 
 public interface ParameterizedName
 {
+    public String getName();
     public TypeParameter[] getParameters();
-    public String getName();
 }

parser/source/java/parser/org/adjective/syntactic/parser/util/SimpleJavaType.java

 {
     private final String _name;
     private final ParameterizedName[] _parameterizedName;
+    private final int _arrayDepth;
 
     public SimpleJavaType(final ASTQualifiedIdentifier name)
     {
-        this(name.getName(), parameterize(name));
+        this(name.getName(), parameterize(name), 0);
+    }
+
+    public SimpleJavaType(final ParameterizedName[] name, int arrayDepth)
+    {
+        this(combine(name), name, arrayDepth);
+    }
+
+    private SimpleJavaType(final String name, ParameterizedName[] parameterizedName, int arrayDepth)
+    {
+        _name = name;
+        _parameterizedName = parameterizedName;
+        _arrayDepth = arrayDepth;
     }
 
     private static ParameterizedName[] parameterize(final ASTQualifiedIdentifier name)
         return parameterizedName;
     }
 
-    private SimpleJavaType(final String name, ParameterizedName[] parameterizedName)
+    private static String combine(final ParameterizedName[] name)
     {
-        _name = name;
-        _parameterizedName = parameterizedName;
+        StringBuilder builder = new StringBuilder(name[0].getName());
+        for (int i = 1; i < name.length; i++)
+        {
+            builder.append(".");
+            builder.append(name[i].getName());
+        }
+        return builder.toString();
     }
 
     @Override
     @Override
     public int getArrayDepth()
     {
-        return 0;
+        return _arrayDepth;
     }
 }

parser/source/java/parser/org/adjective/syntactic/parser/util/SimpleParameterizedName.java

+/* ------------------------------------------------------------------------
+ * Copyright 2013 Tim Vernum
+ * ------------------------------------------------------------------------
+ * Licensed 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 org.adjective.syntactic.parser.util;
+
+public class SimpleParameterizedName implements ParameterizedName
+{
+    private final String _name;
+    private final TypeParameter[] _typeParameters;
+
+    public SimpleParameterizedName(final String name, final TypeParameter[] typeParameters)
+    {
+
+        _name = name;
+        _typeParameters = typeParameters;
+    }
+
+    @Override
+    public String getName()
+    {
+        return _name;
+    }
+
+    @Override
+    public TypeParameter[] getParameters()
+    {
+        return _typeParameters;
+    }
+}

parser/source/java/parser/org/adjective/syntactic/parser/util/SimpleTypeParameter.java

+/* ------------------------------------------------------------------------
+ * Copyright 2013 Tim Vernum
+ * ------------------------------------------------------------------------
+ * Licensed 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 org.adjective.syntactic.parser.util;
+
+public class SimpleTypeParameter implements TypeParameter
+{
+    private final Kind _kind;
+    private final JavaType _type;
+
+    public SimpleTypeParameter(final Kind kind, final JavaType type)
+    {
+        _kind = kind;
+        _type = type;
+    }
+
+    @Override
+    public Kind getKind()
+    {
+        return _kind;
+    }
+
+    @Override
+    public JavaType getType()
+    {
+        return _type;
+    }
+}