Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE when compiling simple enum class with lambda expressions #16

Closed
vegegoku opened this issue Nov 20, 2018 · 3 comments
Closed

NPE when compiling simple enum class with lambda expressions #16

vegegoku opened this issue Nov 20, 2018 · 3 comments
Assignees
Labels

Comments

@vegegoku
Copy link

Describe the bug
I have a simple enum java class that define an inner Functional interface, the interface is used a constructor argument for the enum class, enum entires are defined passing a lambada expression of that functional interface.

compiling with j2cl throws a NullPointerException.

To Reproduce
we can reproduce this issue in using the hello world sample:

  • add the following class next to the HelloWorld.java
package com.google.j2cl.samples.helloworldlib;

public enum Unit{
    px(value -> value + "px"),
    em(value -> value + "em");

    private final UnitFormatter unitFormatter;

    Unit(UnitFormatter unitFormatter) {
        this.unitFormatter = unitFormatter;
    }

    public String of(Number value) {
        return unitFormatter.format(value);
    }

    @FunctionalInterface
    public interface UnitFormatter{
        String format(Number value);
    }
}
  • try to transpile the Hello world sample

this will produce an exception with the following stack trace

ures.wrapAndThrowUnchecked(Futures.java:1546)
        at com.google.common.util.concurrent.Futures.getUnchecked(Futures.java:1532)
        at com.google.j2cl.transpiler.J2clTranspiler.transpile(J2clTranspiler.java:111)
        at com.google.j2cl.transpiler.BazelJ2clBuilder.run(BazelJ2clBuilder.java:74)
        at com.google.j2cl.bazel.BazelWorker.processRequest(BazelWorker.java:62)
        at com.google.j2cl.bazel.BazelWorker.execute(BazelWorker.java:116)
        at com.google.j2cl.bazel.BazelWorker.runPersistentWorker(BazelWorker.java:98)
        at com.google.j2cl.bazel.BazelWorker.start(BazelWorker.java:71)
        at com.google.j2cl.transpiler.BazelJ2clBuilder.main(BazelJ2clBuilder.java:158)
Caused by: java.lang.NullPointerException
        at com.google.j2cl.frontend.CompilationUnitBuilder$ASTConverter.convert(CompilationUnitBuilder.java:1617)
        at com.google.j2cl.frontend.CompilationUnitBuilder$ASTConverter.convert(CompilationUnitBuilder.java:647)
        at com.google.j2cl.frontend.CompilationUnitBuilder$ASTConverter.convert(CompilationUnitBuilder.java:1384)
        at com.google.j2cl.frontend.CompilationUnitBuilder$ASTConverter.convert(CompilationUnitBuilder.java:627)
        at com.google.j2cl.frontend.CompilationUnitBuilder$ASTConverter.convertLambdaBody(CompilationUnitBuilder.java:1037)
        at com.google.j2cl.frontend.CompilationUnitBuilder$ASTConverter.convert(CompilationUnitBuilder.java:1023)
        at com.google.j2cl.frontend.CompilationUnitBuilder$ASTConverter.convert(CompilationUnitBuilder.java:631)
        at java.base/java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
        at java.base/java.util.Iterator.forEachRemaining(Unknown Source)
        at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
        at java.base/java.util.stream.AbstractPipeline.copyInto(Unknown Source)
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
        at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
        at java.base/java.util.stream.AbstractPipeline.evaluate(Unknown Source)
        at java.base/java.util.stream.ReferencePipeline.collect(Unknown Source)
        at com.google.j2cl.frontend.CompilationUnitBuilder$ASTConverter.convertArguments(CompilationUnitBuilder.java:1501)
        at com.google.j2cl.frontend.CompilationUnitBuilder$ASTConverter.convert(CompilationUnitBuilder.java:299)
        at com.google.j2cl.frontend.CompilationUnitBuilder$ASTConverter.convert(CompilationUnitBuilder.java:208)
        at com.google.j2cl.frontend.CompilationUnitBuilder$ASTConverter.convert(CompilationUnitBuilder.java:190)
        at com.google.j2cl.frontend.CompilationUnitBuilder$ASTConverter.convert(CompilationUnitBuilder.java:177)
        at com.google.j2cl.frontend.CompilationUnitBuilder$ASTConverter.access$100(CompilationUnitBuilder.java:133)
        at com.google.j2cl.frontend.CompilationUnitBuilder.buildCompilationUnit(CompilationUnitBuilder.java:1914)
        at com.google.j2cl.frontend.CompilationUnitBuilder.lambda$build$0(CompilationUnitBuilder.java:1936)
        at java.base/java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
        at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(Unknown Source)
        at java.base/java.util.stream.AbstractPipeline.copyInto(Unknown Source)
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
        at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
        at java.base/java.util.stream.AbstractPipeline.evaluate(Unknown Source)
        at java.base/java.util.stream.ReferencePipeline.collect(Unknown Source)
        at com.google.j2cl.frontend.CompilationUnitBuilder.build(CompilationUnitBuilder.java:1938)
        at com.google.j2cl.transpiler.J2clTranspiler.convertUnits(J2clTranspiler.java:141)
        at com.google.j2cl.transpiler.J2clTranspiler.transpileImpl(J2clTranspiler.java:125)
        at com.google.j2cl.transpiler.J2clTranspiler.lambda$transpile$0(J2clTranspiler.java:110)
        at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)
---8<---8<--- End of log snippet, 791 chars omitted ---8<---8<---
Target //src/main/java/com/google/j2cl/samples/helloworld:helloworld failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.388s, Critical Path: 1.15s, Remote (0.00% of the time): [queue: 0.00%, setup: 0.00%, process: 0.00%]
INFO: 2 processes: 1 local, 1 worker.
FAILED: Build did NOT complete successfully

for some reason the CurrentType is being Null in the CompilationUnitBuilder during the process of converting the lambda expression body.

expanding the lamda to anonymous classes works.

@rluble rluble self-assigned this Nov 20, 2018
@rluble rluble added the bug label Nov 20, 2018
@rluble
Copy link
Collaborator

rluble commented Nov 20, 2018

Thanks for the clear repro.

@rluble
Copy link
Collaborator

rluble commented Nov 21, 2018

Fixed by 948bfb8. Please verify.

@rluble rluble closed this as completed Nov 21, 2018
@vegegoku
Copy link
Author

Fix confirmed, it is working now. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants