Skip to content

Commit

Permalink
issue #58 :
Browse files Browse the repository at this point in the history
instrumentation highly improved, clean event generation api introduced
  • Loading branch information
miho committed Aug 7, 2015
1 parent fe5e6e7 commit 366bf27
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* VRLGroovyInstrumentationUtil.java
*
* Copyright (c) 2009–2014 Steinbeis Forschungszentrum (STZ Ölbronn),
* Copyright (c) 2006–2014 by Michael Hoffer
*
* This file is part of Visual Reflection Library (VRL).
*
* VRL is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* as published by the Free Software Foundation.
*
* see: http://opensource.org/licenses/LGPL-3.0
* file://path/to/VRL/src/eu/mihosoft/vrl/resources/license/lgplv3.txt
*
* VRL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* This version of VRL includes copyright notice and attribution requirements.
* According to the LGPL this information must be displayed even if you modify
* the source code of VRL. Neither the VRL Canvas attribution icon nor any
* copyright statement/attribution may be removed.
*
* Attribution Requirements:
*
* If you create derived work you must do three things regarding copyright
* notice and author attribution.
*
* First, the following text must be displayed on the Canvas or an equivalent location:
* "based on VRL source code".
*
* Second, the copyright notice must remain. It must be reproduced in any
* program that uses VRL.
*
* Third, add an additional notice, stating that you modified VRL. In addition
* you must cite the publications listed below. A suitable notice might read
* "VRL source code modified by YourName 2012".
*
* Note, that these requirements are in full accordance with the LGPL v3
* (see 7. Additional Terms, b).
*
* Publications:
*
* M. Hoffer, C.Poliwoda, G.Wittum. Visual Reflection Library -
* A Framework for Declarative GUI Programming on the Java Platform.
* Computing and Visualization in Science, in press.
*/
package eu.mihosoft.vrl.instrumentation;

