Commit 811107e8 authored by Romain Bioteau's avatar Romain Bioteau Committed by GitHub
Browse files

fix(groovy) scripts proposals (#2467)

* improve type inference using jdt resolver instead of instantiating
ClassNode
* Remove useless custom TypeLookup
* Remove useless custom SyntaxHighligther

Closes STUDIO-3684
parent 10b7c8bd
......@@ -11,7 +11,6 @@ Export-Package: org.bonitasoft.studio.groovy.ui;
org.bonitasoft.studio.tests,
org.bonitasoft.groovy.tests,
org.bonitasoft.studio.repository.tests.ex",
org.bonitasoft.studio.groovy.ui.contentassist,
org.bonitasoft.studio.groovy.ui.dialog,
org.bonitasoft.studio.groovy.ui.handler,
org.bonitasoft.studio.groovy.ui.job,
......
......@@ -4,13 +4,6 @@
<extension-point id="comboElementProvider" name="Provider for the Text or Data or Enum or... Widget" schema="schema/comboElementProvider.exsd"/>
<extension-point id="textordataFactory" name="TextOrData Factory" schema="schema/textordataFactory.exsd"/>
<extension
point="org.codehaus.groovy.eclipse.ui.syntaxHighlightingExtension">
<highlightingExtender
extender="org.bonitasoft.studio.groovy.ui.contentassist.BonitaSyntaxHighlighting"
natureID="org.bonitasoft.studio.common.repository.bonitaNature">
</highlightingExtender>
</extension>
<extension
point="org.bonitasoft.studio.groovy.ui.comboElementProvider">
<comboElementsProvider
......@@ -109,15 +102,6 @@
class="org.bonitasoft.studio.groovy.ui.providers.ExportGroovyScriptAction">
</exportConfigurationAction>
</extension>
<extension
point="org.eclipse.jdt.groovy.core.typeLookup">
<lookup
lookup="org.bonitasoft.studio.groovy.ui.contentassist.BonitaConstantsTypeLookup">
<appliesTo
projectNature="org.bonitasoft.studio.common.repository.bonitaNature">
</appliesTo>
</lookup>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
......
/**
* Copyright (C) 2014 BonitaSoft S.A.
* BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.groovy.ui.contentassist;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import org.bonitasoft.studio.businessobject.core.repository.BusinessObjectModelRepositoryStore;
import org.bonitasoft.studio.common.repository.RepositoryAccessor;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import com.google.common.collect.Lists;
/**
* @author Romain Bioteau
*/
@RunWith(MockitoJUnitRunner.class)
public class BonitaSyntaxHighlightingTest {
private BonitaSyntaxHighlighting bonitaSyntaxHighlighting;
@Mock
private IJavaProject javaProject;
@Mock
private IType employeeDAOType;
@Mock
private RepositoryAccessor repositoryAccessor;
@Mock
private BusinessObjectModelRepositoryStore boRepoStore;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
bonitaSyntaxHighlighting = spy(new BonitaSyntaxHighlighting(repositoryAccessor));
when(repositoryAccessor.getRepositoryStore(BusinessObjectModelRepositoryStore.class)).thenReturn(boRepoStore);
doReturn(javaProject).when(bonitaSyntaxHighlighting).currentJavaProject();
}
@Test
public void should_add_engine_provided_expression_in_additional_GJDK_keywords() throws Exception {
doReturn(new IClasspathEntry[0]).when(javaProject).getRawClasspath();
assertThat(bonitaSyntaxHighlighting.getAdditionalGJDKKeywords()).containsAll(BonitaSyntaxHighlighting.BONITA_KEYWORDS);
}
@Test
public void should_add_business_object_dao() throws Exception {
doReturn(Lists.newArrayList(employeeDAOType)).when(boRepoStore).allBusinessObjectDao(javaProject);
doReturn("EmployeeDAO").when(employeeDAOType).getElementName();
assertThat(bonitaSyntaxHighlighting.getAdditionalGJDKKeywords()).contains("employeeDAO");
}
}
package org.bonitasoft.studio.groovy.ui.contentassist;
import java.util.List;
import org.bonitasoft.studio.common.log.BonitaStudioLog;
import org.bonitasoft.studio.groovy.ScriptVariable;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
import org.eclipse.jdt.groovy.search.AbstractSimplifiedTypeLookup;
import org.eclipse.jdt.groovy.search.VariableScope;
import groovy.lang.GroovyClassLoader;
public class BonitaConstantsTypeLookup extends AbstractSimplifiedTypeLookup {
public static List<ScriptVariable> bonitaVariables;
private GroovyCompilationUnit unit;
@Override
protected TypeAndDeclaration lookupTypeAndDeclaration(final ClassNode declaringType, final String name,
final VariableScope scope) {
return bonitaVariables == null ? null : bonitaVariables.stream()
.filter(v -> v.getName().equals(name))
.findFirst()
.map(v -> {
GroovyClassLoader classLoader = unit.getModuleNode().getUnit().getClassLoader();
try {
ClassNode type = new ClassNode(classLoader.loadClass(v.getType()));
return new TypeAndDeclaration(type, null);
} catch (ClassNotFoundException e) {
BonitaStudioLog.error(e);
}
return null;
})
.orElse(null);
}
@Override
public void initialize(final GroovyCompilationUnit unit, final VariableScope topScope) {
this.unit = unit;
}
public static void setBonitaVariables(List<ScriptVariable> nodes) {
bonitaVariables = nodes;
}
}
/**
* Copyright (C) 2009 BonitaSoft S.A.
* BonitaSoft, 31 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.groovy.ui.contentassist;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Lists.newArrayList;
import java.util.ArrayList;
import java.util.List;
import org.bonitasoft.engine.expression.ExpressionConstants;
import org.bonitasoft.studio.businessobject.core.repository.BusinessObjectModelRepositoryStore;
import org.bonitasoft.studio.common.repository.RepositoryAccessor;
import org.bonitasoft.studio.common.repository.RepositoryManager;
import org.codehaus.groovy.eclipse.editor.highlighting.IHighlightingExtender;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jface.text.rules.IRule;
import com.google.common.base.Function;
/**
* @author Romain Bioteau
*/
public class BonitaSyntaxHighlighting implements IHighlightingExtender {
public static List<String> BONITA_KEYWORDS = new ArrayList<String>();
static {
BONITA_KEYWORDS.add(ExpressionConstants.API_ACCESSOR.getEngineConstantName());
BONITA_KEYWORDS.add(ExpressionConstants.ENGINE_EXECUTION_CONTEXT.getEngineConstantName());
BONITA_KEYWORDS.add(ExpressionConstants.NUMBER_OF_ACTIVE_INSTANCES.getEngineConstantName());
BONITA_KEYWORDS.add(ExpressionConstants.NUMBER_OF_TERMINATED_INSTANCES.getEngineConstantName());
BONITA_KEYWORDS.add(ExpressionConstants.NUMBER_OF_COMPLETED_INSTANCES.getEngineConstantName());
BONITA_KEYWORDS.add(ExpressionConstants.NUMBER_OF_INSTANCES.getEngineConstantName());
BONITA_KEYWORDS.add(ExpressionConstants.PROCESS_DEFINITION_ID.getEngineConstantName());
BONITA_KEYWORDS.add(ExpressionConstants.ROOT_PROCESS_INSTANCE_ID.getEngineConstantName());
BONITA_KEYWORDS.add(ExpressionConstants.PROCESS_INSTANCE_ID.getEngineConstantName());
BONITA_KEYWORDS.add(ExpressionConstants.ACTIVITY_INSTANCE_ID.getEngineConstantName());
BONITA_KEYWORDS.add(ExpressionConstants.LOOP_COUNTER.getEngineConstantName());
BONITA_KEYWORDS.add(ExpressionConstants.LOGGED_USER_ID.getEngineConstantName());
BONITA_KEYWORDS.add(ExpressionConstants.TASK_ASSIGNEE_ID.getEngineConstantName());
}
private RepositoryAccessor repositoryAccessor;
public BonitaSyntaxHighlighting() {
}
BonitaSyntaxHighlighting(final RepositoryAccessor repositoryAccessor) {
this.repositoryAccessor = repositoryAccessor;
}
@Override
public List<String> getAdditionalGJDKKeywords() {
final List<String> keyWords = new ArrayList<String>(BONITA_KEYWORDS);
final BusinessObjectModelRepositoryStore repositoryStore = getBusinessObjectRepositoryStore();
keyWords.addAll(newArrayList(transform(repositoryStore.allBusinessObjectDao(currentJavaProject()), toDAOAccessorName())));
return keyWords;
}
private BusinessObjectModelRepositoryStore getBusinessObjectRepositoryStore() {
if (repositoryAccessor != null) {
return repositoryAccessor.getRepositoryStore(BusinessObjectModelRepositoryStore.class);
}
return RepositoryManager.getInstance().getRepositoryStore(BusinessObjectModelRepositoryStore.class);
}
private Function<IType, String> toDAOAccessorName() {
return new Function<IType, String>() {
@Override
public String apply(final IType input) {
return unCapitalizeFirstLetter(input.getElementName());
}
};
}
private String unCapitalizeFirstLetter(final String elementName) {
return Character.toLowerCase(elementName.charAt(0)) + elementName.substring(1, elementName.length());
}
protected IJavaProject currentJavaProject() {
return RepositoryManager.getInstance().getCurrentRepository().getJavaProject();
}
@Override
public List<String> getAdditionalGroovyKeywords() {
return null;
}
@Override
public List<IRule> getAdditionalRules() {
return null;
}
}
......@@ -43,7 +43,6 @@ import org.bonitasoft.studio.groovy.GroovyDocumentUtil;
import org.bonitasoft.studio.groovy.GroovyPlugin;
import org.bonitasoft.studio.groovy.ScriptVariable;
import org.bonitasoft.studio.groovy.ui.Messages;
import org.bonitasoft.studio.groovy.ui.contentassist.BonitaConstantsTypeLookup;
import org.bonitasoft.studio.groovy.ui.dialog.BonitaVariableLabelProvider;
import org.bonitasoft.studio.groovy.ui.dialog.GroovyEditorDocumentationDialogTray;
import org.bonitasoft.studio.groovy.ui.dialog.TestGroovyScriptDialog;
......@@ -550,7 +549,6 @@ public class GroovyScriptExpressionEditor extends SelectionAwareExpressionEditor
dependencyJob.setContext(context);
nodes.addAll(groovyViewer.getProvidedVariables(context, filters));
dependencyJob.setNodes(nodes);
BonitaConstantsTypeLookup.setBonitaVariables(nodes);
final InputLengthValidator lenghtValidator = new InputLengthValidator("", GroovyViewer.MAX_SCRIPT_LENGTH);
String content = inputExpression.getContent();
......
......@@ -16,7 +16,6 @@ package org.bonitasoft.studio.groovy;
import java.util.Map;
import org.bonitasoft.studio.common.log.BonitaStudioLog;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
......@@ -39,14 +38,10 @@ public class BonitaScriptGroovyCompilationUnit extends GroovyCompilationUnit {
ClassNode scriptClassDummy = moduleInfo.module.getScriptClassDummy();
context.values().forEach(var -> {
String typeName = var.getType();
try {
scriptClassDummy.addField(var.getName(),
FieldNode.ACC_PUBLIC | FieldNode.ACC_FINAL,
new ClassNode(moduleInfo.module.getUnit().getClassLoader().loadClass(typeName)),
null);
} catch (ClassNotFoundException e) {
BonitaStudioLog.error(e);
}
scriptClassDummy.addField(var.getName(),
FieldNode.ACC_PUBLIC | FieldNode.ACC_FINAL,
moduleInfo.resolver.resolve(typeName),
null);
});
}
......@@ -55,11 +50,11 @@ public class BonitaScriptGroovyCompilationUnit extends GroovyCompilationUnit {
public void setContext(Map<String, ScriptVariable> context) {
this.context = context;
}
public Map<String, ScriptVariable> getContext() {
return context;
return context;
}
}
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