Commit 59e69d90 authored by Lubomir Bulej's avatar Lubomir Bulej

ParserHelper, SnippetParser, Weaver: cosmetic cleanup before subsequent merge.

parent 7a6819fa
......@@ -16,101 +16,105 @@ import ch.usi.dag.disl.exception.ReflectionException;
import ch.usi.dag.disl.util.AsmHelper.Insns;
import ch.usi.dag.disl.util.ReflectionHelper;
// package visible
abstract class ParserHelper {
public static Class<?> getGuard(Type guardType) throws ReflectionException {
public static Class <?> getGuard (final Type guardType)
throws ReflectionException {
if(guardType == null) {
if (guardType == null) {
return null;
}
return ReflectionHelper.resolveClass(guardType);
return ReflectionHelper.resolveClass (guardType);
}
// NOTE: first parameter is modified by this function
public static <T> void parseAnnotation(T parsedDataObject,
AnnotationNode annotation) {
// NOTE: first parameter is modified by this function
public static <T> void parseAnnotation (
final T parsedDataObject, final AnnotationNode annotation
) {
try {
// nothing to do
if(annotation.values == null) {
if (annotation.values == null) {
return;
}
Iterator<?> it = annotation.values.iterator();
final Iterator <?> it = annotation.values.iterator ();
while (it.hasNext()) {
while (it.hasNext ()) {
// get attribute name
String name = (String) it.next();
final String name = (String) it.next ();
// find correct field
Field attr = parsedDataObject.getClass().getField(name);
final Field attr = parsedDataObject.getClass ().getField (name);
if (attr == null) {
throw new DiSLFatalException("Unknow attribute "
+ name
+ " in annotation "
+ Type.getType(annotation.desc).toString()
+ ". This may happen if annotation class is changed"
+ " but parser class is not.");
throw new DiSLFatalException ("Unknown attribute "
+ name
+ " in annotation "
+ Type.getType (annotation.desc).toString ()
+ ". This may happen if annotation class is changed"
+ " but parser class is not.");
}
// set attribute value into the field
attr.set(parsedDataObject, it.next());
attr.set (parsedDataObject, it.next ());
}
} catch (Exception e) {
throw new DiSLFatalException(
"Reflection error wihle parsing annotation", e);
} catch (final Exception e) {
throw new DiSLFatalException (
"Reflection error while parsing annotation", e);
}
}
public static void usesContextProperly(String className, String methodName,
String methodDescriptor, InsnList instructions)
throws ParserException {
Type[] types = Type.getArgumentTypes(methodDescriptor);
public static void usesContextProperly (
final String className, final String methodName,
final String methodDescriptor, final InsnList instructions
) throws ParserException {
final Type [] types = Type.getArgumentTypes (methodDescriptor);
int maxArgIndex = 0;
// count the max index of arguments
for (int i = 0; i < types.length; i++) {
for (final Type type : types) {
// add number of occupied slots
maxArgIndex += types[i].getSize();
maxArgIndex += type.getSize ();
}
// The following code assumes that all disl snippets are static
for (AbstractInsnNode instr : Insns.selectAll (instructions)) {
for (final AbstractInsnNode instr : Insns.selectAll (instructions)) {
switch (instr.getOpcode()) {
switch (instr.getOpcode ()) {
// test if the context is stored somewhere else
case Opcodes.ALOAD: {
int local = ((VarInsnNode) instr).var;
final int local = ((VarInsnNode) instr).var;
if (local < maxArgIndex
&& instr.getNext().getOpcode() == Opcodes.ASTORE) {
throw new ParserException("In method " + className
+ "." + methodName + " - method parameter"
+ " (context) cannot be stored into local"
+ " variable");
&& instr.getNext ().getOpcode () == Opcodes.ASTORE) {
throw new ParserException ("In method " + className
+ "." + methodName + " - method parameter"
+ " (context) cannot be stored into local"
+ " variable");
}
break;
}
// test if something is stored in the context
// test if something is stored in the context
case Opcodes.ASTORE: {
int local = ((VarInsnNode) instr).var;
final int local = ((VarInsnNode) instr).var;
if (local < maxArgIndex) {
throw new ParserException("In method " + className
+ "." + methodName + " - method parameter"
+ " (context) cannot be overwritten");
throw new ParserException ("In method " + className
+ "." + methodName + " - method parameter"
+ " (context) cannot be overwritten");
}
break;
......
......@@ -44,14 +44,14 @@ import ch.usi.dag.disl.util.ReflectionHelper;
*/
class SnippetParser extends AbstractParser {
private List<Snippet> snippets = new LinkedList<Snippet>();
private final List<Snippet> snippets = new LinkedList<Snippet>();
public List<Snippet> getSnippets() {
return snippets;
}
public void parse(ClassNode classNode) throws ParserException,
public void parse(final ClassNode classNode) throws ParserException,
SnippetParserException, ReflectionException, ScopeParserException,
StaticContextGenException, MarkerException,
GuardException {
......@@ -61,7 +61,7 @@ class SnippetParser extends AbstractParser {
// process local variables
processLocalVars(classNode);
for (MethodNode method : classNode.methods) {
for (final MethodNode method : classNode.methods) {
// skip the constructor
if (Constants.isConstructorName (method.name)) {
......@@ -78,7 +78,7 @@ class SnippetParser extends AbstractParser {
}
// parse snippet
private Snippet parseSnippet(String className, MethodNode method)
private Snippet parseSnippet(final String className, final MethodNode method)
throws SnippetParserException, ReflectionException,
ScopeParserException, StaticContextGenException, MarkerException,
GuardException, ParserException {
......@@ -113,25 +113,25 @@ class SnippetParser extends AbstractParser {
+ method.name + " cannot throw any exception");
}
AnnotationNode annotation = method.invisibleAnnotations.get(0);
final AnnotationNode annotation = method.invisibleAnnotations.get(0);
SnippetAnnotationData annotData =
final SnippetAnnotationData annotData =
parseMethodAnnotation(className + "." + method.name, annotation);
// ** marker **
Marker marker =
final Marker marker =
getMarker(annotData.marker, annotData.args);
// ** scope **
Scope scope = new ScopeImpl(annotData.scope);
final Scope scope = new ScopeImpl(annotData.scope);
// ** guard **
Class<?> guardClass = ParserHelper.getGuard(annotData.guard);
Method guardMethod = GuardHelper.findAndValidateGuardMethod(guardClass,
final Class<?> guardClass = ParserHelper.getGuard(annotData.guard);
final Method guardMethod = GuardHelper.findAndValidateGuardMethod(guardClass,
GuardHelper.snippetContextSet());
// ** parse used static and dynamic context **
Contexts context = parseUsedContexts(method.desc);
final Contexts context = parseUsedContexts(method.desc);
// ** checks **
......@@ -148,7 +148,7 @@ class SnippetParser extends AbstractParser {
// ** create unprocessed code holder class **
// code is processed after everything is parsed
SnippetUnprocessedCode uscd = new SnippetUnprocessedCode(className,
final SnippetUnprocessedCode uscd = new SnippetUnprocessedCode(className,
method.name, method.instructions, method.tryCatchBlocks,
context.getStaticContexts(), context.usesDynamicContext(),
annotData.dynamicBypass, context.usesClassContext(),
......@@ -159,10 +159,10 @@ class SnippetParser extends AbstractParser {
scope, guardMethod, annotData.order, uscd);
}
private SnippetAnnotationData parseMethodAnnotation(String fullMethodName,
AnnotationNode annotation) throws SnippetParserException {
private SnippetAnnotationData parseMethodAnnotation(final String fullMethodName,
final AnnotationNode annotation) throws SnippetParserException {
Type annotationType = Type.getType(annotation.desc);
final Type annotationType = Type.getType(annotation.desc);
// after annotation
if (annotationType.equals(Type.getType(After.class))) {
......@@ -202,15 +202,15 @@ class SnippetParser extends AbstractParser {
public int order = 100; // default
public boolean dynamicBypass = true; // default
public SnippetAnnotationData(Class<?> type) {
public SnippetAnnotationData(final Class<?> type) {
this.type = type;
}
}
private SnippetAnnotationData parseMethodAnnotFields(Class<?> type,
AnnotationNode annotation) {
private SnippetAnnotationData parseMethodAnnotFields(final Class<?> type,
final AnnotationNode annotation) {
SnippetAnnotationData sad = new SnippetAnnotationData(type);
final SnippetAnnotationData sad = new SnippetAnnotationData(type);
ParserHelper.parseAnnotation(sad, annotation);
if (sad.marker == null) {
......@@ -224,14 +224,14 @@ class SnippetParser extends AbstractParser {
return sad;
}
private Marker getMarker(Type markerType, String markerParam)
private Marker getMarker(final Type markerType, final String markerParam)
throws ReflectionException, MarkerException {
// get marker class - as generic class
Class<?> genMarkerClass = ReflectionHelper.resolveClass(markerType);
final Class<?> genMarkerClass = ReflectionHelper.resolveClass(markerType);
// get marker class - as subclass
Class<? extends Marker> markerClass =
final Class<? extends Marker> markerClass =
genMarkerClass.asSubclass(Marker.class);
// instantiate marker WITHOUT Parameter as an argument
......@@ -239,7 +239,7 @@ class SnippetParser extends AbstractParser {
try {
return ReflectionHelper.createInstance(markerClass);
}
catch(ReflectionException e) {
catch(final ReflectionException e) {
if(e.getCause() instanceof NoSuchMethodException) {
throw new MarkerException("Marker " + markerClass.getName()
......@@ -257,7 +257,7 @@ class SnippetParser extends AbstractParser {
return ReflectionHelper.createInstance(markerClass, new Parameter(
markerParam));
}
catch(ReflectionException e) {
catch(final ReflectionException e) {
if(e.getCause() instanceof NoSuchMethodException) {
throw new MarkerException("Marker " + markerClass.getName()
......@@ -270,13 +270,13 @@ class SnippetParser extends AbstractParser {
private static class Contexts {
private Set<String> staticContexts;
private boolean usesDynamicContext;
private boolean usesClassContext;
private boolean usesProcessorContext;
private final Set<String> staticContexts;
private final boolean usesDynamicContext;
private final boolean usesClassContext;
private final boolean usesProcessorContext;
public Contexts(Set<String> staticContexts, boolean usesDynamicContext,
boolean usesClassContext, boolean usesProcessorContext) {
public Contexts(final Set<String> staticContexts, final boolean usesDynamicContext,
final boolean usesClassContext, final boolean usesProcessorContext) {
super();
this.staticContexts = staticContexts;
this.usesDynamicContext = usesDynamicContext;
......@@ -301,15 +301,15 @@ class SnippetParser extends AbstractParser {
}
}
private Contexts parseUsedContexts(String methodDesc)
private Contexts parseUsedContexts(final String methodDesc)
throws ReflectionException, StaticContextGenException {
Set<String> knownStCo = new HashSet<String>();
final Set<String> knownStCo = new HashSet<String>();
boolean usesDynamicContext = false;
boolean usesClassContext = false;
boolean usesArgProcContext = false;
for (Type argType : Type.getArgumentTypes(methodDesc)) {
for (final Type argType : Type.getArgumentTypes(methodDesc)) {
// skip dynamic context class - don't check anything
if (argType.equals(Type.getType(DynamicContext.class))) {
......@@ -329,7 +329,7 @@ class SnippetParser extends AbstractParser {
continue;
}
Class<?> argClass = ReflectionHelper.resolveClass(argType);
final Class<?> argClass = ReflectionHelper.resolveClass(argType);
// static context should implement static context interface
if (!ReflectionHelper.implementsInterface(argClass,
......
......@@ -154,36 +154,36 @@ public class Weaver {
// Return a successor label of weaving location corresponding to
// the input 'end'.
private static LabelNode getEndLabel(
MethodNode methodNode, AbstractInsnNode instr
final MethodNode methodNode, AbstractInsnNode instr
) {
if (Insns.FORWARD.nextRealInsn (instr) != null) {
LabelNode branch = new LabelNode();
final LabelNode branch = new LabelNode();
methodNode.instructions.insert(instr, branch);
JumpInsnNode jump = new JumpInsnNode(Opcodes.GOTO, branch);
final JumpInsnNode jump = new JumpInsnNode(Opcodes.GOTO, branch);
methodNode.instructions.insert(instr, jump);
instr = jump;
}
// Create a label just after the 'GOTO' instruction.
LabelNode label = new LabelNode();
final LabelNode label = new LabelNode();
methodNode.instructions.insert(instr, label);
return label;
}
// generate a try catch block node given the scope of the handler
public static TryCatchBlockNode getTryCatchBlock(MethodNode methodNode,
public static TryCatchBlockNode getTryCatchBlock(final MethodNode methodNode,
AbstractInsnNode start, AbstractInsnNode end) {
InsnList ilst = methodNode.instructions;
final InsnList ilst = methodNode.instructions;
int new_start_offset = ilst.indexOf(start);
int new_end_offset = ilst.indexOf(end);
final int new_end_offset = ilst.indexOf(end);
for (TryCatchBlockNode tcb : methodNode.tryCatchBlocks) {
for (final TryCatchBlockNode tcb : methodNode.tryCatchBlocks) {
int start_offset = ilst.indexOf(tcb.start);
int end_offset = ilst.indexOf(tcb.end);
final int start_offset = ilst.indexOf(tcb.start);
final int end_offset = ilst.indexOf(tcb.end);
if (
AsmHelper.offsetBefore (ilst, new_start_offset, start_offset)
......@@ -204,23 +204,23 @@ public class Weaver {
start = ilst.get(new_start_offset);
end = ilst.get(new_end_offset);
LabelNode startLabel = (LabelNode) start;
LabelNode endLabel = getEndLabel(methodNode, end);
final LabelNode startLabel = (LabelNode) start;
final LabelNode endLabel = getEndLabel(methodNode, end);
return new TryCatchBlockNode(startLabel, endLabel, endLabel, null);
}
private static void insert(MethodNode methodNode,
SCGenerator staticInfoHolder, PIResolver piResolver,
WeavingInfo info, Snippet snippet, SnippetCode code, Shadow shadow,
AbstractInsnNode loc) throws InvalidContextUsageException {
private static void insert(final MethodNode methodNode,
final SCGenerator staticInfoHolder, final PIResolver piResolver,
final WeavingInfo info, final Snippet snippet, final SnippetCode code, final Shadow shadow,
final AbstractInsnNode loc) throws InvalidContextUsageException {
// exception handler will discard the stack and push the
// exception object. Thus, before entering this snippet,
// weaver must backup the stack and restore when exiting
if (code.containsHandledException() && info.stackNotEmpty(loc)) {
InsnList backup = info.backupStack (loc, methodNode.maxLocals);
InsnList restore = info.restoreStack (loc, methodNode.maxLocals);
final InsnList backup = info.backupStack (loc, methodNode.maxLocals);
final InsnList restore = info.restoreStack (loc, methodNode.maxLocals);
methodNode.maxLocals += info.getStackHeight(loc);
......@@ -228,7 +228,7 @@ public class Weaver {
methodNode.instructions.insert(loc, restore);
}
WeavingCode wCode = new WeavingCode(info, methodNode,
final WeavingCode wCode = new WeavingCode(info, methodNode,
code, snippet, shadow, loc);
wCode.transform(staticInfoHolder, piResolver, false);
......@@ -236,18 +236,18 @@ public class Weaver {
methodNode.tryCatchBlocks.addAll(wCode.getTCBs());
}
public static void instrument(ClassNode classNode, MethodNode methodNode,
Map<Snippet, List<Shadow>> snippetMarkings,
List<SyntheticLocalVar> syntheticLocalVars,
SCGenerator staticInfoHolder, PIResolver piResolver)
public static void instrument(final ClassNode classNode, final MethodNode methodNode,
final Map<Snippet, List<Shadow>> snippetMarkings,
final List<SyntheticLocalVar> syntheticLocalVars,
final SCGenerator staticInfoHolder, final PIResolver piResolver)
throws InvalidContextUsageException {
WeavingInfo info = new WeavingInfo(classNode, methodNode,
final WeavingInfo info = new WeavingInfo(classNode, methodNode,
snippetMarkings);
for (Snippet snippet : info.getSortedSnippets()) {
List<Shadow> shadows = snippetMarkings.get(snippet);
SnippetCode code = snippet.getCode();
for (final Snippet snippet : info.getSortedSnippets()) {
final List<Shadow> shadows = snippetMarkings.get(snippet);
final SnippetCode code = snippet.getCode();
// skip snippet with empty code
if (code == null) {
......@@ -258,9 +258,9 @@ public class Weaver {
// For @Before, instrument the snippet just before the
// entrance of a region.
if (snippet.getAnnotationClass().equals(Before.class)) {
for (Shadow shadow : shadows) {
for (final Shadow shadow : shadows) {
AbstractInsnNode loc = shadow.getWeavingRegion().getStart();
final AbstractInsnNode loc = shadow.getWeavingRegion().getStart();
insert(methodNode, staticInfoHolder, piResolver, info,
snippet, code, shadow, loc);
}
......@@ -270,9 +270,9 @@ public class Weaver {
// after each adjusted exit of a region.
if (snippet.getAnnotationClass().equals(AfterReturning.class)
|| snippet.getAnnotationClass().equals(After.class)) {
for (Shadow shadow : shadows) {
for (final Shadow shadow : shadows) {
for (AbstractInsnNode loc : shadow.getWeavingRegion().getEnds()) {
for (final AbstractInsnNode loc : shadow.getWeavingRegion().getEnds()) {
insert(methodNode, staticInfoHolder, piResolver, info,
snippet, code, shadow, loc);
......@@ -286,19 +286,19 @@ public class Weaver {
if (snippet.getAnnotationClass().equals(AfterThrowing.class)
|| snippet.getAnnotationClass().equals(After.class)) {
for (Shadow shadow : shadows) {
for (final Shadow shadow : shadows) {
WeavingRegion region = shadow.getWeavingRegion();
final WeavingRegion region = shadow.getWeavingRegion();
// after-throwing inserts the snippet once, and marks
// the start and the very end as the scope
AbstractInsnNode loc = region.getAfterThrowEnd();
final AbstractInsnNode loc = region.getAfterThrowEnd();
WeavingCode wCode = new WeavingCode(info, methodNode, code,
final WeavingCode wCode = new WeavingCode(info, methodNode, code,
snippet, shadow, loc);
wCode.transform(staticInfoHolder, piResolver, true);
// Create a try-catch clause
TryCatchBlockNode tcb = getTryCatchBlock(methodNode,
final TryCatchBlockNode tcb = getTryCatchBlock(methodNode,
region.getAfterThrowStart(), loc);
methodNode.instructions.insert(tcb.handler,
......
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