-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(java): compilation fails with "code too large" (#1605)
In cases where a module includes a large amount of types, compilation of Java packages could fail with the "code too large" error. This is due to the presence of a single method in the `$Module` class that contains a reference to all types in the package, resulting in a large amount of generated bytecode (more than Java's maximum of 64KB). In order to remediate, moved the FQN to Class mapping to a resource file that gets loaded when the `$Module` class is initialized. The individual `Class` entries are then resolved at the last minute using `Class.forName` to ensure the reoslution happens *after* the full mapping has been processed.
- Loading branch information
1 parent
fe58cc2
commit b9ec853
Showing
9 changed files
with
489 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 49 additions & 5 deletions
54
...-of-base/java/src/main/java/software/amazon/jsii/tests/calculator/baseofbase/$Module.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,63 @@ | ||
package software.amazon.jsii.tests.calculator.baseofbase; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.UncheckedIOException; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import software.amazon.jsii.JsiiModule; | ||
|
||
public final class $Module extends JsiiModule { | ||
private static final Map<String, String> MODULE_TYPES = load(); | ||
|
||
private static Map<String, String> load() { | ||
final Map<String, String> result = new HashMap<>(); | ||
final ClassLoader cl = $Module.class.getClassLoader(); | ||
try (final InputStream is = cl.getResourceAsStream("software/amazon/jsii/tests/calculator/baseofbase/$Module.txt"); | ||
final Reader rd = new InputStreamReader(is, StandardCharsets.UTF_8); | ||
final BufferedReader br = new BufferedReader(rd)) { | ||
br.lines() | ||
.filter(line -> !line.trim().isEmpty()) | ||
.forEach(line -> { | ||
final String[] parts = line.split("=", 2); | ||
final String fqn = parts[0]; | ||
final String className = parts[1]; | ||
result.put(fqn, className); | ||
}); | ||
} | ||
catch (final IOException exception) { | ||
throw new UncheckedIOException(exception); | ||
} | ||
return result; | ||
} | ||
|
||
private final Map<String, Class<?>> cache = new HashMap<>(); | ||
|
||
public $Module() { | ||
super("@scope/jsii-calc-base-of-base", "0.0.0", $Module.class, "jsii-calc-base-of-base@0.0.0.jsii.tgz"); | ||
} | ||
|
||
@Override | ||
protected Class<?> resolveClass(final String fqn) throws ClassNotFoundException { | ||
switch (fqn) { | ||
case "@scope/jsii-calc-base-of-base.IVeryBaseInterface": return software.amazon.jsii.tests.calculator.baseofbase.IVeryBaseInterface.class; | ||
case "@scope/jsii-calc-base-of-base.Very": return software.amazon.jsii.tests.calculator.baseofbase.Very.class; | ||
case "@scope/jsii-calc-base-of-base.VeryBaseProps": return software.amazon.jsii.tests.calculator.baseofbase.VeryBaseProps.class; | ||
default: throw new ClassNotFoundException("Unknown JSII type: " + fqn); | ||
if (!MODULE_TYPES.containsKey(fqn)) { | ||
throw new ClassNotFoundException("Unknown JSII type: " + fqn); | ||
} | ||
return this.cache.computeIfAbsent(MODULE_TYPES.get(fqn), this::findClass); | ||
} | ||
|
||
private Class<?> findClass(final String binaryName) { | ||
try { | ||
return Class.forName(binaryName); | ||
} | ||
catch (final ClassNotFoundException exception) { | ||
throw new RuntimeException(exception); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...base/java/src/main/resources/software/amazon/jsii/tests/calculator/baseofbase/$Module.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@scope/jsii-calc-base-of-base.IVeryBaseInterface=software.amazon.jsii.tests.calculator.baseofbase.IVeryBaseInterface | ||
@scope/jsii-calc-base-of-base.Very=software.amazon.jsii.tests.calculator.baseofbase.Very | ||
@scope/jsii-calc-base-of-base.VeryBaseProps=software.amazon.jsii.tests.calculator.baseofbase.VeryBaseProps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...-calc-base/java/src/main/resources/software/amazon/jsii/tests/calculator/base/$Module.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@scope/jsii-calc-base.Base=software.amazon.jsii.tests.calculator.base.Base | ||
@scope/jsii-calc-base.BaseProps=software.amazon.jsii.tests.calculator.base.BaseProps | ||
@scope/jsii-calc-base.IBaseInterface=software.amazon.jsii.tests.calculator.base.IBaseInterface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...ii-calc-lib/java/src/main/resources/software/amazon/jsii/tests/calculator/lib/$Module.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@scope/jsii-calc-lib.EnumFromScopedModule=software.amazon.jsii.tests.calculator.lib.EnumFromScopedModule | ||
@scope/jsii-calc-lib.IDoublable=software.amazon.jsii.tests.calculator.lib.IDoublable | ||
@scope/jsii-calc-lib.IFriendly=software.amazon.jsii.tests.calculator.lib.IFriendly | ||
@scope/jsii-calc-lib.IThreeLevelsInterface=software.amazon.jsii.tests.calculator.lib.IThreeLevelsInterface | ||
@scope/jsii-calc-lib.MyFirstStruct=software.amazon.jsii.tests.calculator.lib.MyFirstStruct | ||
@scope/jsii-calc-lib.Number=software.amazon.jsii.tests.calculator.lib.Number | ||
@scope/jsii-calc-lib.Operation=software.amazon.jsii.tests.calculator.lib.Operation | ||
@scope/jsii-calc-lib.StructWithOnlyOptionals=software.amazon.jsii.tests.calculator.lib.StructWithOnlyOptionals | ||
@scope/jsii-calc-lib.Value=software.amazon.jsii.tests.calculator.lib.Value |
Oops, something went wrong.