Commits

Tim Vernum  committed fe8a3bb

Translaete constructor references

  • Participants
  • Parent commits 30a0c26

Comments (0)

Files changed (14)

File convert/source/java/main/org/adjective/syntactic/convert/j8to7/AbstractASTConverter.java

 import org.adjective.syntactic.parser.ast.ASTMember;
 import org.adjective.syntactic.parser.ast.ASTMethodDeclaration;
 import org.adjective.syntactic.parser.ast.ASTModifiers;
+import org.adjective.syntactic.parser.ast.ASTNameList;
 import org.adjective.syntactic.parser.name.ParameterizedName;
 import org.adjective.syntactic.parser.name.SimpleParameterizedName;
 import org.adjective.syntactic.parser.name.SimpleTypeParameter;
 
         return methods.iterator().next();
     }
+
+    protected TypeInfo findType(final String name)
+    {
+        String qualifiedName = _importer.getQualifiedName(name);
+        if (qualifiedName == null)
+        {
+            qualifiedName = name;
+        }
+        final TypeInfo typeInfo = _classFinder.getClass(qualifiedName, new TypeParameter[0]);
+        return typeInfo;
+    }
+
+    protected ASTNameList getExceptions(final MethodInfo method)
+    {
+        return method.getExceptionTypes().length == 0 ? null
+                : new ASTNameList(method.getExceptionTypes());
+    }
 }

