1. Tim Vernum
  2. syntactic

Commits

Tim Vernum  committed 68c2263

Lookup classes/interfaces defined in same CompilationUnit

  • Participants
  • Parent commits b27f0d1
  • Branches default

Comments (0)

Files changed (9)

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

View file
+/* ------------------------------------------------------------------------
+ * 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.ast.ASTMethodDeclaration;
+import org.adjective.syntactic.parser.util.JavaType;
+
+public class ASTMethodInfo implements MethodInfo
+{
+    private final ASTMethodDeclaration _method;
+
+    public ASTMethodInfo(final ASTMethodDeclaration method)
+    {
+        _method = method;
+    }
+
+    @Override
+    public JavaType getReturnType()
+    {
+        return _method.getActualReturnType();
+    }
+
+    @Override
+    public String getName()
+    {
+        return _method.getName().getIdentifier();
+    }
+
+    @Override
+    public JavaType[] getParameterTypes()
+    {
+        return _method.getParameters().getParameterTypes();
+    }
+
+    @Override
+    public String[] getParameterNames()
+    {
+        return _method.getParameters().getParameterNames();
+    }
+
+    @Override
+    public boolean isVarArgs()
+    {
+        return _method.getParameters().isVarArgs();
+    }
+
+    @Override
+    public JavaType[] getExceptionTypes()
+    {
+        return _method.getThrows().getTypes();
+    }
+
+}

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

View file
+/* ------------------------------------------------------------------------
+ * 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.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.util.MemberType;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ASTTypeInfo implements TypeInfo
+{
+    private final ASTClassOrInterfaceDeclaration _declaration;
+    private final ASTMethodInfo[] _methods;
+
+    public ASTTypeInfo(final ASTClassOrInterfaceDeclaration declaration)
+    {
+        _declaration = declaration;
+        _methods = buildMethods();
+    }
+
+    private ASTMethodInfo[] buildMethods()
+    {
+        final List<ASTMethodInfo> result = new ArrayList<ASTMethodInfo>();
+        for (ASTClassOrInterfaceBodyElement element : _declaration.getBody().getElements())
+        {
+            if (!element.isMember())
+            {
+                continue;
+            }
+            final ASTMember member = element.getMember();
+            if (member.getMember().getMemberType() != MemberType.METHOD)
+            {
+                continue;
+            }
+            result.add(new ASTMethodInfo((ASTMethodDeclaration) member.getMember()));
+        }
+        return result.toArray(new ASTMethodInfo[result.size()]);
+    }
+
+    @Override
+    public MethodInfo[] getMethods()
+    {
+        return _methods;
+    }
+
+    @Override
+    public String getName()
+    {
+        return null;  //To change body of implemented methods use File | Settings | File Templates.
+    }
+}

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

View file
  */
 package org.adjective.syntactic.convert.j7to8;
 
+import org.adjective.syntactic.parser.ast.ASTClassOrInterfaceDeclaration;
 import org.adjective.syntactic.parser.util.TypeParameter;
 
+import java.util.HashMap;
+import java.util.Map;
+
 public class ClassFinder
 {
+    private Map<String, ASTClassOrInterfaceDeclaration> _localTypes;
+
+    public ClassFinder()
+    {
+        _localTypes = new HashMap<String, ASTClassOrInterfaceDeclaration>();
+    }
+
     public boolean hasClass(final String qualifiedName)
     {
         return findClass(qualifiedName) != null;
         final Class<?> cls = findClass(qualifiedName);
         if (cls == null)
         {
-            return null;
+            ASTClassOrInterfaceDeclaration local = _localTypes.get(qualifiedName);
+            return getInfo(local);
         }
         return new ClassInfo(cls, parameters);
     }
 
+    private TypeInfo getInfo(ASTClassOrInterfaceDeclaration declaration)
+    {
+        return new ASTTypeInfo(declaration);
+    }
+
     private Class<?> findClass(final String qualifiedName)
     {
         try
             return null;
         }
     }
+
+    public void define(final ASTClassOrInterfaceDeclaration node)
+    {
+        _localTypes.put(node.getName().getIdentifier(), node);
+    }
 }

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

View file
     public Object visit(final ASTClassOrInterfaceDeclaration node, final Object data)
     {
         _className.push(node.getName().getIdentifier());
+        _classFinder.define(node);
         try
         {
             return super.visit(node, data);
         final Collection<MethodInfo> methods = filterMethods(typeInfo.getMethods());
         if (methods.size() != 1)
         {
-            throw new ConversionException("Class '" + typeInfo.getName() + "' should have exactly 1 method but has " + methods.size());
+            throw new ConversionException("Class '" + typeInfo.getName() + "' should have exactly 1 method but has " + methods
+                    .size());
         }
 
         MethodInfo method = methods.iterator().next();
         return makeMethodDeclaration(method, parameters, body);
     }
 
-    private <T> T last(T[] array) {
-        return array[array.length -1];
+    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);
         for (int i = 0; i < methods.length; i++)
         {
-            if(!isObjectMethod(methods[i])) {
+            if (!isObjectMethod(methods[i]))
+            {
                 filter.add(methods[i]);
             }
         }
         return filter;
     }
 
-    private boolean isObjectMethod(MethodInfo method) {
+    private boolean isObjectMethod(MethodInfo method)
+    {
         final Method[] objectMethods = Object.class.getMethods();
         for (int i = 0; i < objectMethods.length; i++)
         {
             Method objectMethod = objectMethods[i];
-            if(isMatchingMethod(objectMethod, method)) {
+            if (isMatchingMethod(objectMethod, method))
+            {
                 return true;
             }
         }
         {
             return false;
         }
-        if(objectMethod.getParameterTypes().length != method.getParameterTypes().length) {
+        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])) {
+        for (int i = 0; i < objectMethod.getParameterTypes().length; i++)
+        {
+            if (!isMatchingType(objectMethod.getParameterTypes()[i], method.getParameterTypes()[i]))
+            {
                 return false;
             }
         }
     {
         final JavaType[] parameterTypes = method.getParameterTypes();
         final String[] parameterNames = new String[parameterTypes.length];
-            final Iterator<ASTIdentifier> iterator = parameters.getParameterNames().iterator();
+        final Iterator<ASTIdentifier> iterator = parameters.getParameterNames().iterator();
         for (int i = 0; i < parameterNames.length; i++)
         {
             if (iterator.hasNext())

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

View file
 package org.adjective.syntactic.parser.ast;
 
+import org.adjective.syntactic.parser.util.JavaType;
+
 public class ASTFormalParameters extends org.adjective.syntactic.parser.BaseNode
 {
     public ASTFormalParameters(int id)
         setChildren(formalParameters);
     }
 
-    public <R,T> R jjtAccept(JavaParserVisitor<R,T> visitor, T data)
+    public <R, T> R jjtAccept(JavaParserVisitor<R, T> visitor, T data)
     {
         return visitor.visit(this, data);
     }
     {
         return allChildren(ASTFormalParameter.class);
     }
+
+    public JavaType[] getParameterTypes()
+    {
+        final JavaType[] types = new JavaType[jjtGetNumChildren()];
+        int i = 0;
+        for (ASTFormalParameter parameter : getParameters())
+        {
+            types[i++] = parameter.getActualType();
+        }
+        return types;
+    }
+
+    public String[] getParameterNames()
+    {
+        final String[] names = new String[jjtGetNumChildren()];
+        int i = 0;
+        for (ASTFormalParameter parameter : getParameters())
+        {
+            names[i++] = parameter.getName().getIdentifier();
+        }
+        return names;
+    }
+
+    public boolean isVarArgs()
+    {
+        return !isEmpty() && getLastChild().as(ASTFormalParameter.class).isVarArgs();
+    }
 }

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

View file
     {
         return child(ASTMethodBody.class);
     }
+
+    public JavaType getActualReturnType()
+    {
+        return makeType(getDeclaredReturnType(), getArraySuffix());
+    }
 }

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

View file
 
 import org.adjective.syntactic.parser.util.JavaType;
 import org.adjective.syntactic.parser.util.ParameterizedName;
+import org.adjective.syntactic.parser.util.SimpleJavaType;
 
-public
-class ASTNameList extends org.adjective.syntactic.parser.BaseNode {
-  public ASTNameList(int id) {
-    super(id);
-  }
+public class ASTNameList extends org.adjective.syntactic.parser.BaseNode
+{
+    public ASTNameList(int id)
+    {
+        super(id);
+    }
 
     public ASTNameList(final JavaType[] exceptionType)
     {
         return names;
     }
 
-    public <R,T> R jjtAccept(JavaParserVisitor<R,T> visitor, T data) {
-    return visitor.visit(this, data);
-  }
+    public <R, T> R jjtAccept(JavaParserVisitor<R, T> visitor, T data)
+    {
+        return visitor.visit(this, data);
+    }
+
+    public Iterable<ASTQualifiedIdentifier> getNames()
+    {
+        return allChildren(ASTQualifiedIdentifier.class);
+    }
+
+    public JavaType[] getTypes()
+    {
+        final JavaType[] types = new JavaType[jjtGetNumChildren()];
+        int i = 0;
+        for (ASTQualifiedIdentifier name : getNames())
+        {
+            types[i++] = new SimpleJavaType(name);
+        }
+        return types;
+    }
 }

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

View file
+/* ------------------------------------------------------------------------
+ * 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;
+
+import org.adjective.syntactic.parser.ast.ASTIdentifier;
+import org.adjective.syntactic.parser.ast.ASTQualifiedIdentifier;
+
+public class SimpleJavaType implements JavaType
+{
+    private final String _name;
+    private final ParameterizedName[] _parameterizedName;
+
+    public SimpleJavaType(final ASTQualifiedIdentifier name)
+    {
+        this(name.getName(), parameterize(name));
+    }
+
+    private static ParameterizedName[] parameterize(final ASTQualifiedIdentifier name)
+    {
+        final ParameterizedName[] parameterizedName = new ParameterizedName[name.jjtGetNumChildren()];
+        int i = 0;
+        for (ASTIdentifier identifier : name.getIdentifierComponents())
+        {
+            parameterizedName[i++] = new UnparameterizedName(identifier);
+        }
+        return parameterizedName;
+    }
+
+    private SimpleJavaType(final String name, ParameterizedName[] parameterizedName)
+    {
+        _name = name;
+        _parameterizedName = parameterizedName;
+    }
+
+    @Override
+    public BaseType getBaseType()
+    {
+        return BaseType.OBJECT;
+    }
+
+    @Override
+    public String getTypeName()
+    {
+        return _name;
+    }
+
+    @Override
+    public ParameterizedName[] getParameterizedTypeName()
+    {
+        return _parameterizedName;
+    }
+
+    @Override
+    public int getArrayDepth()
+    {
+        return 0;
+    }
+}

File parser/source/java/parser/org/adjective/syntactic/parser/util/UnparameterizedName.java

View file
  */
 package org.adjective.syntactic.parser.util;
 
+import org.adjective.syntactic.parser.ast.ASTIdentifier;
+
 public class UnparameterizedName implements ParameterizedName
 {
     private final String _name;
         _name = name;
     }
 
+    public UnparameterizedName(final ASTIdentifier identifier)
+    {
+        this(identifier.getIdentifier());
+    }
+
     @Override
     public TypeParameter[] getParameters()
     {