Commit 8c1a3a4b authored by Lukáš Marek's avatar Lukáš Marek

Added jar support - WIP

parent 10ae3006
package ch.usi.dag.disl;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class CompilerStub {
// thx: http://www.exampledepot.com/egs/java.io/File2ByteArray.html
public byte [] readFileAsBytes(String fileName) throws IOException {
// Create input stream
File file = new File(fileName);
InputStream is = new FileInputStream(file);
byte[] bytes = null;
try {
// Get the size of the file
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
throw new IOException(
"File " + file.getName() + " is too large");
}
// Create the byte array to hold the data
bytes = new byte[(int) length];
// Read in the bytes
int offset = 0;
while (offset < bytes.length) {
int numRead = is.read(bytes, offset, bytes.length - offset);
// Input is expected but -1 (end of file) was returned
if(numRead < 0) {
throw new IOException("Could not completely read file "
+ file.getName());
}
offset += numRead;
}
}
finally {
// Close the input stream...
is.close();
}
// ... and return bytes
return bytes;
}
byte [] compile(String fileName) throws IOException {
// read class from file instead of compiling
return readFileAsBytes(fileName);
}
}
package ch.usi.dag.disl;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
......@@ -12,6 +13,7 @@ import org.objectweb.asm.Type;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
import ch.usi.dag.disl.dislclass.loader.ClassByteLoader;
import ch.usi.dag.disl.dislclass.localvar.SyntheticLocalVar;
import ch.usi.dag.disl.dislclass.parser.ClassParser;
import ch.usi.dag.disl.dislclass.processor.Proc;
......@@ -33,9 +35,6 @@ import ch.usi.dag.jborat.agent.Instrumentation;
// TODO javadoc comment all
public class DiSL implements Instrumentation {
final String PROP_DISL_CLASSES = "disl.classes";
final String PROP_CLASSES_DELIM = ",";
final String PROP_DYNAMIC_BYPASS = "disl.dynbypass";
final String PROP_DYNAMIC_BYPASS_TRUE = "yes";
......@@ -51,14 +50,6 @@ public class DiSL implements Instrumentation {
// report every exception within our code - don't let anyone mask it
try {
// TODO replace with jar support
String classesToCompile = System.getProperty(PROP_DISL_CLASSES);
if (classesToCompile == null) {
throw new InitException("Property " + PROP_DISL_CLASSES
+ " is not defined");
}
String useDynBypassStr = System.getProperty(PROP_DYNAMIC_BYPASS);
boolean useDynamicBypass = false;
......@@ -67,25 +58,22 @@ public class DiSL implements Instrumentation {
PROP_DYNAMIC_BYPASS_TRUE);
}
List<byte[]> compiledClasses = new LinkedList<byte[]>();
// TODO replace with jar support
CompilerStub compiler = new CompilerStub();
// *** compile DiSL classes ***
for (String file : classesToCompile.split(PROP_CLASSES_DELIM)) {
compiledClasses.add(compiler.compile(file));
List<InputStream> dislClasses = ClassByteLoader.loadDiSLClasses();
if(dislClasses == null) {
throw new InitException("Cannot load DiSL classes. Please set" +
" the property " + ClassByteLoader.DISL_CLASSES
+ " or supply " + ClassByteLoader.INSTR_JAR);
}
// *** parse compiled classes ***
// *** parse disl classes ***
// - create snippets
// - create static analysis methods
ClassParser parser = new ClassParser();
for (byte[] classAsBytes : compiledClasses) {
parser.parse(classAsBytes);
for (InputStream classIS : dislClasses) {
parser.parse(classIS);
}
// initialize processors
......@@ -143,8 +131,6 @@ public class DiSL implements Instrumentation {
return;
}
// TODO create finite-state machine if possible
// *** match snippet scope ***
List<Snippet> matchedSnippets = new LinkedList<Snippet>();
......@@ -216,7 +202,6 @@ public class DiSL implements Instrumentation {
// *** viewing ***
// TODO ! weaver should have two parts, weaving and rewriting
Weaver.instrument(classNode, methodNode, snippetMarkings,
new LinkedList<SyntheticLocalVar>(usedSLVs), staticInfo,
piResolver);
......
package ch.usi.dag.disl.dislclass.loader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import ch.usi.dag.disl.exception.InitException;
public abstract class ClassByteLoader {
private static final String MANIFEST = "META-INF/MANIFEST.MF";
public static final String INSTR_JAR = "dislinstr.jar";
public static final String DISL_CLASSES = "disl.classes";
public static final String DISL_CLASSES_DELIM = ":";
// TODO ! jar support - add jar dependency
// TODO ! jar support - create ant task for test compilation
// TODO ! jar support - create processor manifest
// TODO ! jar support - test
public static List<InputStream> loadDiSLClasses()
throws InitException {
try {
List<InputStream> result = loadClassesFromProperty();
if(result == null) {
result = loadClassesFromManifest();
}
return result;
} catch (IOException e) {
throw new InitException(e);
}
}
private static List<InputStream> loadClassesFromProperty()
throws IOException {
String classesList = System.getProperty(DISL_CLASSES);
if ( (classesList != null) && (! classesList.isEmpty()) ) {
List<InputStream> dislClasses = new LinkedList<InputStream>();
for (String fileName : classesList.split(DISL_CLASSES_DELIM)) {
File file = new File(fileName);
dislClasses.add(new FileInputStream(file));
}
return dislClasses;
}
return null;
}
private static List<InputStream> loadClassesFromManifest()
throws IOException {
ClassLoader cl = ClassByteLoader.class.getClassLoader();
String classesList = getClassesListFromManifest(cl);
if ( (classesList != null) && (! classesList.isEmpty()) ) {
List<InputStream> dislClasses = new LinkedList<InputStream>();
for (String fileName : classesList.split(DISL_CLASSES_DELIM)) {
dislClasses.add(cl.getResourceAsStream(fileName));
}
return dislClasses;
}
return null;
}
private static String getClassesListFromManifest(ClassLoader classLoader)
throws IOException {
// get all manifests...
Enumeration<URL> resources = classLoader.getResources(MANIFEST);
// and find ours...
while (resources.hasMoreElements()) {
Manifest manifest =
new Manifest(resources.nextElement().openStream());
Attributes attrs = manifest.getMainAttributes();
// contains disl classes
if(attrs != null) {
String dislClasses = attrs.getValue(DISL_CLASSES);
if(dislClasses != null) {
return dislClasses;
}
}
}
return null;
}
}
package ch.usi.dag.disl.dislclass.parser;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
......@@ -24,12 +26,12 @@ public class ClassParser {
SnippetParser snippetParser = new SnippetParser();
ProcessorParser processorParser = new ProcessorParser();
public void parse(byte[] classAsBytes) throws ParserException,
public void parse(InputStream is) throws ParserException,
SnippetParserException, ReflectionException, ScopeParserException,
StaticAnalysisException, ProcessorParserException {
StaticAnalysisException, ProcessorParserException, IOException {
// prepare class node
ClassReader cr = new ClassReader(classAsBytes);
ClassReader cr = new ClassReader(is);
ClassNode classNode = new ClassNode();
cr.accept(classNode, 0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment