Skip to content

Commit

Permalink
m0smithgh-24 Fix the classloader reloader
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew O. Smith committed Jan 7, 2015
1 parent 17bbc00 commit a999c20
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
20 changes: 15 additions & 5 deletions src/main/groovy/com/software_ninja/malabar/lang/JavaParser.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ public class JavaParser implements Parser {
List rtnval = new ArrayList();
MyDiagnosticListener listener = new MyDiagnosticListener(rtnval);
StandardJavaFileManager fileManager = compiler.getStandardFileManager(listener, null, null);

fileManager.setLocation(StandardLocation.CLASS_PATH, classloader.classPath.collect({ new File(it)}));
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(classesDir)));
File outputDir = new File(classesDir);
def localClassloader = new GroovyClassLoader( classloader );

if(! outputDir.exists()) {
outputDir.mkdirs();
}

localClassloader.addURL(outputDir.toURL());

fileManager.setLocation(StandardLocation.CLASS_PATH, classloader.classPath.collect({ new File(it)}));
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(outputDir));
def fmClassloader = fileManager.getClassLoader(StandardLocation.CLASS_PATH);

//String separator = System.getProperty("path.separator");
//String classpath = classloader.classPath.join(separator)
Expand All @@ -51,11 +59,13 @@ public class JavaParser implements Parser {

if(result) {
def m = output.toString() =~ /\[checking (.*)\]/
//log.fine output.toString();
def clazzes = m.collect({classloader.loadClass(it[1])});
log.info output.toString();
//log.info classloader.getParent().getClass().toString();
def clazzes = m.collect({ fmClassloader.loadClass(it[1])});
def clazz = null;
if(clazzes.size > 0 ) {
clazz = clazzes[0];
println "WHICH:" + fmClassloader.getResource("com/software_ninja/test/project/AppTest.class");
def publicClasses = clazzes.grep( { Modifier.isPublic(it.getModifiers())} );
if(publicClasses.size > 0) {
clazz = publicClasses[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import org.sonatype.aether.artifact.Artifact;

import org.sonatype.aether.graph.DependencyNode

import com.software_ninja.malabar.MalabarUtil
import com.software_ninja.malabar.MalabarUtil
import com.software_ninja.malabar.ResourceCache;
import com.software_ninja.malabar.SemanticReflector;
import com.software_ninja.malabar.lang.GroovyParser;
Expand Down Expand Up @@ -111,7 +111,7 @@ public class MavenProjectHandler {
def createCacheEntry(pomFile, mod, func) {
def projectInfo = func();
def parent = pomFile.getParent();
def relPaths =relative.collect({new File (parent, it).getAbsolutePath()});
def relPaths = relative.collect({new File (parent, it).getAbsolutePath()});
def bootClasspath = System.getProperty("sun.boot.class.path");
log.fine "RELPATHS" + relPaths;
def classpath = relPaths +
Expand All @@ -135,12 +135,18 @@ public class MavenProjectHandler {
resourceCache : resourceCache,
parsers : [ "groovy-strict" : new GroovyParser(staticClassloader),
groovy : new GroovyParser(classloader),
java : new JavaParser(classloader, projectInfo['runtime']['elements'][0])],
java : new JavaParser(classloader, projectInfo['test']['elements'][0])],
classLoader : classloader,
classLoaderStatic : staticClassloader];
return rtnval;
}

def clearCache() {
def name = this.getClass().getName();
def f = cache[name];
if( f != null ) f.clear();
}

def lookInCache(pom, func) {
def name = this.getClass().getName();
def pomFile = new File(MalabarUtil.expandFile(pom as String));
Expand All @@ -166,8 +172,14 @@ public class MavenProjectHandler {

def additionalClasspath(relativeJson ,absoluteJson) {

this.relative = relativeJson != null ? new groovy.json.JsonSlurper().parseText (relativeJson ) : [];
this.absolute = absoluteJson != null ? new groovy.json.JsonSlurper().parseText (absoluteJson ) : [];
def newRelative = relativeJson != null ? new groovy.json.JsonSlurper().parseText (relativeJson ) : [];
def newAbsolute = absoluteJson != null ? new groovy.json.JsonSlurper().parseText (absoluteJson ) : [];

if( relative != newRelative || absolute != newAbsolute) {
this.relative = newRelative;
this.absolute = newAbsolute;
clearCache();
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class TestJavaParser {
assertNotNull(rtnval['class']);
assertEquals("com.software_ninja.test.project.AppTest", rtnval['class'].getName());
}

@Test
public void testStringParser() throws Exception {
String code = "class HamsterTest {} ";
Expand All @@ -35,4 +36,6 @@ public class TestJavaParser {
assertEquals("HamsterTest", javaParser.parse(code)['class'].getName());
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,16 @@ class MavenProjectTester {
def result = mph.tags(defaultRepo, pom, className);
}

@Test
public void testFileUnitTest() throws Exception {
String simple = 'src/test/resources/projects/simple/';
String scriptIn = simple + '/src/test/java/com/software_ninja/test/project/AppTest.java';
String pm = simple + "pom.xml";
String repo = "~/.m2/repository";
new File(simple + "target").deleteDir();
def mph = new MavenProjectHandler(config);
def rtnval = mph.unitTest(repo, pm, scriptIn, null, "java");
assertEquals("This always fails", rtnval[0][1]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void testApp()
{
assertTrue( true );
}

@Test
public void failApp()
{
Expand All @@ -33,4 +33,7 @@ public void failApp()
public void testBoolean() throws Exception {
assertEquals(true, app.getTrue());
}


}

0 comments on commit a999c20

Please sign in to comment.