forked from eclipse-vertx/vert.x
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/jannehietamaki/vert.x
- Loading branch information
Showing
3 changed files
with
166 additions
and
6 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/main/java/org/vertx/java/deploy/impl/java/CompilingClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2011-2012 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.vertx.java.deploy.impl.java; | ||
|
||
import java.io.File; | ||
import java.util.Collections; | ||
|
||
import javax.tools.Diagnostic; | ||
import javax.tools.DiagnosticCollector; | ||
import javax.tools.JavaCompiler; | ||
import javax.tools.JavaFileObject; | ||
import javax.tools.JavaFileObject.Kind; | ||
import javax.tools.StandardJavaFileManager; | ||
import javax.tools.StandardLocation; | ||
import javax.tools.ToolProvider; | ||
|
||
/** | ||
* | ||
* Classloader for dynamic .java source file compilation and loading. | ||
* | ||
* @author Janne Hietamäki | ||
*/ | ||
public class CompilingClassLoader extends ClassLoader { | ||
private final File sourceFile; | ||
private final MemoryFileManager fileManager; | ||
|
||
public CompilingClassLoader(ClassLoader loader, String sourceName) { | ||
super(loader); | ||
this.sourceFile = new File(sourceName).getAbsoluteFile(); | ||
if(!this.sourceFile.canRead()) { | ||
throw new RuntimeException("File not found: " + sourceName); | ||
} | ||
try { | ||
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); | ||
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); | ||
StandardJavaFileManager standardFileManager = javaCompiler.getStandardFileManager(null, null, null); | ||
|
||
standardFileManager.setLocation(StandardLocation.SOURCE_PATH, Collections.singleton(sourceFile.getParentFile())); | ||
|
||
fileManager = new MemoryFileManager(standardFileManager); | ||
JavaFileObject javaFile = standardFileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, resolveMainClassName(), Kind.SOURCE); | ||
JavaCompiler.CompilationTask task = javaCompiler.getTask(null, fileManager, diagnostics, null, null, Collections.singleton(javaFile)); | ||
boolean valid = task.call(); | ||
|
||
for (Diagnostic<?> d : diagnostics.getDiagnostics()) { | ||
System.out.println(d); | ||
} | ||
if (!valid) { | ||
throw new RuntimeException("Compilation failed!"); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public String resolveMainClassName() { | ||
String fileName = sourceFile.getName(); | ||
return fileName.substring(0, fileName.length() - Kind.SOURCE.extension.length()); | ||
} | ||
|
||
@Override | ||
protected Class<?> findClass(String name) throws ClassNotFoundException { | ||
byte[] bytecode = fileManager.getCompiledClass(name); | ||
if (bytecode == null) { | ||
throw new ClassNotFoundException(name); | ||
} | ||
return defineClass(name, bytecode, 0, bytecode.length); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/org/vertx/java/deploy/impl/java/MemoryFileManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2011-2012 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.vertx.java.deploy.impl.java; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.tools.FileObject; | ||
import javax.tools.ForwardingJavaFileManager; | ||
import javax.tools.JavaFileManager; | ||
import javax.tools.JavaFileObject; | ||
import javax.tools.SimpleJavaFileObject; | ||
|
||
/** | ||
* Java in-memory file manager used by {@link CompilingClassLoader} to handle | ||
* compiled classes | ||
* | ||
* @author Janne Hietamäki | ||
*/ | ||
public class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> { | ||
private final Map<String, ByteArrayOutputStream> compiledClasses = new HashMap<String, ByteArrayOutputStream>(); | ||
|
||
public MemoryFileManager(JavaFileManager fileManager) { | ||
super(fileManager); | ||
} | ||
|
||
@Override | ||
public JavaFileObject getJavaFileForOutput(Location location, final String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException { | ||
try { | ||
return new SimpleJavaFileObject(new URI(""), kind) { | ||
public OutputStream openOutputStream() throws IOException { | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
compiledClasses.put(className, outputStream); | ||
return outputStream; | ||
} | ||
}; | ||
} catch (URISyntaxException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public byte[] getCompiledClass(String name) { | ||
ByteArrayOutputStream bytes = compiledClasses.get(name); | ||
if (bytes == null) { | ||
return null; | ||
} | ||
return bytes.toByteArray(); | ||
} | ||
} |