Skip to content

Commit

Permalink
Added symbolic service resolution and service loading using the JDT.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Dec 12, 2023
1 parent 7d8d4a4 commit 24cade9
Show file tree
Hide file tree
Showing 18 changed files with 730 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public IQueryWorkspaceQualifiedNameResolver createResolver(AcceleoProject accele
AcceleoPlugin.getPlugin().getClass().getClassLoader(), eclipseProject,
AcceleoParser.QUALIFIER_SEPARATOR, true);
resolver.addLoader(new ModuleLoader(new AcceleoParser(), null));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR, true));

return QueryPlugin.getPlugin().createWorkspaceQualifiedNameResolver(eclipseProject, resolver,
synchronizer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ private GenerationResult launchGeneration() {
AcceleoEvaluator evaluator = new AcceleoEvaluator(queryEnvironment.getLookupEngine());

resolver.addLoader(new ModuleLoader(new AcceleoParser(), evaluator));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR,
false));

final Object resolved = resolver.resolve(moduleQualifiedName);
final Module mainModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void run() {
resolver.clearLoaders();
resolver.addLoader(new ModuleLoader(new AcceleoParser(), evaluator));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(
AcceleoParser.QUALIFIER_SEPARATOR));
AcceleoParser.QUALIFIER_SEPARATOR, false));

final IAcceleoGenerationStrategy strategy = new DefaultGenerationStrategy(model
.getResourceSet().getURIConverter());
Expand Down Expand Up @@ -278,7 +278,8 @@ public void initialize(boolean noDebug, Map<String, Object> arguments) {
registerEPackage(queryEnvironment, EPackage.Registry.INSTANCE.getEPackage(nsURI));
}
resolver.addLoader(new ModuleLoader(new AcceleoParser(), evaluator));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR,
false));

final java.net.URI moduleBinaryURI = resolver.getBinaryURI(java.net.URI.create(moduleURI.toString()));
final String moduleQualifiedName = resolver.getQualifiedName(moduleBinaryURI);
Expand Down Expand Up @@ -334,7 +335,8 @@ protected void generateNoDebug(IQualifiedNameQueryEnvironment environment, Modul
final IQualifiedNameResolver resolver = environment.getLookupEngine().getResolver();
resolver.clearLoaders();
resolver.addLoader(new ModuleLoader(new AcceleoParser(), noDebugEvaluator));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR,
false));

final IAcceleoGenerationStrategy strategy = new DefaultGenerationStrategy(modelResource
.getResourceSet().getURIConverter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public void init() {

final AcceleoEvaluator evaluator = new AcceleoEvaluator(queryEnvironment.getLookupEngine());
resolver.addLoader(new ModuleLoader(new AcceleoParser(), evaluator));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR,
false));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,34 +157,40 @@ public ISourceLocation getSourceLocation(IQualifiedNameResolver resolver, IServi

final URI sourceURI = resolver.getSourceURI(qualifiedName);

final int identifierStartLine = module.getAst().getIdentifierStartLine(moduleElement);
final int identifierStartColumn = module.getAst().getIdentifierStartColumn(moduleElement);
final int identifierStartPosition = module.getAst().getIdentifierStartPosition(
moduleElement);
final IPosition identifierStart = new Position(identifierStartLine, identifierStartColumn,
identifierStartPosition);

final int identifierEndLine = module.getAst().getIdentifierEndLine(moduleElement);
final int identifierEndColumn = module.getAst().getIdentifierEndColumn(moduleElement);
final int identifierEndPosition = module.getAst().getIdentifierEndPosition(moduleElement);
final IPosition identifierEnd = new Position(identifierEndLine, identifierEndColumn,
identifierEndPosition);

final IRange identifierRange = new Range(identifierStart, identifierEnd);

final int startLine = module.getAst().getStartLine(moduleElement);
final int startColumn = module.getAst().getStartColumn(moduleElement);
final int startPosition = module.getAst().getStartPosition(moduleElement);
final IPosition start = new Position(startLine, startColumn, startPosition);

final int endLine = module.getAst().getEndLine(moduleElement);
final int endColumn = module.getAst().getEndColumn(moduleElement);
final int endPosition = module.getAst().getEndPosition(moduleElement);
final IPosition end = new Position(endLine, endColumn, endPosition);

final IRange range = new Range(start, end);

res = new SourceLocation(sourceURI, identifierRange, range);
if (sourceURI != null) {
final int identifierStartLine = module.getAst().getIdentifierStartLine(moduleElement);
final int identifierStartColumn = module.getAst().getIdentifierStartColumn(
moduleElement);
final int identifierStartPosition = module.getAst().getIdentifierStartPosition(
moduleElement);
final IPosition identifierStart = new Position(identifierStartLine,
identifierStartColumn, identifierStartPosition);

final int identifierEndLine = module.getAst().getIdentifierEndLine(moduleElement);
final int identifierEndColumn = module.getAst().getIdentifierEndColumn(moduleElement);
final int identifierEndPosition = module.getAst().getIdentifierEndPosition(
moduleElement);
final IPosition identifierEnd = new Position(identifierEndLine, identifierEndColumn,
identifierEndPosition);

final IRange identifierRange = new Range(identifierStart, identifierEnd);

final int startLine = module.getAst().getStartLine(moduleElement);
final int startColumn = module.getAst().getStartColumn(moduleElement);
final int startPosition = module.getAst().getStartPosition(moduleElement);
final IPosition start = new Position(startLine, startColumn, startPosition);

final int endLine = module.getAst().getEndLine(moduleElement);
final int endColumn = module.getAst().getEndColumn(moduleElement);
final int endPosition = module.getAst().getEndPosition(moduleElement);
final IPosition end = new Position(endLine, endColumn, endPosition);

final IRange range = new Range(start, end);

res = new SourceLocation(sourceURI, identifierRange, range);
} else {
res = null;
}
} else {
res = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.jdt.core;bundle-version="3.27.0",
org.eclipse.acceleo.query.ide,
org.eclipse.emf.common,
org.eclipse.debug.core
org.eclipse.debug.core,
org.eclipse.emf.ecore,
org.eclipse.acceleo.aql
Bundle-Activator: org.eclipse.acceleo.query.ide.jdt.Activator$Implementation
Export-Package: org.eclipse.acceleo.query.ide.jdt,
org.eclipse.acceleo.query.ide.jdt.runtime.impl.namespace,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*******************************************************************************
* Copyright (c) 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.query.ide.jdt.runtime.impl;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.acceleo.query.ide.jdt.Activator;
import org.eclipse.acceleo.query.ide.jdt.validation.type.EclipseJDTITypeType;
import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
import org.eclipse.acceleo.query.runtime.impl.AbstractService;
import org.eclipse.acceleo.query.runtime.impl.JavaMethodService;
import org.eclipse.acceleo.query.runtime.namespace.ILoader;
import org.eclipse.acceleo.query.validation.type.IType;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.ILocalVariable;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;

/**
* Implementation of an {@link org.eclipse.acceleo.query.runtime.IService IService} for Eclipse JDT
* {@link IMethod}.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class EclipseJDTIMethodService extends AbstractService<IMethod> {

/**
* Constructor.
*
* @param method
* the {@link IMethod}
*/
public EclipseJDTIMethodService(IMethod method) {
super(method);
}

@Override
public String getName() {
return getOrigin().getElementName();
}

@Override
public String getShortSignature() {
try {
return getOrigin().getSignature();
} catch (JavaModelException e) {
return getName() + "(...)";
}
}

@Override
public String getLongSignature() {
return getOrigin().getDeclaringType() + "::" + getShortSignature();
}

@Override
public List<IType> computeParameterTypes(IReadOnlyQueryEnvironment queryEnvironment) {
final List<IType> res = new ArrayList<IType>();

try {
for (ILocalVariable parameter : getOrigin().getParameters()) {
final org.eclipse.jdt.core.IType type = getITypeFromSignature(parameter.getTypeSignature());
res.add(getITypeType(queryEnvironment, type));
}
} catch (JavaModelException e) {
Activator.getPlugin().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"can't get parameter type from: " + getLongSignature(), e));
}

