Commit d08cbcd9 authored by Vít Kabele's avatar Vít Kabele

Added classes and tests.

parent 82c61c1d
/**
* This file is part of disl project
* Provide convenient API to the manifest file in instrumentation jar package.
*
* Author: Vit Kabele <vit@kabele.me>
* Created on the 26/10/2018
*/
package ch.usi.dag.disl;
import java.util.List;
import java.util.jar.Manifest;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
final class DiSLManifest {
static final String DISL_CLASSES = "DiSL-Classes";
static final String DISL_TRANSFORMER = "DiSL-Transformer";
static final String DISL_TRANSFORMERS = "DiSL-Transformers";
static final String SEPARATOR = ",";
private Manifest manifest;
DiSLManifest(Manifest manifest) throws ManifestException {
this.manifest = manifest;
if(manifest.getMainAttributes ().getValue (DISL_CLASSES) == null)
throw ManifestException.MissingField (DISL_CLASSES);
}
/**
* Get names of DiSL classes from manifest attribute DISL_CLASSES
* @return List of class names
*/
List<String> getDiSLClasses(){
String DiSLClasses = manifest.getMainAttributes ().getValue (DISL_CLASSES);
Pattern splitter = Pattern.compile (SEPARATOR);
return splitter.splitAsStream (DiSLClasses)
.map (String::trim)
.filter (s -> !s.isEmpty ())
.collect (Collectors.toList ());
}
}
/**
* 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.
*
* Author: Vit Kabele <vit@kabele.me>
* Created on the 26/10/2018
*/
package ch.usi.dag.disl;
import ch.usi.dag.util.asm.ClassNodeHelper;
import org.objectweb.asm.tree.ClassNode;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
/**
* Load the Jar from bytes and provide API necessary to obtain required objects,
* such as DiSL class snippets, Transformer classes and Exclusion sets.
*/
final class InstrumentationJar {
private final byte[] JarBytes;
private DiSLManifest diSLManifest;
/**
* Create new instance of InstrumentationJar class based on provided byte array.
*
* @param JarBytes Byte array representing jar file.
* @throws IOException Error while reading, missing manifest or missing one of the mandatory fields.
*/
InstrumentationJar(byte[] JarBytes) throws IOException, ManifestException {
this.JarBytes = JarBytes;
JarInputStream jis = new JarInputStream ( new ByteArrayInputStream (JarBytes), false );
Manifest manifest = jis.getManifest ();
if (manifest == null)
throw new IOException ("Given Jar file has no manifest included.");
diSLManifest = new DiSLManifest (manifest);
}
/**
* Returns the Class no
* @return ClassNodes of the DiSL classes specified in manifest file
* @throws IOException On input error
*/
List<ClassNode> getDislClasses() throws IOException {
List<ClassNode> ClassNodes = new ArrayList<> ();
JarInputStream jis = new JarInputStream ( new ByteArrayInputStream (JarBytes));
List<String> dislClasses = diSLManifest.getDiSLClasses ().stream ().map( s -> s += ".class" ).collect(
Collectors.toList());
JarEntry je;
while((je = jis.getNextJarEntry ()) != null){
if(
!je.isDirectory () &&
dislClasses.contains (je.getName ())
){
ClassNodes.add ( ClassNodeHelper.SNIPPET.unmarshal (jis));
}
}
return ClassNodes;
}
}
/**
* This file is part of disl project
* Author: Vit Kabele <vit@kabele.me>
* Created on the 26/10/2018
*/
package ch.usi.dag.disl;
import ch.usi.dag.disl.exception.DiSLException;
class ManifestException extends DiSLException {
ManifestException() { super(); }
ManifestException(String cause) { super(cause); }
static ManifestException MissingField(String fieldName) {
return new ManifestException (
String.format ("Missing field \"%s\" in manifest file", fieldName)
);
}
}
package ch.usi.dag.disl;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Manifest;
import static org.junit.Assert.*;
/**
* This file is part of disl project
* Author: Vit Kabele <vit@kabele.me>
* Created on the 26/10/2018
*/
public class DiSLManifestTest {
@Test
public void testGetDiSLClasses () throws ManifestException {
String dislClass = "Abcdef";
Manifest manifest = new Manifest ();
manifest.getMainAttributes ().putValue ( DiSLManifest.DISL_CLASSES, dislClass );
DiSLManifest diSLManifest = new DiSLManifest (manifest);
List<String> classes = diSLManifest.getDiSLClasses ();
assertEquals ( classes.get (0), dislClass);
}
@Test
public void testGetMultipleDiSLClasses () throws ManifestException {
ArrayList<String> list = new ArrayList<>();
list.add ("DislClass");
list.add ("JustAnotherDislClass");
Manifest manifest = new Manifest ();
manifest.getMainAttributes ().putValue (
DiSLManifest.DISL_CLASSES,
String.join (DiSLManifest.SEPARATOR, list)
);
DiSLManifest diSLManifest = new DiSLManifest (manifest);
assertArrayEquals (
list.toArray (),
diSLManifest.getDiSLClasses ().toArray ()
);
}
}
This diff is collapsed.
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