diff --git a/core/src/main/java/org/teavm/model/transformation/AssertionRemoval.java b/core/src/main/java/org/teavm/model/transformation/AssertionRemoval.java new file mode 100644 index 000000000..587a6b1d6 --- /dev/null +++ b/core/src/main/java/org/teavm/model/transformation/AssertionRemoval.java @@ -0,0 +1,62 @@ +/* + * Copyright 2022 Alexey Andreev. + * + * 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.teavm.model.transformation; + +import org.teavm.model.BasicBlock; +import org.teavm.model.ClassHolder; +import org.teavm.model.ClassHolderTransformer; +import org.teavm.model.ClassHolderTransformerContext; +import org.teavm.model.Instruction; +import org.teavm.model.MethodHolder; +import org.teavm.model.MethodReference; +import org.teavm.model.Program; +import org.teavm.model.instructions.IntegerConstantInstruction; +import org.teavm.model.instructions.InvokeInstruction; + +public class AssertionRemoval implements ClassHolderTransformer { + private static final MethodReference ASSERTION_METHOD = new MethodReference(Class.class, "desiredAssertionStatus", + boolean.class); + + @Override + public void transformClass(ClassHolder cls, ClassHolderTransformerContext context) { + for (MethodHolder method : cls.getMethods()) { + if (method.getProgram() != null) { + removeAssertions(method.getProgram()); + } + } + } + + private void removeAssertions(Program program) { + for (BasicBlock block : program.getBasicBlocks()) { + for (Instruction instruction : block) { + if (instruction instanceof InvokeInstruction) { + InvokeInstruction invoke = (InvokeInstruction) instruction; + if (invoke.getInstance() != null && invoke.getMethod().equals(ASSERTION_METHOD)) { + if (invoke.getReceiver() == null) { + invoke.delete(); + } else { + IntegerConstantInstruction replacement = new IntegerConstantInstruction(); + replacement.setConstant(0); + replacement.setReceiver(invoke.getReceiver()); + replacement.setLocation(invoke.getLocation()); + invoke.replace(replacement); + } + } + } + } + } + } +} diff --git a/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java b/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java index 14d8c8476..10e1ca1a3 100644 --- a/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java +++ b/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java @@ -57,6 +57,7 @@ import org.teavm.model.ClassHolderTransformer; import org.teavm.model.ClassReader; import org.teavm.model.PreOptimizingClassHolderSource; import org.teavm.model.ReferenceCache; +import org.teavm.model.transformation.AssertionRemoval; import org.teavm.parsing.ClasspathClassHolderSource; import org.teavm.tooling.sources.SourceFileProvider; import org.teavm.tooling.sources.SourceFilesCopier; @@ -111,6 +112,7 @@ public class TeaVMTool { private boolean longjmpSupported = true; private boolean heapDump; private boolean shortFileNames; + private boolean assertionsRemoved; public File getTargetDirectory() { return targetDirectory; @@ -268,6 +270,10 @@ public class TeaVMTool { this.shortFileNames = shortFileNames; } + public void setAssertionsRemoved(boolean assertionsRemoved) { + this.assertionsRemoved = assertionsRemoved; + } + public void setProgressListener(TeaVMProgressListener progressListener) { this.progressListener = progressListener; } @@ -406,6 +412,10 @@ public class TeaVMTool { vm.setProgressListener(progressListener); } + if (assertionsRemoved) { + vm.add(new AssertionRemoval()); + } + vm.setProperties(properties); vm.setProgramCache(incremental ? programCache : EmptyProgramCache.INSTANCE); vm.setCacheStatus(cacheStatus); diff --git a/tools/core/src/main/java/org/teavm/tooling/builder/BuildStrategy.java b/tools/core/src/main/java/org/teavm/tooling/builder/BuildStrategy.java index 5b1ea9cd7..1f60f31ac 100644 --- a/tools/core/src/main/java/org/teavm/tooling/builder/BuildStrategy.java +++ b/tools/core/src/main/java/org/teavm/tooling/builder/BuildStrategy.java @@ -84,5 +84,7 @@ public interface BuildStrategy { void setShortFileNames(boolean shortFileNames); + void setAssertionsRemoved(boolean assertionsRemoved); + BuildResult build() throws BuildException; } diff --git a/tools/core/src/main/java/org/teavm/tooling/builder/InProcessBuildStrategy.java b/tools/core/src/main/java/org/teavm/tooling/builder/InProcessBuildStrategy.java index 8310db04f..ff45d55b7 100644 --- a/tools/core/src/main/java/org/teavm/tooling/builder/InProcessBuildStrategy.java +++ b/tools/core/src/main/java/org/teavm/tooling/builder/InProcessBuildStrategy.java @@ -69,6 +69,7 @@ public class InProcessBuildStrategy implements BuildStrategy { private Properties properties = new Properties(); private TeaVMToolLog log = new EmptyTeaVMToolLog(); private boolean shortFileNames; + private boolean assertionsRemoved; public InProcessBuildStrategy(ClassLoaderFactory classLoaderFactory) { this.classLoaderFactory = classLoaderFactory; @@ -226,6 +227,11 @@ public class InProcessBuildStrategy implements BuildStrategy { this.shortFileNames = shortFileNames; } + @Override + public void setAssertionsRemoved(boolean assertionsRemoved) { + this.assertionsRemoved = assertionsRemoved; + } + @Override public BuildResult build() throws BuildException { TeaVMTool tool = new TeaVMTool(); @@ -257,6 +263,7 @@ public class InProcessBuildStrategy implements BuildStrategy { tool.setLongjmpSupported(longjmpSupported); tool.setHeapDump(heapDump); tool.setShortFileNames(shortFileNames); + tool.setAssertionsRemoved(assertionsRemoved); tool.getProperties().putAll(properties); diff --git a/tools/core/src/main/java/org/teavm/tooling/builder/RemoteBuildStrategy.java b/tools/core/src/main/java/org/teavm/tooling/builder/RemoteBuildStrategy.java index 165bc9068..7f9e870e4 100644 --- a/tools/core/src/main/java/org/teavm/tooling/builder/RemoteBuildStrategy.java +++ b/tools/core/src/main/java/org/teavm/tooling/builder/RemoteBuildStrategy.java @@ -200,6 +200,11 @@ public class RemoteBuildStrategy implements BuildStrategy { request.shortFileNames = shortFileNames; } + @Override + public void setAssertionsRemoved(boolean assertionsRemoved) { + request.assertionsRemoved = assertionsRemoved; + } + @Override public BuildResult build() throws BuildException { RemoteBuildResponse response; diff --git a/tools/core/src/main/java/org/teavm/tooling/daemon/BuildDaemon.java b/tools/core/src/main/java/org/teavm/tooling/daemon/BuildDaemon.java index fea6b6665..524769b00 100644 --- a/tools/core/src/main/java/org/teavm/tooling/daemon/BuildDaemon.java +++ b/tools/core/src/main/java/org/teavm/tooling/daemon/BuildDaemon.java @@ -164,6 +164,7 @@ public class BuildDaemon extends UnicastRemoteObject implements RemoteBuildServi tool.setLongjmpSupported(request.longjmpSupported); tool.setHeapDump(request.heapDump); tool.setShortFileNames(request.shortFileNames); + tool.setAssertionsRemoved(request.assertionsRemoved); for (String sourceDirectory : request.sourceDirectories) { tool.addSourceFileProvider(new DirectorySourceFileProvider(new File(sourceDirectory))); diff --git a/tools/core/src/main/java/org/teavm/tooling/daemon/RemoteBuildRequest.java b/tools/core/src/main/java/org/teavm/tooling/daemon/RemoteBuildRequest.java index 20edaa785..464cb920f 100644 --- a/tools/core/src/main/java/org/teavm/tooling/daemon/RemoteBuildRequest.java +++ b/tools/core/src/main/java/org/teavm/tooling/daemon/RemoteBuildRequest.java @@ -51,4 +51,5 @@ public class RemoteBuildRequest implements Serializable { public boolean longjmpSupported; public boolean heapDump; public boolean shortFileNames; + public boolean assertionsRemoved; } diff --git a/tools/maven/plugin/src/main/java/org/teavm/maven/TeaVMCompileMojo.java b/tools/maven/plugin/src/main/java/org/teavm/maven/TeaVMCompileMojo.java index cb3067403..d57b1a0bb 100644 --- a/tools/maven/plugin/src/main/java/org/teavm/maven/TeaVMCompileMojo.java +++ b/tools/maven/plugin/src/main/java/org/teavm/maven/TeaVMCompileMojo.java @@ -161,6 +161,9 @@ public class TeaVMCompileMojo extends AbstractMojo { @Parameter(property = "teavm.shortFileNames", defaultValue = "false") private boolean shortFileNames; + @Parameter(property = "teavm.assertionsRemoved", defaultValue = "false") + private boolean assertionsRemoved; + private void setupBuilder(BuildStrategy builder) throws MojoExecutionException { builder.setLog(new MavenTeaVMToolLog(getLog())); try { @@ -186,6 +189,7 @@ public class TeaVMCompileMojo extends AbstractMojo { builder.setMinHeapSize(minHeapSize * 1024 * 1024); builder.setMaxHeapSize(maxHeapSize * 1024 * 1024); builder.setShortFileNames(shortFileNames); + builder.setAssertionsRemoved(assertionsRemoved); } catch (RuntimeException e) { throw new MojoExecutionException("Unexpected error occurred", e); }