From 5de1b3877dbe233c67b0fa3b17232df97625fced Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Mon, 28 Apr 2014 17:33:56 +0400 Subject: [PATCH] Adds configuring method and class aliases in teavm-maven-plugin --- .../src/main/java/org/teavm/vm/TeaVM.java | 28 ++++++-- .../org/teavm/maven/BuildJavascriptMojo.java | 41 +++++++++-- .../main/java/org/teavm/maven/ClassAlias.java | 41 +++++++++++ .../java/org/teavm/maven/MethodAlias.java | 68 +++++++++++++++++++ .../org/teavm/maven/MethodAliasArgument.java | 24 +++++++ 5 files changed, 194 insertions(+), 8 deletions(-) create mode 100644 teavm-maven-plugin/src/main/java/org/teavm/maven/ClassAlias.java create mode 100644 teavm-maven-plugin/src/main/java/org/teavm/maven/MethodAlias.java create mode 100644 teavm-maven-plugin/src/main/java/org/teavm/maven/MethodAliasArgument.java diff --git a/teavm-core/src/main/java/org/teavm/vm/TeaVM.java b/teavm-core/src/main/java/org/teavm/vm/TeaVM.java index 23b37d628..66cc8ee43 100644 --- a/teavm-core/src/main/java/org/teavm/vm/TeaVM.java +++ b/teavm-core/src/main/java/org/teavm/vm/TeaVM.java @@ -167,17 +167,37 @@ public class TeaVM implements TeaVMHost { * @return an entry point that you can additionally adjust. */ public TeaVMEntryPoint entryPoint(String name, MethodReference ref) { - if (entryPoints.containsKey(name)) { - throw new IllegalArgumentException("Entry point with public name `" + name + "' already defined " + - "for method " + ref); + if (name != null) { + if (entryPoints.containsKey(name)) { + throw new IllegalArgumentException("Entry point with public name `" + name + "' already defined " + + "for method " + ref); + } } TeaVMEntryPoint entryPoint = new TeaVMEntryPoint(name, ref, dependencyChecker.linkMethod(ref, DependencyStack.ROOT)); dependencyChecker.initClass(ref.getClassName(), DependencyStack.ROOT); - entryPoints.put(name, entryPoint); + if (name != null) { + entryPoints.put(name, entryPoint); + } return entryPoint; } + /** + *

Adds an entry point. TeaVM guarantees, that all methods that are required by the entry point + * will be available at run-time in browser. Also you need to specify for each parameter of entry point + * which actual types will be passed here by calling {@link TeaVMEntryPoint#withValue(int, String)}. + * It is highly recommended to read explanation on {@link TeaVMEntryPoint} class documentation.

+ * + *

You should call this method after installing all plugins and interceptors, but before + * doing the actual build.

+ * + * @param ref a full reference to the method which is an entry point. + * @return an entry point that you can additionally adjust. + */ + public TeaVMEntryPoint entryPoint(MethodReference ref) { + return entryPoint(null, ref); + } + public TeaVMEntryPoint linkMethod(MethodReference ref) { TeaVMEntryPoint entryPoint = new TeaVMEntryPoint("", ref, dependencyChecker.linkMethod(ref, DependencyStack.ROOT)); diff --git a/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptMojo.java b/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptMojo.java index c7de92dd9..c9a2267a9 100644 --- a/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptMojo.java +++ b/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptMojo.java @@ -39,10 +39,7 @@ import org.teavm.model.MethodDescriptor; import org.teavm.model.MethodReference; import org.teavm.model.ValueType; import org.teavm.parsing.ClasspathClassHolderSource; -import org.teavm.vm.BuildTarget; -import org.teavm.vm.DirectoryBuildTarget; -import org.teavm.vm.TeaVM; -import org.teavm.vm.TeaVMBuilder; +import org.teavm.vm.*; import org.teavm.vm.spi.AbstractRendererListener; /** @@ -91,6 +88,12 @@ public class BuildJavascriptMojo extends AbstractMojo { @Parameter private String[] transformers; + @Parameter + private ClassAlias[] classAliases; + + @Parameter + private MethodAlias[] methodAliases; + public void setProject(MavenProject project) { this.project = project; } @@ -139,6 +142,14 @@ public class BuildJavascriptMojo extends AbstractMojo { this.properties = properties; } + public void setClassAliases(ClassAlias[] classAliases) { + this.classAliases = classAliases; + } + + public void setMethodAliases(MethodAlias[] methodAliases) { + this.methodAliases = methodAliases; + } + @Override public void execute() throws MojoExecutionException { Log log = getLog(); @@ -172,6 +183,28 @@ public class BuildJavascriptMojo extends AbstractMojo { vm.entryPoint("main", new MethodReference(mainClass, mainMethodDesc)) .withValue(1, "java.lang.String"); } + if (classAliases != null) { + for (ClassAlias alias : classAliases) { + vm.exportType(alias.getAlias(), alias.getClassName()); + } + } + if (methodAliases != null) { + for (MethodAlias methodAlias : methodAliases) { + MethodReference ref = new MethodReference(methodAlias.getClassName(), methodAlias.getMethodName(), + MethodDescriptor.parseSignature(methodAlias.getDescriptor())); + TeaVMEntryPoint entryPoint = vm.entryPoint(methodAlias.getAlias(), ref); + if (methodAlias.getTypes() != null) { + for (int i = 0; i < methodAlias.getTypes().length; ++i) { + String types = methodAlias.getTypes()[i]; + if (types != null) { + for (String type : types.split(" ")) { + entryPoint.withValue(i, type); + } + } + } + } + } + } targetDirectory.mkdirs(); try (FileWriter writer = new FileWriter(new File(targetDirectory, targetFileName))) { if (runtime == RuntimeCopyOperation.MERGED) { diff --git a/teavm-maven-plugin/src/main/java/org/teavm/maven/ClassAlias.java b/teavm-maven-plugin/src/main/java/org/teavm/maven/ClassAlias.java new file mode 100644 index 000000000..29aefd6b0 --- /dev/null +++ b/teavm-maven-plugin/src/main/java/org/teavm/maven/ClassAlias.java @@ -0,0 +1,41 @@ +/* + * Copyright 2014 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.maven; + +/** + * + * @author Alexey Andreev + */ +public class ClassAlias { + private String className; + private String alias; + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public String getAlias() { + return alias; + } + + public void setAlias(String alias) { + this.alias = alias; + } +} diff --git a/teavm-maven-plugin/src/main/java/org/teavm/maven/MethodAlias.java b/teavm-maven-plugin/src/main/java/org/teavm/maven/MethodAlias.java new file mode 100644 index 000000000..1e477d35c --- /dev/null +++ b/teavm-maven-plugin/src/main/java/org/teavm/maven/MethodAlias.java @@ -0,0 +1,68 @@ +/* + * Copyright 2014 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.maven; + +/** + * + * @author Alexey Andreev + */ +public class MethodAlias { + private String alias; + private String className; + private String methodName; + private String descriptor; + private String[] types; + + public String getAlias() { + return alias; + } + + public void setAlias(String alias) { + this.alias = alias; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public String getMethodName() { + return methodName; + } + + public void setMethodName(String methodName) { + this.methodName = methodName; + } + + public String getDescriptor() { + return descriptor; + } + + public void setDescriptor(String descriptor) { + this.descriptor = descriptor; + } + + public String[] getTypes() { + return types; + } + + public void setTypes(String[] types) { + this.types = types; + } +} diff --git a/teavm-maven-plugin/src/main/java/org/teavm/maven/MethodAliasArgument.java b/teavm-maven-plugin/src/main/java/org/teavm/maven/MethodAliasArgument.java new file mode 100644 index 000000000..9f8879921 --- /dev/null +++ b/teavm-maven-plugin/src/main/java/org/teavm/maven/MethodAliasArgument.java @@ -0,0 +1,24 @@ +/* + * Copyright 2014 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.maven; + +/** + * + * @author Alexey Andreev + */ +public class MethodAliasArgument { + +}