Skip to content

Instantly share code, notes, and snippets.

@louismrose
Created June 12, 2014 08:20
Show Gist options
  • Save louismrose/cd994658cd95ac85ac88 to your computer and use it in GitHub Desktop.
Save louismrose/cd994658cd95ac85ac88 to your computer and use it in GitHub Desktop.
Obtaining values from recorded property accesses with EOL
package org.eclipse.epsilon.eol;
import java.util.Arrays;
import org.eclipse.epsilon.eol.exceptions.EolRuntimeException;
import org.eclipse.epsilon.eol.execute.context.IEolContext;
import org.eclipse.epsilon.eol.execute.introspection.IPropertyGetter;
import org.eclipse.epsilon.eol.execute.introspection.recording.IPropertyAccess;
import org.eclipse.epsilon.eol.execute.introspection.recording.PropertyAccessExecutionListener;
import org.eclipse.epsilon.eol.execute.introspection.recording.PropertyAccessRecorder;
import org.eclipse.epsilon.eol.models.java.JavaModel;
public class EolWorkbench {
public static void main(String[] args) throws Exception {
// Create a simple EOL module with a property access recorder attached
EolEvaluator module = new EolEvaluator();
PropertyAccessRecorder recorder = new PropertyAccessRecorder();
module.getContext().getExecutorFactory().addExecutionListener(new PropertyAccessExecutionListener(recorder));
// Add a model to the context, backed by a Java object
// The model contains a single Person with name=Bob and age=23
module.getContext().getModelRepository().addModel(new JavaModel("People", Arrays.asList(new Person("Bob", 23)), Arrays.asList(Person.class)));
// Obtain a reference to the first person in EOL
module.execute("var p = Person.all.first;");
// Access the properties of EOL whilst recording
recorder.startRecording();
module.execute("p.name.println();");
module.execute("p.age.println();");
recorder.stopRecording();
// Iterate over all property accesses and obtain their values
for (IPropertyAccess access : recorder.getPropertyAccesses().all()) {
System.out.println("Object: " + access.getModelElement());
System.out.println("Property: " + access.getPropertyName());
System.out.println("Value: " + getValueFromPropertyAccess(access, module.getContext()));
}
}
public static Object getValueFromPropertyAccess(IPropertyAccess propertyAccess, IEolContext context) throws EolRuntimeException {
IPropertyGetter getter = context.getIntrospectionManager().getPropertyGetterFor(propertyAccess.getModelElement(), propertyAccess.getPropertyName(), context);
return getter.invoke(propertyAccess.getModelElement(), propertyAccess.getPropertyName());
}
public static class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
public Object getValueFromPropertyAccess(IPropertyAccess propertyAccess, IEolContext context) throws EolRuntimeException {
IPropertyGetter getter = context.getIntrospectionManager().getPropertyGetterFor(propertyAccess.getModelElement(), propertyAccess.getPropertyName(), context);
return getter.invoke(propertyAccess.getModelElement(), propertyAccess.getPropertyName());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment