Package org.teavm.vm

Class TeaVM

  • All Implemented Interfaces:
    ServiceRepository, TeaVMHost

    public class TeaVM
    extends java.lang.Object
    implements TeaVMHost, ServiceRepository

    TeaVM itself. This class builds a JavaScript VM that runs a certain code. Here you can specify entry points into your code (such like main method). TeaVM guarantees that all required classes and methods will be provided by built VM.

    Here is a typical code snippet:

    
    ClassLoader classLoader = ...; // obtain ClassLoader somewhere
    ClassHolderSource classSource = new ClasspathClassHolderSource(classLoader);
    TeaVM vm = new TeaVMBuilder()
            .setClassLoader(classLoader)
            .setClassSource(classSource)
            .build();
    vm.setMinifying(false); // optionally disable obfuscation
    vm.installPlugins();    // install all default plugins
                            // that are found in a classpath
    vm.entryPoint("main", new MethodReference(
            "fully.qualified.ClassName",  "main",
             ValueType.array(ValueType.object("java.lang.String")),
             ValueType.VOID));
    StringBuilder sb = new StringBuilder();
    vm.build(sb, null);
    vm.checkForMissingItems();
    
    • Method Detail

      • addVirtualMethods

        public void addVirtualMethods​(java.util.function.Predicate<MethodReference> virtualMethods)
      • setProperties

        public void setProperties​(java.util.Properties properties)
        Specifies configuration properties for TeaVM and its plugins. You should call this method before installing any plugins or interceptors.
        Parameters:
        properties - configuration properties to set. These properties will be copied into this VM instance, so VM won't see any further changes in this object.
      • getProperties

        public java.util.Properties getProperties()
        Description copied from interface: TeaVMHost
        Gets configuration properties. These properties are usually specified by setProperties(Properties).
        Specified by:
        getProperties in interface TeaVMHost
        Returns:
        a copy of all of the VM properties. Any further changes to returned objects will not be visible to VM.
      • setProgramCache

        public void setProgramCache​(ProgramCache programCache)
      • setCacheStatus

        public void setCacheStatus​(CacheStatus cacheStatus)
      • wasCancelled

        public boolean wasCancelled()
      • entryPoint

        public void entryPoint​(java.lang.String className,
                               java.lang.String name)
      • entryPoint

        public void entryPoint​(java.lang.String className)
      • preserveType

        public void preserveType​(java.lang.String className)
      • getClasses

        public java.util.Collection<java.lang.String> getClasses()
      • setLastKnownClasses

        public void setLastKnownClasses​(int lastKnownClasses)
      • build

        public void build​(BuildTarget buildTarget,
                          java.lang.String outputName)

        Does actual build. Call this method after TeaVM is fully configured and all entry points are specified. This method may fail if there are items (classes, methods and fields) that are required by entry points, but weren't found in classpath. In this case no actual generation happens and no exceptions thrown, but you can further call getProblemProvider() to learn the build state.

        Parameters:
        buildTarget - where to generate additional resources. Can be null, but if there are plugins or interceptors that generate additional resources, the build process will fail.
        outputName - name of output file within buildTarget. Should not be null.
      • build

        public void build​(java.io.File dir,
                          java.lang.String fileName)
      • installPlugins

        public void installPlugins()

        Finds and install all plugins in the current class path. The standard ServiceLoader approach is used to find plugins. So this method scans all META-INF/services/org.teavm.vm.spi.TeaVMPlugin resources and obtains all implementation classes that are enumerated there.

      • registerService

        public <T> void registerService​(java.lang.Class<T> type,
                                        T instance)
        Specified by:
        registerService in interface TeaVMHost