Commit 79facd5f authored by Vít Kabele's avatar Vít Kabele

Common transformers method extracted to abstract class

parent c4872b0c
......@@ -17,9 +17,7 @@ import java.util.stream.Collectors;
/**
* Load the transformers from received jars.
*/
public class IsolatedTransformers implements Transformers {
private List<Transformer> transformers;
class IsolatedTransformers extends Transformers {
private final ByteArrayClassLoader classLoader = new ByteArrayClassLoader ();
......@@ -36,29 +34,6 @@ public class IsolatedTransformers implements Transformers {
}
@Override
public byte[] apply (
final byte[] originalBytes
) throws TransformerException {
byte[] result = originalBytes;
for (final Transformer transformer : transformers) {
try {
final byte[] bytes = transformer.transform (result);
if (bytes != null) {
result = bytes;
}
} catch (final Exception e) {
throw new TransformerException (
e, "transformation failed in %s", transformer
);
}
}
return result;
}
private Transformer createTransformer (Pair<String, byte[]> transformer) {
final Class<?> resolvedClass = resolveTransformer (transformer.getKey (), transformer.getValue ());
......@@ -85,23 +60,4 @@ public class IsolatedTransformers implements Transformers {
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 ();
} catch (final Exception e) {
throw new InitializationException (
e, "failed to instantiate transformer %s",
transformerClass.getName ()
);
}
}
}
......@@ -6,7 +6,12 @@
*/
package ch.usi.dag.disl;
public interface Transformers {
import java.util.List;
abstract class Transformers {
protected List<Transformer> transformers;
/**
* Apply the transformers to given class.
......@@ -17,5 +22,42 @@ public interface Transformers {
*/
byte [] apply (
byte[] originalBytes
) throws TransformerException;
) throws TransformerException {
byte[] result = originalBytes;
for (final Transformer transformer : transformers) {
try {
final byte[] bytes = transformer.transform (result);
if (bytes != null) {
result = bytes;
}
} catch (final Exception e) {
throw new TransformerException (
e, "transformation failed in %s", transformer
);
}
}
return result;
}
/**
* Create an instance of given transformer.
*
* @param transformerClass Transformer class.
* @return New instance if {@link Transformer} class.
* @throws InitializationException Failed to instantiate transformer.
*/
static Transformer instantiateTransformer (Class<?> transformerClass) throws InitializationException{
try {
return (Transformer) transformerClass.newInstance ();
} catch (final Exception e) {
throw new InitializationException (
e, "failed to instantiate transformer %s",
transformerClass.getName ()
);
}
}
}
......@@ -5,44 +5,13 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
class TransformersImpl implements Transformers {
private final List<Transformer> __transformers;
//
class TransformersImpl extends Transformers {
private TransformersImpl (final List<Transformer> transformers) {
// not to be instantiated from outside
__transformers = transformers;
}
@Override
public byte[] apply (
final byte[] originalBytes
) throws TransformerException {
byte[] result = originalBytes;
for (final Transformer transformer : __transformers) {
try {
final byte[] bytes = transformer.transform (result);
if (bytes != null) {
result = bytes;
}
} catch (final Exception e) {
throw new TransformerException (
e, "transformation failed in %s", transformer
);
}
}
return result;
this.transformers = transformers;
}
//
/**
* Loads and instantiates {@link Transformer} classes.
......@@ -51,7 +20,7 @@ class TransformersImpl implements Transformers {
try {
return new TransformersImpl (
transformers
.map (className -> __createTransformer (className))
.map (TransformersImpl::__createTransformer)
.collect (Collectors.toList ())
);
} catch (final Exception e) {
......@@ -65,7 +34,7 @@ class TransformersImpl implements Transformers {
private static Transformer __createTransformer (final String className) {
final Class<?> resolvedClass = __resolveTransformer (className);
if (Transformer.class.isAssignableFrom (resolvedClass)) {
return __instantiateTransformer (resolvedClass);
return instantiateTransformer (resolvedClass);
} else {
throw new InitializationException (
"%s does not implement %s",
......@@ -86,19 +55,4 @@ class TransformersImpl implements Transformers {
}
}
private static Transformer __instantiateTransformer (
final Class<?> transformerClass
) {
try {
return (Transformer) transformerClass.newInstance ();
} catch (final Exception e) {
throw new InitializationException (
e, "failed to instantiate transformer %s",
transformerClass.getName ()
);
}
}
}
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