return res;
}

private org.eclipse.jdt.core.IType getITypeFromSignature(final String typeSignature)
throws JavaModelException {
final org.eclipse.jdt.core.IType res;

final String signatureSimpleName = Signature.getSignatureSimpleName(typeSignature);

if (!"void".equals(signatureSimpleName)) {
final String wrappedName = EclipseJDTITypeType.PRIMITIVE_WRAPPER_NAMES.getOrDefault(
signatureSimpleName, signatureSimpleName);
final String[][] resolvedType = getOrigin().getDeclaringType().resolveType(wrappedName);
if (resolvedType != null) {
res = getOrigin().getJavaProject().findType(resolvedType[0][0] + ILoader.DOT
+ resolvedType[0][1]);
} else {
res = null;
}
} else {
res = null;
}

return res;
}

private IType getITypeType(IReadOnlyQueryEnvironment queryEnvironment, org.eclipse.jdt.core.IType type) {
// TODO Collection types
return new EclipseJDTITypeType(queryEnvironment, type);
}

@Override
public int getNumberOfParameters() {
return getOrigin().getNumberOfParameters();
}

@Override
public int getPriority() {
return JavaMethodService.PRIORITY;
}

@Override
public Set<IType> computeType(IReadOnlyQueryEnvironment queryEnvironment) {
final Set<IType> res = new LinkedHashSet<IType>();

try {
final org.eclipse.jdt.core.IType returnType = getITypeFromSignature(getOrigin().getReturnType());
res.addAll(getIType(queryEnvironment, returnType));
} catch (JavaModelException e) {
Activator.getPlugin().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"can't get return type from: " + getLongSignature(), e));
}

return res;
}

private Set<IType> getIType(IReadOnlyQueryEnvironment queryEnvironment,
org.eclipse.jdt.core.IType returnType) {
final Set<IType> res = new LinkedHashSet<>();

res.add(new EclipseJDTITypeType(queryEnvironment, returnType));

return res;
}

@Override
protected Object internalInvoke(Object[] arguments) throws Exception {
throw new UnsupportedOperationException("This service can't be used at runtime.");
}

@Override
public boolean equals(Object obj) {
return obj instanceof EclipseJDTIMethodService && ((EclipseJDTIMethodService)obj).getOrigin().equals(
getOrigin());
}

@Override
public int hashCode() {
return getOrigin().hashCode();
}

}

0 comments on commit 24cade9

Please sign in to comment.