Class Reflector

java.lang.Object
com.vaadin.flow.plugin.maven.Reflector

public final class Reflector extends Object
Helper class to deal with classloading of Flow plugin mojos.
  • Field Details

  • Constructor Details

    • Reflector

      public Reflector(URLClassLoader isolatedClassLoader)
      Creates a new reflector instance for the given classloader.
      Parameters:
      isolatedClassLoader - class loader to be used to create mojo instances.
  • Method Details

    • getIsolatedClassLoader

      public URLClassLoader getIsolatedClassLoader()
      Gets the isolated class loader.
      Returns:
      the isolated class loader.
    • loadClass

      public Class<?> loadClass(String className) throws ClassNotFoundException
      Loads the class with the given name from the isolated classloader.
      Parameters:
      className - the name of the class to load.
      Returns:
      the class object.
      Throws:
      ClassNotFoundException - if the class was not found.
    • getResource

      public URL getResource(String name)
      Get a resource from the classpath of the isolated class loader.
      Parameters:
      name - class literal
      Returns:
      the resource
    • createMojo

      public org.apache.maven.plugin.Mojo createMojo(BuildDevBundleMojo sourceMojo) throws Exception
      Creates a copy of the given Flow mojo, loading classes the isolated classloader.

      Loads the given mojo class from the isolated class loader and then creates a new instance for it and fills all field copying values from the original mojo. The input mojo must have a public no-args constructor. Mojo fields must reference types that can be safely loaded be the isolated class loader, such as JDK or Maven core API. It also creates and injects a ClassFinder, based on the isolated class loader.
      Parameters:
      sourceMojo - The mojo for which to create the instance from the isolated class loader.
      Returns:
      an instance of the mojo loaded from the isolated class loader.
      Throws:
      Exception - if the mojo instance cannot be created.
    • of

      public static Reflector of(org.apache.maven.project.MavenProject project, org.apache.maven.plugin.MojoExecution mojoExecution)
      Gets a new Reflector instance for the current Mojo execution.

      An isolated class loader is created based on project and plugin dependencies, with the first ones having precedence over the seconds. The maven.api class realm is used as parent classloader, allowing usage of Maven core classes in the mojo.
      Parameters:
      project - the maven project.
      mojoExecution - the current mojo execution.
      Returns:
      a Reflector instance for the current maven execution.