Commits

Tim Vernum  committed 5930f74

Lookup generic type parameters (without using Orthodox)

  • Participants
  • Parent commits fc529ff

Comments (0)

Files changed (7)

 syntax: regexp
 ^[^/]*/output/
+^output/
 ^.idea/
 ^[a-z]*-generated/
 ^[a-z]*-samples/

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

  */
 package org.adjective.syntactic.convert.j7to8;
 
+import org.adjective.syntactic.parser.util.TypeParameter;
+
 public class ClassFinder
 {
     public boolean hasClass(final String qualifiedName)
     {
-        return getClass(qualifiedName) != null;
+        return findClass(qualifiedName) != null;
     }
 
-    public TypeInfo getClass(final String qualifiedName)
+    public TypeInfo getClass(final String qualifiedName, TypeParameter[] parameters)
+    {
+        final Class<?> cls = findClass(qualifiedName);
+        if (cls == null)
+        {
+            return null;
+        }
+        return new ClassInfo(cls, parameters);
+    }
+
+    private Class<?> findClass(final String qualifiedName)
     {
         try
         {
-            final Class<?> cls = Class.forName(qualifiedName);
-            return new ClassInfo(cls);
+            return Class.forName(qualifiedName);
         }
         catch (ClassNotFoundException e)
         {

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

  */
 package org.adjective.syntactic.convert.j7to8;
 
-import org.adjective.syntactic.parser.util.BaseType;
 import org.adjective.syntactic.parser.util.JavaType;
-import org.adjective.syntactic.parser.util.ParameterizedName;
-import org.adjective.syntactic.parser.util.UnparameterizedName;
+import org.adjective.syntactic.parser.util.TypeParameter;
 
-public class ClassInfo implements TypeInfo, JavaType
+import java.lang.reflect.TypeVariable;
+
+public class ClassInfo implements TypeInfo, TypeParameterLookup
 {
     private final Class<?> _cls;
+    private final TypeParameter[] _parameters;
 
-    public ClassInfo(final Class<?> cls)
+    public ClassInfo(final Class<?> cls, final TypeParameter[] parameters)
     {
         _cls = cls;
+        _parameters = parameters;
     }
 
     @Override
     public MethodInfo[] getMethods()
     {
-        return ClassMethodInfo.array(_cls.getMethods());
+        return ClassMethodInfo.array(_cls.getMethods(), this);
     }
 
     @Override
     }
 
     @Override
-    public BaseType getBaseType()
+    public JavaType findParameter(String name)
     {
-        if (_cls.isPrimitive())
+        final TypeVariable<? extends Class<?>>[] typeParameters = _cls.getTypeParameters();
+        for (int i = 0; i < typeParameters.length; i++)
         {
-            if (_cls == Void.TYPE)
+            TypeVariable<? extends Class<?>> typeParameter = typeParameters[i];
+            if (typeParameter.getName().equals(name))
             {
-                return BaseType.VOID;
-            }
-            if (_cls == Boolean.TYPE)
-            {
-                return BaseType.BOOLEAN;
-            }
-            if (_cls == Byte.TYPE)
-            {
-                return BaseType.BYTE;
-            }
-            if (_cls == Short.TYPE)
-            {
-                return BaseType.SHORT;
-            }
-            if (_cls == Integer.TYPE)
-            {
-                return BaseType.INT;
-            }
-            if (_cls == Long.TYPE)
-            {
-                return BaseType.LONG;
-            }
-            if (_cls == Float.TYPE)
-            {
-                return BaseType.FLOAT;
-            }
-            if (_cls == Double.TYPE)
-            {
-                return BaseType.DOUBLE;
-            }
-            if (_cls == Character.TYPE)
-            {
-                return BaseType.CHAR;
+                return _parameters[i].getType();
             }
         }
-        return BaseType.OBJECT;
+        return new ClassType(Object.class);
     }
 
-    @Override
-    public String getTypeName()
-    {
-        return _cls.getName();
-    }
-
-    @Override
-    public ParameterizedName[] getParameterizedTypeName()
-    {
-        final String[] parts = getTypeName().split("\\.");
-        final ParameterizedName[] names = new ParameterizedName[parts.length];
-        for (int i = 0; i < names.length; i++)
-        {
-            names[i] = new UnparameterizedName(parts[i]);
-        }
-        return names;
-    }
-
-    @Override
-    public int getArrayDepth()
-    {
-        int depth = 0;
-        for (Class cls = _cls; cls.isArray(); cls = cls.getComponentType())
-        {
-            depth++;
-        }
-        return depth;
-    }
-
-    public static ClassInfo[] array(final Class<?>[] classes)
-    {
-        final ClassInfo[] info = new ClassInfo[classes.length];
-        for (int i = 0; i < info.length; i++)
-        {
-            info[i] = new ClassInfo(classes[i]);
-        }
-        return info;
-    }
 }

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

 import org.adjective.syntactic.parser.util.JavaType;
 
 import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
 
 public class ClassMethodInfo implements MethodInfo
 {
     private final Method _method;
+    private final TypeParameterLookup _parameterLookup;
 
-    public ClassMethodInfo(final Method method)
+    public ClassMethodInfo(final Method method, final TypeParameterLookup parameterLookup)
     {
-
         _method = method;
+        _parameterLookup = parameterLookup;
     }
 
-    public static ClassMethodInfo[] array(final Method[] methods)
+    public static ClassMethodInfo[] array(final Method[] methods, TypeParameterLookup parameterLookup)
     {
         final ClassMethodInfo[] info = new ClassMethodInfo[methods.length];
         for (int i = 0; i < methods.length; i++)
         {
-            info[i] = new ClassMethodInfo(methods[i]);
+            info[i] = new ClassMethodInfo(methods[i], parameterLookup);
         }
         return info;
     }
     @Override
     public JavaType getReturnType()
     {
-        return new ClassInfo(_method.getReturnType());
+        // @TODO lookup
+        return new ClassType(_method.getReturnType());
     }
 
     @Override
     @Override
     public JavaType[] getParameterTypes()
     {
-        final Class<?>[] parameters = _method.getParameterTypes();
-        return ClassInfo.array(parameters);
+        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++)
+        {
+            Type parameter = typeParameters[i];
+            if (parameter instanceof TypeVariable)
+            {
+                types[i] = lookup((TypeVariable) parameter);
+            }
+            else
+            {
+                types[i] = new ClassType(classParameters[i]);
+            }
+        }
+        return types;
+    }
+
+    private JavaType lookup(final TypeVariable var)
+    {
+        return this._parameterLookup.findParameter(var.getName());
     }
 
     @Override
     @Override
     public JavaType[] getExceptionTypes()
     {
-        return ClassInfo.array(_method.getExceptionTypes());
+        // @TODO lookup
+        return ClassType.array(_method.getExceptionTypes());
     }
 
     @Override

File convert/source/java/main/org/adjective/syntactic/convert/j7to8/ClassType.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.convert.j7to8;
+
+import org.adjective.syntactic.parser.util.BaseType;
+import org.adjective.syntactic.parser.util.JavaType;
+import org.adjective.syntactic.parser.util.ParameterizedName;
+import org.adjective.syntactic.parser.util.UnparameterizedName;
+
+public class ClassType implements JavaType
+{
+
+    private Class<?> _cls;
+
+    public ClassType(Class<?> cls)
+    {
+        _cls = cls;
+    }
+
+    @Override
+    public BaseType getBaseType()
+    {
+        if (_cls.isPrimitive())
+        {
+            if (_cls == Void.TYPE)
+            {
+                return BaseType.VOID;
+            }
+            if (_cls == Boolean.TYPE)
+            {
+                return BaseType.BOOLEAN;
+            }
+            if (_cls == Byte.TYPE)
+            {
+                return BaseType.BYTE;
+            }
+            if (_cls == Short.TYPE)
+            {
+                return BaseType.SHORT;
+            }
+            if (_cls == Integer.TYPE)
+            {
+                return BaseType.INT;
+            }
+            if (_cls == Long.TYPE)
+            {
+                return BaseType.LONG;
+            }
+            if (_cls == Float.TYPE)
+            {
+                return BaseType.FLOAT;
+            }
+            if (_cls == Double.TYPE)
+            {
+                return BaseType.DOUBLE;
+            }
+            if (_cls == Character.TYPE)
+            {
+                return BaseType.CHAR;
+            }
+        }
+        return BaseType.OBJECT;
+    }
+
+    @Override
+    public String getTypeName()
+    {
+        return _cls.getName();
+    }
+
+    @Override
+    public ParameterizedName[] getParameterizedTypeName()
+    {
+        final String[] parts = getTypeName().split("\\.");
+        final ParameterizedName[] names = new ParameterizedName[parts.length];
+        for (int i = 0; i < names.length; i++)
+        {
+            names[i] = new UnparameterizedName(parts[i]);
+        }
+        return names;
+    }
+
+    @Override
+    public int getArrayDepth()
+    {
+        int depth = 0;
+        for (Class cls = _cls; cls.isArray(); cls = cls.getComponentType())
+        {
+            depth++;
+        }
+        return depth;
+    }
+
+    public static ClassType[] array(final Class<?>... cls)
+    {
+        ClassType[] type = new ClassType[cls.length];
+        for (int i = 0; i < cls.length; i++)
+        {
+            type[i] = new ClassType(cls[i]);
+        }
+        return type;
+    }
+}

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

             }
         }
 
-        final TypeInfo typeInfo = _classFinder.getClass(name);
+        final TypeInfo typeInfo = _classFinder.getClass(name, last(forType.getParameterizedTypeName()).getParameters());
         if (typeInfo == null)
         {
             throw new ConversionException("Cannot find '" + name + "'");
         return makeMethodDeclaration(method, parameters, body);
     }
 
+    private <T> T last(T[] array) {
+        return array[array.length -1];
+    }
+
     private Collection<MethodInfo> filterMethods(final MethodInfo[] methods)
     {
         Collection<MethodInfo> filter = new ArrayList<MethodInfo>(1);

File convert/source/java/main/org/adjective/syntactic/convert/j7to8/TypeParameterLookup.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.convert.j7to8;
+
+import org.adjective.syntactic.parser.util.JavaType;
+
+public interface TypeParameterLookup {
+    JavaType findParameter(String name);
+}