use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.
the class ScriptsInterpreterVisitor method visit.
@Override
public void visit(Script entity) {
Writer outputWriter = op().getWriter();
IBindingManager bm = getBindings();
BindingsAdapter bindingsAdapter = new BindingsAdapter(bm, true);
LanguageName languageName = entity.getLanguageName();
/*
* TODO
* if contains an upper level fragment fail
*/
Source source = entity.getSource();
IEntity sourceRoot = EntityUtils.isFragment(source) ? source.wGetRoot() : source;
IEntity result0 = bm.getResult();
IVisitor ov = op().setVisitor(sourceRoot, 0, op().getVisitor(sourceRoot, 1));
op().stagedVisit(source);
IEntity sourceModel = bm.getResult();
// workaround restore previous value (also for self and queryIterator?)
bm.setResult(result0);
op().setVisitor(sourceRoot, 0, ov);
String sourceString = toPrettyPrintString(sourceModel);
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(languageName.getValue());
if (scriptEngine == null)
throw new VisitException("Cannot find an interpreter for the language: " + languageName);
scriptEngine.setBindings(bindingsAdapter, ScriptContext.ENGINE_SCOPE);
PrintWriter printWriter = new PrintWriter(outputWriter);
scriptEngine.getContext().setWriter(printWriter);
scriptEngine.getContext().setErrorWriter(printWriter);
Object result = null;
bm.wEnterScope();
do {
try {
result = scriptEngine.eval(sourceString);
break;
} catch (ScriptException e) {
bm.wExitScope();
if (!bindingsAdapter.isAutoboxing())
throw new VisitException("Script Interpreter failure.", e);
bindingsAdapter.setAutoboxing(false);
bm.wEnterScope();
}
} while (true);
bm.wExitScope(true);
if (result instanceof IEntity)
bm.setResult((IEntity) result);
else if (result != null)
bm.setResult(BindingManagerFactory.instance.createSpecificValue(result));
}
use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.
the class ModelMergeViewer method refresh.
protected void refresh(Object input) {
IMergeViewerContentProvider contentProvider = getContentProvider();
if (contentProvider != null && input != null) {
readSideModel(MergeSide.ANCESTOR, contentProvider.getAncestorContent(input), contentProvider.getAncestorLabel(input));
readSideModel(MergeSide.LEFT, contentProvider.getLeftContent(input), contentProvider.getLeftLabel(input));
readSideModel(MergeSide.RIGHT, contentProvider.getRightContent(input), contentProvider.getRightLabel(input));
setDirty(false);
IBindingManager bm = BindingManagerFactory.instance.createArguments();
BehaviorUtils.apply("whole:org.whole.lang.changes:RevisionsLibrarySemantics#compare", viewer.getEntityContents(), bm);
// viewer.rebuildNotation();
}
}
use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.
the class BytecodeGeneratorTest method testASM.
@Test
public void testASM() throws ClassNotFoundException {
IEntity program = new ExampleTest().create();
IBindingManager args = BindingManagerFactory.instance.createArguments();
final byte[] code = BytecodeGeneratorOperation.compile(program, args);
ReflectionFactory.setUserClassLoader(new ClassLoader() {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
if ("Example".equals(name))
return defineClass("Example", code, 0, code.length);
throw new ClassNotFoundException("Unable to find class: " + name);
}
});
Class<?> exampleClass = ReflectionFactory.getPlatformClassLoader().loadClass("Example");
try {
Method main = exampleClass.getMethod("test", new Class[0]);
main.invoke(exampleClass.newInstance(), new Object[0]);
} catch (Exception e) {
fail();
}
}
use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.
the class ArtifactsWorkspaceUtils method getPersistenceKits.
public static IEntity getPersistenceKits(FileArtifact fileArtifact) {
IEntity persistenceKitIds = BindingManagerFactory.instance.createTuple();
IBindingManager bindings = BindingManagerFactory.instance.createBindingManager();
bindPath(fileArtifact, bindings, false);
if (bindings.wIsSet("persistenceKitId")) {
persistenceKitIds.wAdd(BindingManagerFactory.instance.createSpecificValue(ReflectionFactory.getPersistenceKit(bindings.wStringValue("persistenceKitId"))));
return persistenceKitIds;
} else
return getPersistenceKits();
}
use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.
the class ArtifactsWorkspaceUtils method getArtifacts.
public static IEntity getArtifacts(IEntity artifact) {
ArtifactsEntityFactory aef = ArtifactsEntityFactory.instance(RegistryConfigurations.RESOLVER);
IEntity children = BindingManagerFactory.instance.createTuple();
if (Matcher.match(ArtifactsEntityDescriptorEnum.Workspace, artifact)) {
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
for (IProject project : workspaceRoot.getProjects()) {
Project projectArtifact = aef.createProject();
projectArtifact.setName(aef.createName(project.getName()));
projectArtifact.setArtifacts(aef.createArtifacts(0));
children.wAdd(projectArtifact);
}
} else {
try {
IBindingManager bindings = BindingManagerFactory.instance.createBindingManager();
bindPath(artifact, bindings, false);
IContainer container = (IContainer) (bindings.wIsSet("folder") ? bindings.wGetValue("folder") : bindings.wGetValue("project"));
IJavaElement javaContainer = JavaCore.create(container);
if (javaContainer == null || javaContainer.getElementType() == IJavaElement.JAVA_PROJECT) {
for (IResource resource : container.members()) {
if (resource.getType() == IResource.FILE) {
appendFileArtifact(((IFile) resource).getName(), children);
} else {
FolderArtifact folderArtifact = aef.createFolderArtifact();
Metadata metadata = aef.createMetadata();
metadata.setAttributes(aef.createAttributes(aef.createAttribute(AttributeEnum.source)));
folderArtifact.setMetadata(metadata);
folderArtifact.setName(aef.createName(StringUtils.getFileName(resource.getName())));
folderArtifact.setArtifacts(aef.createArtifacts(0));
children.wAdd(folderArtifact);
}
}
} else {
if (javaContainer.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
IPackageFragmentRoot fragmentRoot = (IPackageFragmentRoot) javaContainer;
if (fragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
for (IJavaElement element : fragmentRoot.getChildren()) {
if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
if (element.getElementName() == IPackageFragment.DEFAULT_PACKAGE_NAME) {
appendPackageFragmentChildren((IPackageFragment) element, children);
} else {
PackageArtifact packageArtifact = aef.createPackageArtifact();
packageArtifact.setName(aef.createName(StringUtils.getFileName(element.getElementName())));
packageArtifact.setArtifacts(aef.createArtifacts(0));
children.wAdd(packageArtifact);
}
}
}
}
} else
// IJavaElement.PACKAGE_FRAGMENT
appendPackageFragmentChildren((IPackageFragment) javaContainer, children);
}
} catch (Exception e) {
}
}
return children;
}
Aggregations