Package org.teavm.vm

Class TeaVM

java.lang.Object
org.teavm.vm.TeaVM
All Implemented Interfaces:
ServiceRepository, TeaVMHost

public class TeaVM extends 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 Details

    • addVirtualMethods

      public void addVirtualMethods(Predicate<MethodReference> virtualMethods)
    • add

      public void add(DependencyListener listener)
      Specified by:
      add in interface TeaVMHost
    • add

      public void add(ClassHolderTransformer transformer)
      Specified by:
      add in interface TeaVMHost
    • add

      public void add(MethodReference methodRef, BootstrapMethodSubstitutor substitutor)
      Specified by:
      add in interface TeaVMHost
    • add

      public void add(MethodReference methodRef, DependencyPlugin dependencyPlugin)
      Specified by:
      add in interface TeaVMHost
    • getClassLoader

      public ClassLoader getClassLoader()
      Description copied from interface: TeaVMHost
      Gets class loaded that is used by TeaVM. This class loader is usually specified by TeaVMBuilder.setClassLoader(ClassLoader)
      Specified by:
      getClassLoader in interface TeaVMHost
      Returns:
      class loader that can be used by plugins.
    • setProperties

      public void setProperties(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 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.
    • getProgramCache

      public ProgramCache getProgramCache()
    • setProgramCache

      public void setProgramCache(ProgramCache programCache)
    • setCacheStatus

      public void setCacheStatus(CacheStatus cacheStatus)
    • getOptimizationLevel

      public TeaVMOptimizationLevel getOptimizationLevel()
    • setOptimizationLevel

      public void setOptimizationLevel(TeaVMOptimizationLevel optimizationLevel)
    • getProgressListener

      public TeaVMProgressListener getProgressListener()
    • setProgressListener

      public void setProgressListener(TeaVMProgressListener progressListener)
    • wasCancelled

      public boolean wasCancelled()
    • getProblemProvider

      public ProblemProvider getProblemProvider()
    • getPlatformTags

      public String[] getPlatformTags()
      Specified by:
      getPlatformTags in interface TeaVMHost
    • entryPoint

      public void entryPoint(String className, String name)
    • entryPoint

      public void entryPoint(String className)
    • preserveType

      public void preserveType(String className)
    • getDependencyClassSource

      public ClassReaderSource getDependencyClassSource()
    • getClasses

      public Collection<String> getClasses()
    • getMethods

      public Collection<MethodReference> getMethods()
    • getDependencyInfo

      public DependencyInfo getDependencyInfo()
    • getWrittenClasses

      public ListableClassReaderSource getWrittenClasses()
    • setLastKnownClasses

      public void setLastKnownClasses(int lastKnownClasses)
    • build

      public void build(BuildTarget buildTarget, 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.
    • link

    • build

      public void build(File dir, 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.

    • getService

      public <T> T getService(Class<T> type)
      Specified by:
      getService in interface ServiceRepository
    • registerService

      public <T> void registerService(Class<T> type, T instance)
      Specified by:
      registerService in interface TeaVMHost
    • getExtension

      public <T extends TeaVMHostExtension> T getExtension(Class<T> extensionType)
      Specified by:
      getExtension in interface TeaVMHost