/**
*
* @author Michael Hoffer <[email protected]>
*/
public class VRLGroovyInstrumentationUtil {
// http://groovy.codehaus.org/Compile-time+Metaprogramming+-+AST+Transformations
// http://hamletdarcy.blogspot.de/2009/01/groovy-compile-time-meta-magic.html
// methodcall transformations:
// http://groovy.329449.n5.nabble.com/AST-Transforming-a-MethodCallExpression-td5711841.html
// type transformations:
// http://groovy.329449.n5.nabble.com/is-possible-an-AST-transformation-to-convert-all-BigDecimals-to-doubles-in-GroovyLab-td5711461.html

private VRLGroovyInstrumentationUtil() {
throw new AssertionError("Don't instantiate me!");
}

/**
* Do not call manually! This method will be used by AST transformations to
* instrument method calls.
*
* @param staticCall defines whether method call is a static method call
* @param o instance the method belongs to
* @param mName method name
* @param args method arguments
* @return return value of the method that shall be instrumented
* @throws Throwable
*/
public static Object __instrumentCode(boolean staticCall, Object o, String mName, Object[] args) throws Throwable {

System.out.println(" --> calling " + mName + "(...)");

Object result = null;

if (staticCall) {
result = org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod((Class<?>) o, mName, args);
} else {
result = org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(o, mName, args);
}

System.out.println(" --> returning " + result);

return result;
}

/**
* Do not call manually! This method will be used by AST transformations to
* instrument method calls.
*
* @param staticCall defines whether method call is a static method call
* @param scopeId position in scope
* @param o instance the method belongs to
* @param mName method name
* @param args method arguments
* @return return value of the method that shall be instrumented
* @throws Throwable
*/
public static Object __instrumentCode(int scopeId, boolean staticCall, Object o, String mName, Object[] args) throws Throwable {

System.out.println(" --> calling " + o.toString() + "." + mName + "(...): scope=" + scopeId);

Object result = null;

if (staticCall) {
result = org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod((Class<?>) o, mName, args);
} else {
result = org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(o, mName, args);
}

System.out.println(" --> returning " + result);

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private Expression instrumentNonStaticMethodCall(Expression exp) {
// create a static method call to the instrumentation method which
// calls the original method via reflection
return new StaticMethodCallExpression(
new ClassNode(VRLInstrumentationUtil.class),
new ClassNode(VRLGroovyInstrumentationUtil.class),
"__instrumentCode", finalArgs);
}

Expand Down Expand Up @@ -288,7 +288,7 @@ private Expression instrumentStaticMethodCall(Expression exp) {
// create a static method call to the instrumentation method which
// calls the original method via reflection
return new StaticMethodCallExpression(
new ClassNode(VRLInstrumentationUtil.class),
new ClassNode(VRLGroovyInstrumentationUtil.class),
"__instrumentCode", finalArgs);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,124 +1,138 @@
/*
* VRLInstrumentationUtil.java
*
* Copyright (c) 2009–2014 Steinbeis Forschungszentrum (STZ Ölbronn),
* Copyright (c) 2006–2014 by Michael Hoffer
*
* This file is part of Visual Reflection Library (VRL).
*
* VRL is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* as published by the Free Software Foundation.
*
* see: http://opensource.org/licenses/LGPL-3.0
* file://path/to/VRL/src/eu/mihosoft/vrl/resources/license/lgplv3.txt
*
* VRL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* This version of VRL includes copyright notice and attribution requirements.
* According to the LGPL this information must be displayed even if you modify
* the source code of VRL. Neither the VRL Canvas attribution icon nor any
* copyright statement/attribution may be removed.
*
* Attribution Requirements:
*
* If you create derived work you must do three things regarding copyright
* notice and author attribution.
*
* First, the following text must be displayed on the Canvas or an equivalent location:
* "based on VRL source code".
*
* Second, the copyright notice must remain. It must be reproduced in any
* program that uses VRL.
*
* Third, add an additional notice, stating that you modified VRL. In addition
* you must cite the publications listed below. A suitable notice might read
* "VRL source code modified by YourName 2012".
*
* Note, that these requirements are in full accordance with the LGPL v3
* (see 7. Additional Terms, b).
*
* Publications:
*
* M. Hoffer, C.Poliwoda, G.Wittum. Visual Reflection Library -
* A Framework for Declarative GUI Programming on the Java Platform.
* Computing and Visualization in Science, in press.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package eu.mihosoft.vrl.instrumentation;

import eu.mihosoft.vrl.lang.model.Argument;
import eu.mihosoft.vrl.lang.model.ControlFlow;
import eu.mihosoft.vrl.lang.model.IArgument;
import eu.mihosoft.vrl.lang.model.Invocation;
import eu.mihosoft.vrl.lang.model.Type;

/**
*
* @author Michael Hoffer <[email protected]>
* Instrumentation utility class for model based instrumentation.
*
* @author Michael Hoffer &lt;[email protected]&gt;
*/
public class VRLInstrumentationUtil {
// http://groovy.codehaus.org/Compile-time+Metaprogramming+-+AST+Transformations
// http://hamletdarcy.blogspot.de/2009/01/groovy-compile-time-meta-magic.html
// methodcall transformations:
// http://groovy.329449.n5.nabble.com/AST-Transforming-a-MethodCallExpression-td5711841.html
// type transformations:
// http://groovy.329449.n5.nabble.com/is-possible-an-AST-transformation-to-convert-all-BigDecimals-to-doubles-in-GroovyLab-td5711461.html

private VRLInstrumentationUtil() {
throw new AssertionError("Don't instantiate me!");
}


/**
* Do not call manually! This method will be used by AST transformations to
* instrument method calls.
* Generates a pre event invocation for the specified invocation.
*
* @param staticCall defines whether method call is a static method call
* @param o instance the method belongs to
* @param mName method name
* @param args method arguments
* @return return value of the method that shall be instrumented
* @throws Throwable
* @param cf controlflow
* @param inv invocation
* @return the generated pre event invocation
*/
public static Object __instrumentCode(boolean staticCall, Object o, String mName, Object[] args) throws Throwable {
public static Invocation generatePreEvent(ControlFlow cf, Invocation inv) {

System.out.println(" --> calling " + mName + "(...)");
IArgument[] args = new IArgument[inv.getArguments().size() + 2];

Object result = null;
args[0] = Argument.constArg(Type.STRING, inv.getId());
args[1] = Argument.constArg(Type.STRING, inv.getMethodName());

if (staticCall) {
result = org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod((Class<?>) o, mName, args);
} else {
result = org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(o, mName, args);
for (int i = 0; i < inv.getArguments().size(); i++) {
args[i + 2] = inv.getArguments().get(i);
}

System.out.println(" --> returning " + result);
return cf.callStaticMethod(
"id",
Type.fromClass(VRLInstrumentationUtil.class),
"__preEvent", Type.VOID, args);
}

/**
* Generates a post event invocation for the specified invocation.
*
* @param cf controlflow
* @param inv invocation
* @param retValArg return value argument of the specified invocation
* @return the generated post event invocation
*/
public static Invocation generatePostEvent(ControlFlow cf,
Invocation inv, IArgument retValArg) {

IArgument[] args = new IArgument[3];

return result;
args[0] = Argument.constArg(Type.STRING, inv.getId());
args[1] = Argument.constArg(Type.STRING, inv.getMethodName());
args[2] = retValArg;

return cf.callStaticMethod(
"",
Type.fromClass(VRLInstrumentationUtil.class),
"__postEvent", Type.VOID, args);
}

/**
* Do not call manually! This method will be used by AST transformations to
* instrument method calls.
* Generates a post event invocation for the specified invocation.
*
* @param staticCall defines whether method call is a static method call
* @param scopeId position in scope
* @param o instance the method belongs to
* @param mName method name
* @param args method arguments
* @return return value of the method that shall be instrumented
* @throws Throwable
* @param cf controlflow
* @param inv invocation
* @return the generated post event invocation
*/
public static Object __instrumentCode(int scopeId, boolean staticCall, Object o, String mName, Object[] args) throws Throwable {
public static Invocation generatePostEvent(ControlFlow cf,
Invocation inv) {
IArgument[] args = new IArgument[2];

System.out.println(" --> calling " + o.toString() + "." + mName + "(...): scope=" + scopeId);
args[0] = Argument.constArg(Type.STRING, inv.getId());
args[1] = Argument.constArg(Type.STRING, inv.getMethodName());

Object result = null;
return cf.callStaticMethod(
"",
Type.fromClass(VRLInstrumentationUtil.class),
"__postEvent", Type.VOID, args);
}

if (staticCall) {
result = org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod((Class<?>) o, mName, args);
} else {
result = org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(o, mName, args);
/**
* Don't call this method manually. It is designed for the automatic model
* based instrumentation only! API is subject to change.
*
* @param id invocation id
* @param invName invocation name
* @param args invocation arguments
* @deprecated
*/
@Deprecated()
public static void __preEvent(String id, String invName, Object... args) {
String[] argsStr = new String[args.length];

for (int i = 0; i < argsStr.length; i++) {
String s = args[i] != null ? args[i].toString() : "null";
argsStr[i] = "'" + s + "'";
}

System.out.println(" --> returning " + result);
System.out.println("pre-event: " + invName + ", id: " + id + ", args: [ " + String.join(", ", argsStr) + " ]");
}

/**
* Don't call this method manually. It is designed for the automatic model
* based instrumentation only! API is subject to change.
*
* @param id invocation id
* @param invName invocation name
* @param retVal return value
* @deprecated
*/
@Deprecated()
public static void __postEvent(String id, String invName, Object retVal) {
String retValStr = "'" + retVal != null ? retVal.toString() : "null" + "'";

System.out.println("post-event: '" + invName + "', id: '" + id + "', ret: [ '" + retValStr + "' ]");
}

/**
* Don't call this method manually. It is designed for the automatic model
* based instrumentation only! API is subject to change.
*
* @param id invocation id
* @param invName invocation name
* @deprecated
*/
@Deprecated()
public static void __postEvent(String id, String invName) {

return result;
System.out.println("post-event: '" + invName + "', id: '" + id + "', ret: [ void ]");
}
}
Loading

0 comments on commit 366bf27

Please sign in to comment.