Commit 6941490c authored by Vít Kabele's avatar Vít Kabele

Added comments and some tests.

parent 31b2cc8b
Pipeline #3203 passed with stages
in 7 minutes and 43 seconds
/**
* Create the exclusion set based on provided instrumentation jars
*
* This file is part of disl project
*
* Author: Vit Kabele <vit@kabele.me>
* Created on the 26/10/2018
*/
......@@ -16,9 +19,16 @@ import java.util.jar.JarEntry;
import java.util.stream.Collectors;
/**
* Create the exclusion set based on provided instrumentation jars.
*/
public abstract class ExclusionSetFactory {
/**
* Prepare the exclusion set including hardcoded, instrumentation and user defined exclusions.
* @param instrumentationJars Provided jars.
* @return Exclusion set.
*/
public static Set <Scope> prepare (List<InstrumentationJar> instrumentationJars) {
try {
final Set <Scope> result = hardCodedExclusions ();
......@@ -38,6 +48,13 @@ public abstract class ExclusionSetFactory {
}
}
/**
* Extract the instrumentation from jar file.
*
* @param jar JarFile.
* @return Extracted exclusion scopes.
*/
private static List<Scope> instrumentationExclusions(InstrumentationJar jar){
return jar
......@@ -57,8 +74,19 @@ public abstract class ExclusionSetFactory {
}
private static final String __DISL_EXCLUSION_LIST__ = "disl.exclusionList";
/**
* Parameter name for exclusion list.
*/
private static final String DISL_EXCLUSION_LIST = "disl.exclusionList";
/**
* Load user defined exclusions from (server) manifest file.
*
* @return Exclusion set.
* @throws FileNotFoundException Referenced file not found.
*/
private static Set <Scope> userDefinedExclusions () throws FileNotFoundException {
final String commentPrefix = "#";
final Set <Scope> result = new HashSet <> ();
......@@ -67,7 +95,7 @@ public abstract class ExclusionSetFactory {
// If a user specified a custom exclusion list, load it line by line,
// each line representing a single exclusion scope.
//
final String fileName = System.getProperty (__DISL_EXCLUSION_LIST__, "");
final String fileName = System.getProperty (DISL_EXCLUSION_LIST, "");
if (!fileName.isEmpty ()) {
final Scanner scanner = new Scanner (new FileInputStream (fileName));
while (scanner.hasNextLine ()) {
......@@ -83,6 +111,12 @@ public abstract class ExclusionSetFactory {
return result;
}
/**
* Hardcoded exclusions.
*
* @return Hardcoded exclusions.
*/
private static Set<Scope> hardCodedExclusions () {
final String [] exclusions = new String [] {
//
......
/**
* This file is part of disl project
* Load the Jar from bytes and provide API necessary to obtain required objects,
* such as DiSL class snippets, Transformer classes and Exclusion sets.
*
* This file is part of disl project
*
* Author: Vit Kabele <vit@kabele.me>
* Created on the 26/10/2018
*/
......@@ -140,16 +141,13 @@ final class InstrumentationJar {
return transformers;
}
JarInputStream getJarInputStream(){
try {
return new JarInputStream (new ByteArrayInputStream (JarBytes));
} catch (Exception e){
// Exception cannot be raised here, since the JarBytes variable is checked in constructor.
}
return null;
}
/**
* Allows iterating over JarEntries.
* Every iterator is independent instance, thus is safe iterate in concurrent environment.
*
* @return {@link Iterator}
*/
Iterator iterator(){
try{
return new Iterator (
......@@ -159,11 +157,19 @@ final class InstrumentationJar {
)
)
);
} catch (IOException e){}
} catch (IOException e){
// Exception cannot be raised here, because the JarBytes are checked in constructor.
}
return null;
}
/**
* Allow using stream monadic semantic on JarEntries.
*
* @return Stream of JarEntries
*/
Stream<JarEntry> stream(){
return StreamSupport.stream (
Spliterators.spliteratorUnknownSize (iterator (), Spliterator.ORDERED),
......@@ -171,6 +177,10 @@ final class InstrumentationJar {
);
}
/**
* Iterator over JarEntries of given jar.
*/
static class Iterator implements java.util.Iterator<JarEntry> {
private final JarInputStream jis;
......@@ -183,7 +193,9 @@ final class InstrumentationJar {
public boolean hasNext () {
try {
return jis.available () > 0;
} catch (IOException e){}
} catch (IOException e){
// Exception cannot be raised here, because the JarBytes are checked in constructor.
}
return false;
}
......@@ -193,7 +205,9 @@ final class InstrumentationJar {
public JarEntry next () {
try{
return jis.getNextJarEntry ();
} catch (IOException e){}
} catch (IOException e){
// Exception cannot be raised here, because the JarBytes are checked in constructor.
}
return null;
}
......
/**
* Load the transformers classes from received jars
*
* This file is part of disl project
*
* Author: Vit Kabele <vit@kabele.me>
* Created on the 26/10/2018
*/
......@@ -11,10 +14,19 @@ import java.util.List;
import java.util.stream.Collectors;
/**
* Load the transformers from received jars.
*/
public class IsolatedTransformers implements Transformers {
private List<Transformer> transformers;
private final ByteArrayClassLoader classLoader = new ByteArrayClassLoader ();
/**
* Create new instance of {@link IsolatedTransformers} class.
* @param transformersList List of name, classBytes of transformer class
*/
IsolatedTransformers(List<Pair<String,byte[]>> transformersList){
transformers = transformersList
......@@ -61,12 +73,26 @@ public class IsolatedTransformers implements Transformers {
}
}
private Class<?> resolveTransformer (String name, byte[] classbytes){
ByteArrayClassLoader classLoader = new ByteArrayClassLoader ();
/**
* Load the class from given classBytes.
*
* @param name Class name.
* @param classbytes Class bytes.
* @return Loaded class.
*/
private Class<?> resolveTransformer (String name, byte[] classbytes){
return classLoader.createClass(name, classbytes,0, classbytes.length);
}
/**
* Create an instance of given transformer.
*
* @param transformerClass Transformer class.
* @return New instance if {@link Transformer} class.
* @throws InitializationException Failed to instantiate transformer.
*/
private Transformer instantiateTransformer (Class<?> transformerClass) throws InitializationException{
try {
return (Transformer) transformerClass.newInstance ();
......
/**
* To be thrown in case of error in instrumentation manifest.
*
* This file is part of disl project
*
* Author: Vit Kabele <vit@kabele.me>
* Created on the 26/10/2018
*/
......@@ -7,12 +10,31 @@ package ch.usi.dag.disl;
import ch.usi.dag.disl.exception.DiSLException;
/**
* To be thrown in case of error in instrumentation manifest.
*/
class ManifestException extends DiSLException {
/**
* Create new instance of {@link ManifestException} class.
*/
ManifestException() { super(); }
/**
* Create new instance of {@link ManifestException} class with message.
*
* @param cause Message.
*/
ManifestException(String cause) { super(cause); }
/**
* Retreive new instance {@link ManifestException} with prepared message.
*
* @param fieldName Missing field name.
* @return New instance of {@link ManifestException}
*/
static ManifestException MissingField(String fieldName) {
return new ManifestException (
String.format ("Missing field \"%s\" in manifest file", fieldName)
......
/**
* Provide access to disl classes from a {@link ch.usi.dag.disl.DiSL} instance.
*
* This file is part of disl project
*
* Author: Vit Kabele <vit@kabele.me>
* Created on the 26/10/2018
*/
......@@ -24,10 +27,22 @@ import java.util.Set;
import java.util.stream.Collectors;
/**
* Provide access to disl classes from a {@link ch.usi.dag.disl.DiSL} instance.
*/
public class IsolatedDislClasses implements DislClasses {
private final SnippetParser snippetParser;
/**
* Create new instance of the {@link IsolatedDislClasses} class.
*
* @param options Code options.
* @param classNodes ASM Class nodes containing information about instrumentation classes.
* @throws ParserException
* @throws ProcessorException
* @throws ReflectionException
*/
public IsolatedDislClasses(final Set<DiSL.CodeOption> options, List<ClassNode> classNodes)
throws ParserException, ProcessorException, ReflectionException {
......@@ -90,7 +105,7 @@ public class IsolatedDislClasses implements DislClasses {
.collect (Collectors.toList ());
}
private static boolean isArgumentProcessor (final ClassNode classNode) {
private boolean isArgumentProcessor (final ClassNode classNode) {
//
// An argument processor must have an @ArgumentProcessor annotation
// associated with the class. DiSL instrumentation classes may have
......
......@@ -11,6 +11,8 @@ import org.objectweb.asm.tree.ClassNode;
import java.io.IOException;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.stream.Collectors;
import static org.junit.Assert.*;
......@@ -43,7 +45,7 @@ public class InstrumentationJarTest {
* @throws ManifestException
*/
@Test
public void TestGetDislClasses() throws IOException, ManifestException {
public void TestGetDislClasses() throws ManifestException {
InstrumentationJar jar = new InstrumentationJar (SmokeExampleInsrumentation);
List<ClassNode> classNodes = jar.getDislClasses ();
......@@ -53,4 +55,26 @@ public class InstrumentationJarTest {
assertEquals ("DiSLClass", node.name);
}
@Test
public void TestIterator() throws ManifestException{
InstrumentationJar jar = new InstrumentationJar (SmokeExampleInsrumentation);
InstrumentationJar.Iterator iterator = jar.iterator ();
assertTrue ("Instrumentation does not contain any entry", iterator.hasNext ());
JarEntry je = iterator.next ();
assertEquals ("Error while reading jar.", je.getName (), "DiSLClass.class");
}
@Test
public void TestStream() throws ManifestException {
InstrumentationJar jar = new InstrumentationJar (SmokeExampleInsrumentation);
long items = jar.stream ().count ();
assertEquals ("Invalid count of entries", 2, items);
}
}
......@@ -86,7 +86,7 @@ public class ClientServerRunner extends Runner {
) throws IOException {
final List <String> command = new LinkedList <> (Arrays.asList (
__CLIENT_JAVA_COMMAND__, "-noverify",
String.format ("-agentpath:%s", _DISL_AGENT_LIB_),
String.format ("-agentpath:%s=%s", _DISL_AGENT_LIB_, testInstJar.getPath ()),
String.format ("-Xbootclasspath/a:%s", Runner.classPath (
_DISL_BYPASS_JAR_, testInstJar
))
......
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