All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.codetrans.lang.scala.ScalaLang Maven / Gradle / Ivy

There is a newer version: 4.1.0.Beta1
Show newest version
package io.vertx.codetrans.lang.scala;

import io.vertx.codetrans.CodeBuilder;
import io.vertx.codetrans.Lang;
import io.vertx.codetrans.Script;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Scala language
 *
 * @author Jochen Mader className, String methodName) {
    File folder = new File(root, className.stream().collect(Collectors.joining(File.separator)));
    if (methodName != null) {
      folder = new File(folder, methodName);
    }
    return new File(folder.getParent(), folder.getName() + ".scala");
  }

  @Override
  public Script loadScript(ClassLoader loader, String path, String method) throws Exception {
    loader = new URLClassLoader(new URL[]{ new File(new File("target"), "scala-classes").toURI().toURL() }, loader);
    String fqn = path.replace('/', '.') + '.' + method;
    Class clazz = loader.loadClass(fqn);
    Object instance = clazz.newInstance();
    Method m = clazz.getDeclaredMethod("apply");
    return new Script() {
      @Override
      public String getSource() {
        throw new UnsupportedOperationException();
      }
      @Override
      public void run(Map globals) throws Exception {
        try {
          m.invoke(instance);
        } catch (InvocationTargetException e) {
          rethrowAny(e.getCause());
        }
      }
    };
  }

  @Override
  public String getExtension() {
    return "scala";
  }

  @SuppressWarnings("unchecked")
  private static  void throwThis(Throwable exception) throws T {
    throw (T) exception;
  }

  private static void rethrowAny(Throwable exception) {
    ScalaLang.throwThis(exception);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy