Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[DROOLS-5780] descope ecj (apache#3237)
* [DROOLS-5780] descope ecj

* wip

* wip

* wip

* wip

* wip

* wip

* wip
  • Loading branch information
mariofusco committed Nov 20, 2020
1 parent c20943d commit 850a656
Show file tree
Hide file tree
Showing 26 changed files with 372 additions and 254 deletions.
Expand Up @@ -22,11 +22,11 @@
import java.util.List;

import org.drools.compiler.commons.jci.compilers.CompilationResult;
import org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler;
import org.drools.compiler.commons.jci.compilers.EclipseJavaCompilerSettings;
import org.drools.compiler.commons.jci.compilers.JavaCompiler;
import org.drools.compiler.commons.jci.compilers.NativeJavaCompiler;
import org.drools.compiler.compiler.io.File;
import org.drools.compiler.compiler.io.Folder;
import org.drools.compiler.compiler.io.FileSystemItem;
import org.drools.compiler.compiler.io.Folder;
import org.drools.compiler.compiler.io.memory.MemoryFile;
import org.drools.compiler.compiler.io.memory.MemoryFileSystem;
import org.drools.compiler.kie.builder.impl.KieFileSystemImpl;
Expand All @@ -50,7 +50,9 @@
import org.kie.api.runtime.StatelessKieSession;
import org.kie.api.runtime.conf.ClockTypeOption;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class AbstractKnowledgeTest {

Expand Down Expand Up @@ -298,10 +300,7 @@ public List<String> compile(KieModuleModel kproj,

copyFolder( srcMfs, srcFolder, trgMfs, trgFolder, kproj );

EclipseJavaCompilerSettings settings = new EclipseJavaCompilerSettings();
settings.setSourceVersion( "1.5" );
settings.setTargetVersion( "1.5" );
EclipseJavaCompiler compiler = new EclipseJavaCompiler( settings, "" );
JavaCompiler compiler = new NativeJavaCompiler();
CompilationResult res = compiler.compile( classes.toArray( new String[classes.size()] ), trgMfs, trgMfs );

if ( res.getErrors().length > 0 ) {
Expand Down
39 changes: 1 addition & 38 deletions drools-compiler/pom.xml
Expand Up @@ -73,10 +73,6 @@
<artifactId>gunit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>ecj</artifactId>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
Expand Down Expand Up @@ -177,36 +173,6 @@
</pluginManagement>

<plugins>
<!-- shade ecj to allow the gwt compiler to use its own version -->
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createSourcesJar>true</createSourcesJar>
<artifactSet>
<includes>
<include>org.eclipse.jdt:ecj</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>org.eclipse.jdt</pattern>
<shadedPattern>org.drools.compiler.shade.org.eclipse.jdt</shadedPattern>
</relocation>
</relocations>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
Expand All @@ -217,13 +183,10 @@
com.sun.codemodel;resolution:=optional,
javax.enterprise.*;resolution:=optional,
org.antlr.*;resolution:=optional,
<!-- Hack/workaround: relying on ECJ package version [0,1) is needed as ECJ does not export specific package
versions (and thus the version defaults to 0.0.0). This is just a workaround as proper fix needs be done
in ECJ directly to export the versions correctly in the MANIFEST.MF. -->
org.eclipse.jdt.*;version="[0,1)";resolution:=optional,
org.osgi.*;resolution:=optional,
org.drools.core.base.accumulators;resolution:=optional,
org.drools.core.rule.builder.dialect.asm;resolution:=optional,
org.drools.ecj;resolution:=optional,
org.drools.mvel.asm;resolution:=optional,
org.drools.cdi;resolution:=optional,
org.drools.template.parser;resolution:=optional,
Expand Down
Expand Up @@ -30,6 +30,8 @@ public abstract class AbstractJavaCompiler implements JavaCompiler {

protected CompilationProblemHandler problemHandler;

private JavaCompilerSettings javaCompilerSettings;

public void setCompilationProblemHandler( final CompilationProblemHandler pHandler ) {
problemHandler = pHandler;
}
Expand All @@ -46,7 +48,10 @@ public CompilationResult compile( final String[] pClazzNames, final ResourceRead
}

public CompilationResult compile( final String[] pClazzNames, final ResourceReader pReader, final ResourceStore pStore, final ClassLoader pClassLoader ) {
return compile(pClazzNames, pReader, pStore, pClassLoader, createDefaultSettings());
return compile(pClazzNames, pReader, pStore, pClassLoader, javaCompilerSettings != null ? javaCompilerSettings : createDefaultSettings());
}

public void setJavaCompilerSettings( JavaCompilerSettings javaCompilerSettings ) {
this.javaCompilerSettings = javaCompilerSettings;
}
}
Expand Up @@ -46,10 +46,14 @@ public interface JavaCompiler {
*/
void setCompilationProblemHandler( final CompilationProblemHandler pHandler );

default void setSourceFolder( String sourceFolder ) { }

/**
* factory method to create the underlying default settings
*/
JavaCompilerSettings createDefaultSettings();

void setJavaCompilerSettings( JavaCompilerSettings javaCompilerSettings );

/**
* uses the default compiler settings and the current classloader
Expand Down
Expand Up @@ -19,9 +19,9 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.drools.compiler.compiler.JavaConfiguration;
import org.drools.core.util.ClassUtils;

/**
* Creates JavaCompilers
Expand All @@ -32,30 +32,38 @@ public enum JavaCompilerFactory {

private final Map classCache = new HashMap();

/**
* Tries to guess the class name by convention. So for compilers
* following the naming convention
*
* org.apache.commons.jci.compilers.SomeJavaCompiler
*
* you can use the short-hands "some"/"Some"/"SOME". Otherwise
* you have to provide the full class name. The compiler is
* getting instanciated via (cached) reflection.
*
* @param pHint
* @return JavaCompiler or null
*/
public JavaCompiler createCompiler(final String pHint) {

final String className;
if (pHint.indexOf('.') < 0) {
className = "org.drools.compiler.commons.jci.compilers." + ClassUtils.toJavaCasing(pHint) + "JavaCompiler";
} else {
className = pHint;
public JavaCompiler loadCompiler( JavaConfiguration configuration) {
return loadCompiler( configuration.getCompiler(), configuration.getJavaLanguageLevel() );
}

public JavaCompiler loadCompiler( JavaConfiguration.CompilerType compilerType, String lngLevel ) {
return loadCompiler( compilerType, lngLevel, "" );
}

public JavaCompiler loadCompiler( JavaConfiguration.CompilerType compilerType, String lngLevel, String sourceFolder ) {
JavaCompiler compiler = createCompiler( compilerType ).orElseThrow( () -> new RuntimeException("Instance of " + compilerType + " compiler cannot be created!") );
compiler.setJavaCompilerSettings( createSettings( compiler, compilerType, lngLevel ) );
compiler.setSourceFolder(sourceFolder);
return compiler;
}

private JavaCompilerSettings createSettings( JavaCompiler compiler, JavaConfiguration.CompilerType compilerType, String lngLevel ) {
JavaCompilerSettings settings = compiler.createDefaultSettings();
settings.setTargetVersion( lngLevel );
// FIXME: the native Java compiler doesn't work with JPMS
if (compilerType == JavaConfiguration.CompilerType.ECLIPSE || lngLevel.startsWith( "1." )) {
settings.setSourceVersion( lngLevel );
}

return settings;
}

private Optional<JavaCompiler> createCompiler( JavaConfiguration.CompilerType compilerType) {
return createCompiler(compilerType.getImplClassName());
}

private Optional<JavaCompiler> createCompiler(String className) {
Class clazz = (Class) classCache.get(className);

if (clazz == null) {
try {
clazz = Class.forName(className);
Expand All @@ -68,47 +76,11 @@ public JavaCompiler createCompiler(final String pHint) {
if (clazz == null) {
return null;
}

try {
return (JavaCompiler) clazz.newInstance();
return Optional.of( (JavaCompiler) clazz.getConstructor().newInstance() );
} catch (Throwable t) {
return null;
return Optional.empty();
}
}

public JavaCompiler loadCompiler( JavaConfiguration configuration) {
return loadCompiler( configuration.getCompiler(), configuration.getJavaLanguageLevel() );
}

public JavaCompiler loadCompiler( JavaConfiguration.CompilerType compilerType, String lngLevel ) {
JavaCompiler compiler;
switch ( compilerType ) {
case NATIVE : {
compiler = createCompiler( "native" );
if (compiler == null) {
throw new RuntimeException("Instance of native compiler cannot be created!");
} else {
updateSettings( compiler.createDefaultSettings(), lngLevel );
}
break;
}
case ECLIPSE :
default : {
compiler = createCompiler( "eclipse" );
if (compiler == null) {
throw new RuntimeException("Instance of eclipse compiler cannot be created!");
} else {
updateSettings( compiler.createDefaultSettings(), lngLevel );
}
break;
}
}
return compiler;
}

private JavaCompilerSettings updateSettings( JavaCompilerSettings settings, String lngLevel ) {
settings.setTargetVersion( lngLevel );
settings.setSourceVersion( lngLevel );
return settings;
}
}

0 comments on commit 850a656

Please sign in to comment.