File convert/source/java/main/org/adjective/syntactic/convert/j8to7/LambdaConverter.java

     private ASTMethodDeclaration makeMethodDeclaration(final MethodInfo method, final ASTLambdaParameters parameters, final ASTLambdaBody lambdaBody)
     {
         final ASTMethodBody body = getMethodBody(lambdaBody);
-        final ASTNameList exceptions = method.getExceptionTypes().length == 0 ? null
-                : new ASTNameList(method.getExceptionTypes());
+        final ASTNameList exceptions = getExceptions(method);
         return new ASTMethodDeclaration(method.getReturnType(),
                                         new ASTIdentifier(method.getName()),
                                         makeFormalParameters(method, parameters),

File convert/source/java/main/org/adjective/syntactic/convert/j8to7/MemberReferenceConverter.java

 package org.adjective.syntactic.convert.j8to7;
 
 import org.adjective.syntactic.convert.j8to7.type.MethodInfo;
+import org.adjective.syntactic.convert.j8to7.type.TypeInfo;
 import org.adjective.syntactic.convert.j8to7.util.ClassFinder;
 import org.adjective.syntactic.convert.j8to7.util.Importer;
-import org.adjective.syntactic.parser.ast.ASTArraySuffixList;
-import org.adjective.syntactic.parser.ast.ASTBlock;
-import org.adjective.syntactic.parser.ast.ASTFormalParameter;
-import org.adjective.syntactic.parser.ast.ASTFormalParameters;
-import org.adjective.syntactic.parser.ast.ASTIdentifier;
-import org.adjective.syntactic.parser.ast.ASTMemberReference;
-import org.adjective.syntactic.parser.ast.ASTMethodBody;
-import org.adjective.syntactic.parser.ast.ASTMethodDeclaration;
-import org.adjective.syntactic.parser.ast.ASTNameList;
+import org.adjective.syntactic.parser.ast.*;
 import org.adjective.syntactic.parser.node.ExpressionNode;
+import org.adjective.syntactic.parser.node.StatementNode;
 import org.adjective.syntactic.parser.type.JavaType;
+import org.adjective.syntactic.parser.type.SimpleJavaType;
 
-public class MemberReferenceConverter extends AbstractASTConverter {
+public class MemberReferenceConverter extends AbstractASTConverter
+{
 
     public MemberReferenceConverter(final Importer importer, final ClassFinder classFinder)
     {
     private ASTMethodDeclaration makeMethodDeclaration(final MethodInfo method, final ExpressionNode target, final ASTMemberReference reference)
     {
         final ASTFormalParameters parameters = makeFormalParameters(method);
-        // TODO
-        final ASTNameList exceptions = new ASTNameList();
-        final ASTMethodBody body = new ASTMethodBody(new ASTBlock());
+        final ASTNameList exceptions = getExceptions(method);
 
+        StatementNode statement = null;
+        if (target instanceof ASTQualifiedIdentifier)
+        {
+            // Might be a class reference [static member ref, receiver member ref, or constructor ref]
+            final TypeInfo refType = findType(target.as(ASTQualifiedIdentifier.class).getName());
+            if (refType != null)
+            {
+                statement = makeMethodBody(method, refType, reference);
+            }
+        }
+        if (statement == null)
+        {
+            statement = makeMethodBody(method, target, reference);
+        }
+
+        final ASTMethodBody body = new ASTMethodBody(new ASTBlock(statement));
         return new ASTMethodDeclaration(method.getReturnType(),
                                         new ASTIdentifier(method.getName()),
                                         parameters,
                                         body);
     }
 
+    private StatementNode makeMethodBody(final MethodInfo method, final ExpressionNode target, final ASTMemberReference reference)
+    {
+        // TODO
+        return new ASTEmptyStatement();
+    }
+
+    private StatementNode makeMethodBody(final MethodInfo method, final TypeInfo refType, final ASTMemberReference reference)
+    {
+        final SimpleJavaType javaType = new SimpleJavaType(refType.getName());
+        if (reference.isConstructorReference())
+        {
+            final String[] parameterNames = method.getParameterNames();
+            final ASTQualifiedIdentifier[] argNames = new ASTQualifiedIdentifier[parameterNames.length];
+            for (int i = 0; i < parameterNames.length; i++)
+            {
+                argNames[i] = new ASTQualifiedIdentifier(parameterNames[i]);
+            }
+            final ASTArguments arguments = new ASTArguments(new ASTArgumentList(argNames));
+            return new ASTReturnStatement(new ASTAllocationExpression(javaType, arguments, null));
+        }
+        return new ASTEmptyStatement();
+    }
+
     private ASTFormalParameters makeFormalParameters(final MethodInfo method)
     {
         final JavaType[] parameterTypes = method.getParameterTypes();

File convert/source/java/main/org/adjective/syntactic/convert/j8to7/type/ClassType.java

 
 public class ClassType implements JavaType
 {
-
     private Class<?> _cls;
 
     public ClassType(Class<?> cls)

File convert/source/java/main/org/adjective/syntactic/convert/j8to7/util/ClassFinder.java

         if (cls == null)
         {
             ASTClassOrInterfaceDeclaration local = _localTypes.get(qualifiedName);
+            if (local == null)
+            {
+                return null;
+            }
             return new ASTTypeInfo(local, parameters);
         }
         return new ClassInfo(cls, parameters);

File convert/source/java/samples/sample/MemberReferences.java

 
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
+import java.util.Date;
 import java.util.concurrent.Callable;
 
 public class MemberReferences
 {
+    public interface Factory<RET, ARG> {
+        public RET create(ARG arg);
+    }
+
     private static void execute()
     {
 
         Callable<String> c = this::toString;
         ChangeListener listener = ChangeEvent::toString;
         Runnable runnable = MemberReferences::execute;
+        Callable<Date> cd = Date::new;
+        Factory<?, String> factory = Date::new;
     }
 
 }

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

         tokenNew.image = "new";
         tokenNew.kind= JavaParserConstants.NEW;
         jjtSetFirstToken(tokenNew);
-        setChildren(super.makeTypeNode(type), arguments, body);
+        setChildren(super.makeTypeNode(type), arguments);
+        if (body != null)
+        {
+            appendChild(body);
+        }
     }
 
     public <R, T> R jjtAccept(JavaParserVisitor<R, T> visitor, T data)

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

 package org.adjective.syntactic.parser.ast;
 
 import org.adjective.syntactic.parser.node.BaseNode;
+import org.adjective.syntactic.parser.node.ExpressionNode;
+import org.adjective.syntactic.parser.node.Nodes;
 
 public class ASTArgumentList extends BaseNode
 {
         super(id);
     }
 
+    public ASTArgumentList(ASTExpression... arguments)
+    {
+        this(JavaParserTreeConstants.JJTARGUMENTLIST);
+        setChildren(arguments);
+    }
+
+    public ASTArgumentList(ExpressionNode... arguments)
+    {
+        this(ASTExpression.array(arguments));
+    }
+
     public <R,T> R jjtAccept(JavaParserVisitor<R,T> visitor, T data)
     {
         return visitor.visit(this, data);
     }
 
-    public Iterable<ASTExpression> getArguments()
+    public Nodes<ASTExpression> getArguments()
     {
         return allChildren(ASTExpression.class);
     }

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

         this(JavaParserTreeConstants.JJTARGUMENTS);
     }
 
+    public ASTArguments(ASTArgumentList argumentList)
+    {
+        this();
+        setChildren(argumentList);
+    }
+
     public <R,T> R jjtAccept(JavaParserVisitor<R,T> visitor, T data)
     {
         return visitor.visit(this, data);

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

         super(id);
     }
 
+    public ASTEmptyStatement()
+    {
+        this(JavaParserTreeConstants.JJTEMPTYSTATEMENT);
+    }
+
     public <R, T> R jjtAccept(JavaParserVisitor<R, T> visitor, T data)
     {
         return visitor.visit(this, data);

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

     {
         return jjtGetNumChildren() == 3 ? child(2).as(ASTExpression.class) : null;
     }
+
+    public static ASTExpression[] array(final ExpressionNode[] expressions)
+    {
+        final ASTExpression[] ast = new ASTExpression[expressions.length];
+        for (int i = 0; i < ast.length; i++)
+        {
+            ast[i] = new ASTExpression(expressions[i]);
+        }
+        return ast;
+    }
 }

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

 package org.adjective.syntactic.parser.ast;
 
 import org.adjective.syntactic.parser.node.BaseNode;
+import org.adjective.syntactic.parser.node.ExpressionNode;
 import org.adjective.syntactic.parser.node.StatementNode;
 
 public class ASTReturnStatement extends BaseNode implements StatementNode
         setChildren(expression);
     }
 
+    public ASTReturnStatement(final ExpressionNode expression)
+    {
+        this(new ASTExpression(expression));
+    }
+
     public ASTExpression getExpression()
     {
         return getFirstChild().as(ASTExpression.class);

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

 import org.adjective.syntactic.parser.type.JavaType;
 import org.adjective.syntactic.parser.node.TypeNode;
 import org.adjective.syntactic.parser.name.TypeParameter;
+import org.adjective.syntactic.parser.type.SimpleJavaType;
 
 public class ASTTypeArgument extends BaseNode implements TypeParameter
 {
     @Override
     public Kind getKind()
     {
+        if (isEmpty())
+        {
+            return Kind.EXTENDS;
+        }
         if(isWildcard()) {
             return getFirstChild().as(ASTWildcardBounds.class).getKind();
         } else {
     @Override
     public JavaType getType()
     {
+        if (isEmpty())
+        {
+            return new SimpleJavaType("Object");
+        }
         if(isWildcard()) {
             return getFirstChild().as(ASTWildcardBounds.class).getType();
         } else {

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

     {
         this(name.getName(), parameterize(name), 0);
     }
+    public SimpleJavaType(final String name)
+    {
+        this(name, parameterize(name), 0);
+    }
 
     public SimpleJavaType(final ParameterizedName[] name, int arrayDepth)
     {
         return parameterizedName;
     }
 
+    private static ParameterizedName[] parameterize(final String name)
+    {
+        final String[] split = name.split("\\.");
+        final ParameterizedName[] parameterizedName = new ParameterizedName[split.length];
+        for (int i = 0; i < parameterizedName.length; i++)
+        {
+            parameterizedName[i] = new UnparameterizedName(split[i]);
+        }
+        return parameterizedName;
+    }
+
     private static String combine(final ParameterizedName[] name)
     {
         StringBuilder builder = new StringBuilder(name[0].getName());