Commits

Tim Vernum committed 94a86f9

Refactor converter to move methods into {Type|Method}Info classes

Comments (0)

Files changed (12)

+#!/bin/bash
+
+cd $(dirname $0)
+cd ..
+
+CP="lib/convert/org.adjective.orthodox.jar:parser/output/ant/classes/parser:convert/output/ant/classes/"
+MAIN="org.adjective.syntactic.convert.j7to8.Main"
+
+for FILE in "$@"
+do
+    java -cp "${CP}" "${MAIN}" "${FILE}"
+done
+

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

 import org.adjective.syntactic.parser.type.SimpleJavaType;
 import org.adjective.syntactic.parser.util.ModifierSet;
 
-import java.lang.reflect.Method;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Iterator;
 
     private MethodInfo getFunctionalMethod(final TypeInfo typeInfo)
     {
-        final Collection<MethodInfo> methods = filterMethods(typeInfo.getMethods());
+        final Collection<MethodInfo> methods = typeInfo.getAbstractMethods();
         if (methods.size() != 1)
         {
             throw new ConversionException("Class '" + typeInfo.getName() + "' should have exactly 1 method but has " + methods
         return false;
     }
 
-    private Collection<MethodInfo> filterMethods(final MethodInfo[] methods)
-    {
-        Collection<MethodInfo> filter = new ArrayList<MethodInfo>(1);
-        for (final MethodInfo method : methods)
-        {
-            if (!isObjectMethod(method))
-            {
-                filter.add(method);
-            }
-        }
-        return filter;
-    }
-
-    private boolean isObjectMethod(MethodInfo method)
-    {
-        final Method[] objectMethods = Object.class.getMethods();
-        for (Method objectMethod : objectMethods)
-        {
-            if (isMatchingMethod(objectMethod, method))
-            {
-                return true;
-            }
-        }
-        return false;
-    }
-
-    private boolean isMatchingMethod(final Method objectMethod, final MethodInfo method)
-    {
-        if (!objectMethod.getName().equals(method.getName()))
-        {
-            return false;
-        }
-        if (objectMethod.getParameterTypes().length != method.getParameterTypes().length)
-        {
-            return false;
-        }
-        for (int i = 0; i < objectMethod.getParameterTypes().length; i++)
-        {
-            if (!isMatchingType(objectMethod.getParameterTypes()[i], method.getParameterTypes()[i]))
-            {
-                return false;
-            }
-        }
-        return true;
-    }
-
-    private boolean isMatchingType(final Class<?> cls, final JavaType type)
-    {
-        return cls.getName().equals(type.getTypeName());
-    }
-
     private ASTFormalParameters makeFormalParameters(final MethodInfo method, final ASTLambdaParameters parameters)
     {
         final JavaType[] parameterTypes;

convert/source/java/main/org/adjective/syntactic/convert/j7to8/type/ASTMethodInfo.java

 import org.adjective.syntactic.parser.ast.ASTMethodDeclaration;
 import org.adjective.syntactic.parser.ast.ASTNameList;
 import org.adjective.syntactic.parser.type.JavaType;
+import org.adjective.syntactic.parser.util.ModifierSet;
 
-public class ASTMethodInfo implements MethodInfo
+public class ASTMethodInfo extends AbstractMethodInfo implements MethodInfo
 {
     private final ASTMethodDeclaration _method;
     private final TypeParameterLookup _parameterLookup;
+    private final ModifierSet _modifiers;
 
-    public ASTMethodInfo(final ASTMethodDeclaration method, final TypeParameterLookup lookup)
+    public ASTMethodInfo(final ModifierSet modifiers, final ASTMethodDeclaration method, final TypeParameterLookup lookup)
     {
         _method = method;
         _parameterLookup = lookup;
+        _modifiers = new ModifierSet(modifiers);
+        if (isDeclaredOnObject())
+        {
+            _modifiers.clear(ModifierSet.Modifier.ABSTRACT);
+        }
     }
 
     private JavaType getType(final JavaType type)
     }
 
     @Override
+    public ModifierSet getModifiers()
+    {
+        return _modifiers;
+    }
+
+    @Override
     public JavaType[] getParameterTypes()
     {
         return getTypes(_method.getParameters().getParameterTypes());

convert/source/java/main/org/adjective/syntactic/convert/j7to8/type/ASTTypeInfo.java

  */
 package org.adjective.syntactic.convert.j7to8.type;
 
-import org.adjective.syntactic.parser.ast.*;
+import org.adjective.syntactic.parser.ast.ASTClassOrInterfaceBodyElement;
+import org.adjective.syntactic.parser.ast.ASTClassOrInterfaceDeclaration;
+import org.adjective.syntactic.parser.ast.ASTMember;
+import org.adjective.syntactic.parser.ast.ASTMethodDeclaration;
+import org.adjective.syntactic.parser.ast.ASTTypeParameter;
+import org.adjective.syntactic.parser.name.TypeParameter;
 import org.adjective.syntactic.parser.type.JavaType;
 import org.adjective.syntactic.parser.type.MemberType;
-import org.adjective.syntactic.parser.name.TypeParameter;
+import org.adjective.syntactic.parser.util.ModifierSet;
 
 import java.util.ArrayList;
 import java.util.List;
 
-public class ASTTypeInfo implements TypeInfo, TypeParameterLookup
+public class ASTTypeInfo extends AbstractTypeInfo implements TypeInfo, TypeParameterLookup
 {
     private final ASTClassOrInterfaceDeclaration _declaration;
     private final TypeParameter[] _parameters;
             {
                 continue;
             }
-            result.add(new ASTMethodInfo((ASTMethodDeclaration) member.getMember(), this));
+            ModifierSet modifiers = getModifiers(member);
+            result.add(new ASTMethodInfo(modifiers, (ASTMethodDeclaration) member.getMember(), this));
         }
         return result.toArray(new ASTMethodInfo[result.size()]);
     }
 
+    private ModifierSet getModifiers(final ASTMember member)
+    {
+        ModifierSet modifiers = member.getModifiers();
+        if (this._declaration.isInterface())
+        {
+            modifiers = new ModifierSet(modifiers);
+            modifiers.set(ModifierSet.Modifier.ABSTRACT);
+            modifiers.set(ModifierSet.Modifier.PUBLIC);
+        }
+        return modifiers;
+    }
+
     @Override
     public MethodInfo[] getMethods()
     {

convert/source/java/main/org/adjective/syntactic/convert/j7to8/type/AbstractMethodInfo.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.type;
+
+import org.adjective.syntactic.convert.j7to8.util.ClassUtil;
+
+import java.lang.reflect.Method;
+
+public abstract class AbstractMethodInfo implements  MethodInfo {
+
+    protected boolean isDeclaredOnObject()
+    {
+        final Method[] objectMethods = Object.class.getMethods();
+        for (Method objectMethod : objectMethods)
+        {
+            if (matches(objectMethod))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private boolean matches(final Method objectMethod)
+    {
+        if (!objectMethod.getName().equals(getName()))
+        {
+            return false;
+        }
+        if (objectMethod.getParameterTypes().length != getParameterTypes().length)
+        {
+            return false;
+        }
+        for (int i = 0; i < objectMethod.getParameterTypes().length; i++)
+        {
+            if (!ClassUtil.isMatchingType(objectMethod.getParameterTypes()[i], getParameterTypes()[i]))
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+}

convert/source/java/main/org/adjective/syntactic/convert/j7to8/type/AbstractTypeInfo.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.type;
+
+import org.adjective.syntactic.parser.util.ModifierSet;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+public abstract class AbstractTypeInfo implements TypeInfo
+{
+    @Override
+    public Collection<MethodInfo> getAbstractMethods()
+    {
+        final MethodInfo[] methods = getMethods();
+        Collection<MethodInfo> filter = new ArrayList<MethodInfo>(1);
+        for (final MethodInfo method : methods)
+        {
+            if (method.getModifiers().is(ModifierSet.Modifier.ABSTRACT))
+            {
+                filter.add(method);
+            }
+        }
+        return filter;
+    }
+}

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

  */
 package org.adjective.syntactic.convert.j7to8.type;
 
+import org.adjective.syntactic.parser.name.TypeParameter;
 import org.adjective.syntactic.parser.type.JavaType;
-import org.adjective.syntactic.parser.name.TypeParameter;
 
 import java.lang.reflect.TypeVariable;
 
-public class ClassInfo implements TypeInfo, TypeParameterLookup
+public class ClassInfo extends AbstractTypeInfo implements TypeInfo, TypeParameterLookup
 {
     private final Class<?> _cls;
     private final TypeParameter[] _parameters;

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

 package org.adjective.syntactic.convert.j7to8.type;
 
 import org.adjective.syntactic.parser.type.JavaType;
+import org.adjective.syntactic.parser.util.ModifierSet;
 
 import java.lang.reflect.Method;
 import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
 
-public class ClassMethodInfo implements MethodInfo
+public class ClassMethodInfo extends AbstractMethodInfo implements MethodInfo
 {
     private final Method _method;
     private final TypeParameterLookup _parameterLookup;
     }
 
     @Override
+    public ModifierSet getModifiers()
+    {
+        final ModifierSet modifiers = new ModifierSet(_method.getModifiers());
+        if (isDeclaredOnObject())
+        {
+            modifiers.clear(ModifierSet.Modifier.ABSTRACT);
+        }
+        return modifiers;
+    }
+
+    @Override
     public JavaType[] getParameterTypes()
     {
         return getJavaTypes(_method.getGenericParameterTypes(), _method.getParameterTypes());
         final String[] names = new String[_method.getParameterTypes().length];
         for (int i = 0; i < names.length; i++)
         {
-            names[i] = "p" + (i + 1);
+            names[i] = "arg" + i;
         }
         return names;
     }

convert/source/java/main/org/adjective/syntactic/convert/j7to8/type/MethodInfo.java

 package org.adjective.syntactic.convert.j7to8.type;
 
 import org.adjective.syntactic.parser.type.JavaType;
+import org.adjective.syntactic.parser.util.ModifierSet;
 
 public interface MethodInfo {
 
+    public String getName();
+    public ModifierSet getModifiers();
     public JavaType getReturnType();
-    public String getName();
     public JavaType[] getParameterTypes();
     public String[] getParameterNames();
 

convert/source/java/main/org/adjective/syntactic/convert/j7to8/type/TypeInfo.java

  */
 package org.adjective.syntactic.convert.j7to8.type;
 
+import java.util.Collection;
+
 public interface TypeInfo
 {
+    public String getName();
     public MethodInfo[] getMethods();
-    public String getName();
+    public Collection<MethodInfo> getAbstractMethods();
 }

convert/source/java/main/org/adjective/syntactic/convert/j7to8/util/ClassUtil.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.util;
+
+import org.adjective.syntactic.parser.type.JavaType;
+
+public class ClassUtil
+{
+    public static boolean isMatchingType(final Class<?> cls, final JavaType type)
+    {
+        return cls.getName().equals(type.getTypeName());
+    }
+}

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

 {
     public static enum Modifier
     {
-        PUBLIC, PROTECTED, PRIVATE, ABSTRACT, STATIC, FINAL, SYNCHRONIZED, NATIVE, TRANSIENT, VOLATILE, STRICTFP;
+        PUBLIC(java.lang.reflect.Modifier.PUBLIC),
+        PROTECTED(java.lang.reflect.Modifier.PROTECTED),
+        PRIVATE(java.lang.reflect.Modifier.PRIVATE),
+        ABSTRACT(java.lang.reflect.Modifier.ABSTRACT),
+        STATIC(java.lang.reflect.Modifier.STATIC),
+        FINAL(java.lang.reflect.Modifier.FINAL),
+        SYNCHRONIZED(java.lang.reflect.Modifier.SYNCHRONIZED),
+        NATIVE(java.lang.reflect.Modifier.NATIVE),
+        TRANSIENT(java.lang.reflect.Modifier.TRANSIENT),
+        VOLATILE(java.lang.reflect.Modifier.VOLATILE),
+        STRICTFP(java.lang.reflect.Modifier.STRICT);
+
+        private final int _mask;
+
+        Modifier(final int javaBitMask)
+        {
+            _mask = javaBitMask;
+        }
+
+        public int getMask()
+        {
+            return _mask;
+        }
     }
 
     private final EnumSet<Modifier> _modifiers;
         _modifiers = EnumSet.copyOf(modifiers);
     }
 
+    public ModifierSet(final int javaModifierBitmask)
+    {
+        this();
+        for (Modifier modifier : Modifier.values())
+        {
+            if ((javaModifierBitmask & modifier.getMask()) != 0)
+            {
+                this.set(modifier);
+            }
+        }
+    }
+
     public void set(Modifier modifier)
     {
         _modifiers.add(modifier);