rpau / javalang-compiler

Java compiler elements (symbol and type tables) to perform code semantic analysis
GNU Lesser General Public License v3.0
10 stars 4 forks source link

Parametrized class error #20

Closed cal101 closed 8 years ago

cal101 commented 8 years ago
<T> void doClass(Class<T> clazz) {

        }

...
void x() {
            doClass(null);
}

results in

Caused by: java.lang.RuntimeException: Error loading methods in a given scope
    at org.walkmod.javalang.compiler.actions.LoadMethodDeclarationsAction$MethodsPopulator.loadMethods(LoadMethodDeclarationsAction.java:254)
    at org.walkmod.javalang.compiler.actions.LoadMethodDeclarationsAction$MethodsPopulator.visit(LoadMethodDeclarationsAction.java:227)
    at org.walkmod.javalang.compiler.actions.LoadMethodDeclarationsAction$MethodsPopulator.visit(LoadMethodDeclarationsAction.java:192)
    at org.walkmod.javalang.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:136)
    at org.walkmod.javalang.compiler.actions.LoadMethodDeclarationsAction.doPush(LoadMethodDeclarationsAction.java:187)
    at org.walkmod.javalang.compiler.symbols.SymbolAction.execute(SymbolAction.java:25)
    at org.walkmod.javalang.compiler.symbols.Symbol.invokeActions(Symbol.java:144)
    at org.walkmod.javalang.compiler.symbols.SymbolTable.invokeActions(SymbolTable.java:201)
    at org.walkmod.javalang.compiler.symbols.SymbolTable.pushSymbol(SymbolTable.java:342)
    ... 23 more
Caused by: org.walkmod.javalang.exceptions.NoSuchExpressionTypeException: Error resolving the signature of the method doClass at [86, 9]
    at org.walkmod.javalang.compiler.types.TypeVisitorAdapter.visit(TypeVisitorAdapter.java:1147)
    at org.walkmod.javalang.compiler.types.TypeVisitorAdapter.visit(TypeVisitorAdapter.java:115)
    at org.walkmod.javalang.ast.body.MethodDeclaration.accept(MethodDeclaration.java:207)
    at org.walkmod.javalang.compiler.actions.LoadMethodDeclarationsAction.pushMethod(LoadMethodDeclarationsAction.java:126)
    at org.walkmod.javalang.compiler.actions.LoadMethodDeclarationsAction.access$100(LoadMethodDeclarationsAction.java:59)
    at org.walkmod.javalang.compiler.actions.LoadMethodDeclarationsAction$MethodsPopulator.loadMethods(LoadMethodDeclarationsAction.java:247)
    ... 31 more
Caused by: org.walkmod.javalang.exceptions.NoSuchExpressionTypeException: Error locating method doClass with type args [java.lang.Class<java.lang.Object>] and type params {T=java.lang.Object} for parameters [Class<T> clazz] in current class
    at org.walkmod.javalang.compiler.types.TypeVisitorAdapter.visit(TypeVisitorAdapter.java:1137)
    ... 36 more

Exception is because of extra exception I added (https://github.com/cal101/javalang-compiler/commit/ae21e10aa51b72cc6c4361b069aa68fcb3a0e8b8).

rpau commented 8 years ago

Cal, I have created a local test with your code and I can't reproduce the error. I am deploying a new version to maven (2.2.9) to ensure that you are executing the changes made on Friday

cal101 commented 8 years ago

Here a complete test case:

package de.walkmod.typedclassliteral;

public class ClassLiteral {
    <T> T doClass(String x, Class<T> clazz) {
        return null;
    }

    public void x() {
        doClass("x", null);
    }
}

with my code gives

Caused by: org.walkmod.javalang.exceptions.NoSuchExpressionTypeException: java.lang.IllegalArgumentException: symboldata is null for pos 1 in args ["x", null], expression null
        ... 35 more
Caused by: java.lang.IllegalArgumentException: symboldata is null for pos 1 in args ["x", null], expression null
        at org.walkmod.javalang.compiler.reflection.AbstractGenericsBuilderFromParameterTypes.loadTypeMappingFromTypeArgs(AbstractGenericsBuilderFromParameterTypes.java:164)
        at org.walkmod.javalang.compiler.reflection.AbstractGenericsBuilderFromParameterTypes.loadTypeMappingFromTypeArgs(AbstractGenericsBuilderFromParameterTypes.java:131)
        at org.walkmod.javalang.compiler.reflection.AbstractGenericsBuilderFromParameterTypes.buildTypeParamsTypes(AbstractGenericsBuilderFromParameterTypes.java:192)
        at org.walkmod.javalang.compiler.reflection.GenericsBuilderFromMethodParameterTypes.build(GenericsBuilderFromMethodParameterTypes.java:69)
        at org.walkmod.javalang.compiler.types.TypeVisitorAdapter.visit(TypeVisitorAdapter.java:475)
        ... 34 more

with your code gives:

Caused by: org.walkmod.javalang.exceptions.NoSuchExpressionTypeException: java.lang.NullPointerException
    ... 35 more
Caused by: java.lang.NullPointerException
    at org.walkmod.javalang.compiler.reflection.AbstractGenericsBuilderFromParameterTypes.loadTypeMappingFromTypeArgs(AbstractGenericsBuilderFromParameterTypes.java:160)
    at org.walkmod.javalang.compiler.reflection.AbstractGenericsBuilderFromParameterTypes.loadTypeMappingFromTypeArgs(AbstractGenericsBuilderFromParameterTypes.java:131)
    at org.walkmod.javalang.compiler.reflection.AbstractGenericsBuilderFromParameterTypes.buildTypeParamsTypes(AbstractGenericsBuilderFromParameterTypes.java:186)
    at org.walkmod.javalang.compiler.reflection.GenericsBuilderFromMethodParameterTypes.build(GenericsBuilderFromMethodParameterTypes.java:69)
    at org.walkmod.javalang.compiler.types.TypeVisitorAdapter.visit(TypeVisitorAdapter.java:473)
    ... 34 more
    public void loadTypeMappingFromTypeArgs(SymbolTable symbolTable) throws Exception {
...
                                 if (e == null) {
                                         throw new IllegalArgumentException("expression is null for pos " + pos + " in args " + args);
                                  }
                                  if (e.getSymbolData() == null) {
                                         throw new IllegalArgumentException("symboldata is null for pos " + pos + " in args " + args + ", expression " + e);
                                   }
                List<SymbolData> params = e.getSymbolData().getParameterizedTypes();
rpau commented 8 years ago

Cal,

Sorry if I have a obvious question, but have you tried this upgrading the javalang-compiler with the last changes? Now it should be 2.2.9

cal101 commented 8 years ago

I did but just to be sure I repeated:

rm -rf ~/.ivy2
...
found org.walkmod#javalang-compiler;2.2.9 in maven2

Caused by: java.lang.NullPointerException
    at org.walkmod.javalang.compiler.reflection.AbstractGenericsBuilderFromParameterTypes.loadTypeMappingFromTypeArgs(AbstractGenericsBuilderFromParameterTypes.java:160)