Commits

Tim Vernum committed b27f0d1

Improved generic support. Fix lookahead parsing of lambda parameters

  • Participants
  • Parent commits 5930f74

Comments (0)

Files changed (5)

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

File convert/source/java/main/org/adjective/syntactic/convert/j7to8/ClassMethodInfo.java

     @Override
     public JavaType getReturnType()
     {
-        // @TODO lookup
-        return new ClassType(_method.getReturnType());
+        return getJavaType(_method.getGenericReturnType(), _method.getReturnType());
     }
 
     @Override
     @Override
     public JavaType[] getParameterTypes()
     {
-        final Class<?>[] classParameters = _method.getParameterTypes();
-        final Type[] typeParameters = _method.getGenericParameterTypes();
-        final JavaType[] types = new JavaType[typeParameters.length];
-        for (int i = 0; i < typeParameters.length; i++)
+        return getJavaTypes(_method.getGenericParameterTypes(), _method.getParameterTypes());
+    }
+
+    private JavaType[] getJavaTypes(final Type[] genericTypes, final Class<?>[] classes)
+    {
+        final JavaType[] javaTypes = new JavaType[genericTypes.length];
+        for (int i = 0; i < genericTypes.length; i++)
         {
-            Type parameter = typeParameters[i];
-            if (parameter instanceof TypeVariable)
-            {
-                types[i] = lookup((TypeVariable) parameter);
-            }
-            else
-            {
-                types[i] = new ClassType(classParameters[i]);
-            }
+            javaTypes[i] = getJavaType(genericTypes[i], classes[i]);
         }
-        return types;
+        return javaTypes;
+    }
+
+    private JavaType getJavaType(final Type parameter, final Class<?> cls)
+    {
+        final JavaType javaType;
+        if (parameter instanceof TypeVariable)
+        {
+            javaType = lookup((TypeVariable) parameter);
+        }
+        else
+        {
+            javaType = new ClassType(cls);
+        }
+        return javaType;
     }
 
     private JavaType lookup(final TypeVariable var)
     @Override
     public JavaType[] getExceptionTypes()
     {
-        // @TODO lookup
-        return ClassType.array(_method.getExceptionTypes());
+        return getJavaTypes(_method.getGenericExceptionTypes(), _method.getExceptionTypes());
     }
 
     @Override

File convert/source/java/samples/sample/GenericLambda.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.
+ * ------------------------------------------------------------------------
+ */
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class GenericLambda
+{
+    public interface Transformer<FROM, TO>
+    {
+        public TO transform(FROM from);
+    }
+
+    public static <IN, OUT> List<OUT> transform(Iterable<? extends IN> list, Transformer<? super IN, ? extends OUT> transformer)
+    {
+        List<OUT> result = new ArrayList<OUT>();
+        for (IN in : list)
+        {
+            result.add(transformer.transform(in));
+        }
+        return result;
+    }
+
+    public static void main(String[] args)
+    {
+        final Transformer<? super Long, CharSequence> transformer = (Number n) -> n.toString();
+        transform(Collections.singletonList(99L),
+                  transformer
+        );
+    }
+
+}

File parser/source/grammar/java.jjt

 void LambdaParameterList() :
 {}
 {
-    LOOKAHEAD(2)
+    LOOKAHEAD(FormalParameter())
+    ( FormalParameter() ( "," FormalParameter() ) * )
+|
     ( Identifier() ( "," Identifier() ) * )
-|
-    ( FormalParameter() ( "," FormalParameter() ) * )
 }
 
 void LambdaBody() :

File parser/source/java/parser/org/adjective/syntactic/parser/ast/ASTParameterizedIdentifier.java

     @Override
     public TypeParameter[] getParameters()
     {
-        return getTypeArguments().getParameters();
+        final ASTTypeArguments arguments = getTypeArguments();
+        return arguments == null ? new TypeParameter[0] : arguments.getParameters();
     }
 
     @Override