Adds configuring method and class aliases in teavm-maven-plugin

This commit is contained in:
konsoletyper 2014-04-28 17:33:56 +04:00
parent abc6c83827
commit 5de1b3877d
5 changed files with 194 additions and 8 deletions

View File

@ -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;
}
/**
* <p>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.</p>
*
* <p>You should call this method after installing all plugins and interceptors, but before
* doing the actual build.</p>
*
* @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));

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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 {
}