Skip to content

Commit

Permalink
Added cache on computed IService types.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Dec 8, 2023
1 parent c63da84 commit 7d8d4a4
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
import org.eclipse.acceleo.Query;
import org.eclipse.acceleo.Variable;
import org.eclipse.acceleo.aql.completion.proposals.QueryServiceCompletionProposal;
import org.eclipse.acceleo.query.ast.Call;
import org.eclipse.acceleo.query.parser.AstValidator;
import org.eclipse.acceleo.query.runtime.ICompletionProposal;
import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
import org.eclipse.acceleo.query.runtime.IValidationResult;
import org.eclipse.acceleo.query.runtime.impl.ValidationServices;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameLookupEngine;
import org.eclipse.acceleo.query.validation.type.IType;
Expand Down Expand Up @@ -64,7 +62,7 @@ public String getName() {
}

@Override
public List<IType> getParameterTypes(IReadOnlyQueryEnvironment queryEnvironment) {
public List<IType> computeParameterTypes(IReadOnlyQueryEnvironment queryEnvironment) {
List<IType> result = new ArrayList<IType>();
final AstValidator validator = new AstValidator(new ValidationServices(queryEnvironment));
for (Variable var : getOrigin().getParameters()) {
Expand All @@ -82,19 +80,7 @@ public int getNumberOfParameters() {
}

@Override
public Set<IType> getType(Call call, ValidationServices services, IValidationResult validationResult,
IReadOnlyQueryEnvironment queryEnvironment, List<IType> argTypes) {
final AstValidator validator = new AstValidator(services);

final Set<IType> result = validator.getDeclarationTypes(queryEnvironment, validator.validate(
Collections.emptyMap(), getOrigin().getType()).getPossibleTypes(getOrigin().getType()
.getAst()));

return result;
}

@Override
public Set<IType> getType(IReadOnlyQueryEnvironment queryEnvironment) {
public Set<IType> computeType(IReadOnlyQueryEnvironment queryEnvironment) {
final AstValidator validator = new AstValidator(new ValidationServices(queryEnvironment));

final Set<IType> result = validator.getDeclarationTypes(queryEnvironment, validator.validate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public String getName() {
}

@Override
public List<IType> getParameterTypes(IReadOnlyQueryEnvironment queryEnvironment) {
public List<IType> computeParameterTypes(IReadOnlyQueryEnvironment queryEnvironment) {
final List<IType> result = new ArrayList<IType>();
final AstValidator validator = new AstValidator(new ValidationServices(queryEnvironment));
for (Variable var : getOrigin().getParameters()) {
Expand All @@ -82,7 +82,7 @@ public int getNumberOfParameters() {
}

@Override
public Set<IType> getType(IReadOnlyQueryEnvironment queryEnvironment) {
public Set<IType> computeType(IReadOnlyQueryEnvironment queryEnvironment) {
Set<IType> result = new LinkedHashSet<IType>();
result.add(new ClassType(queryEnvironment, String.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ public abstract class AbstractService<O> implements IService<O> {
*/
private final O origin;

/**
* Known {@link IReadOnlyQueryEnvironment} to invalidate cached {@link #returnTypes} and
* {@link #res}.
*/
private IReadOnlyQueryEnvironment knwonEnvironment;

/**
* Return {@link IType} cache.
*/
private Set<IType> returnTypes;

/**
* Parameters {@link IType} cache.
*/
private List<IType> res;

/**
* Constructor with an {@link Object origin}.
*
Expand All @@ -60,6 +76,44 @@ public O getOrigin() {
return this.origin;
}

@Override
public List<IType> getParameterTypes(IReadOnlyQueryEnvironment queryEnvironment) {
if (knwonEnvironment != queryEnvironment || returnTypes == null) {
knwonEnvironment = queryEnvironment;
res = computeParameterTypes(queryEnvironment);
}

return res;
}

/**
* Computes the {@link #getParameterTypes(IReadOnlyQueryEnvironment)}.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @return the {@link #getParameterTypes(IReadOnlyQueryEnvironment)}
*/
protected abstract List<IType> computeParameterTypes(IReadOnlyQueryEnvironment queryEnvironment);

@Override
public Set<IType> getType(IReadOnlyQueryEnvironment queryEnvironment) {
if (knwonEnvironment != queryEnvironment || returnTypes == null) {
knwonEnvironment = queryEnvironment;
returnTypes = computeType(queryEnvironment);
}

return returnTypes;
}

/**
* Computes the {@link #getType(IReadOnlyQueryEnvironment)}.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @return the {@link #getType(IReadOnlyQueryEnvironment)}
*/
protected abstract Set<IType> computeType(IReadOnlyQueryEnvironment queryEnvironment);

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,13 @@ private Method lookupMethod(EOperation operation) {
return result;
}

/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.IService#getName()
*/
@Override
public String getName() {
return getOrigin().getName();
}

/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.IService#getParameterTypes(org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment)
*/
@Override
public List<IType> getParameterTypes(IReadOnlyQueryEnvironment queryEnvironment) {
public List<IType> computeParameterTypes(IReadOnlyQueryEnvironment queryEnvironment) {
final List<IType> result = new ArrayList<IType>();

result.add(new EClassifierType(queryEnvironment, getOrigin().getEContainingClass()));
Expand All @@ -143,21 +133,11 @@ public List<IType> getParameterTypes(IReadOnlyQueryEnvironment queryEnvironment)
return result;
}

/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.IService#getNumberOfParameters()
*/
@Override
public int getNumberOfParameters() {
return getOrigin().getEParameters().size() + 1;
}

/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.impl.AbstractService#internalInvoke(java.lang.Object[])
*/
@Override
protected Object internalInvoke(Object[] arguments) throws Exception {
final Object result;
Expand Down Expand Up @@ -242,18 +222,13 @@ private Object eOperationJavaInvoke(Method eInvokeMethod, final Object receiver,
}
}

/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.IService#getPriority()
*/
@Override
public int getPriority() {
return PRIORITY;
}

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

final IType eClassifierType = new EClassifierType(queryEnvironment, getOrigin().getEType());
Expand All @@ -266,12 +241,6 @@ public Set<IType> getType(IReadOnlyQueryEnvironment queryEnvironment) {
return result;
}

/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.impl.AbstractService#matches(org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment,
* org.eclipse.acceleo.query.validation.type.IType[])
*/
@Override
public boolean matches(IReadOnlyQueryEnvironment queryEnvironment, IType[] argumentTypes) {
final List<Set<IType>> eClassifierTypes = new ArrayList<Set<IType>>(argumentTypes.length);
Expand Down Expand Up @@ -335,11 +304,6 @@ public boolean matches(IReadOnlyQueryEnvironment queryEnvironment, IType[] argum
return canMatch;
}

/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.IService#getShortSignature()
*/
@Override
public String getShortSignature() {
final List<IType> parameterTypes = getParameterTypes(null);
Expand All @@ -348,11 +312,6 @@ public String getShortSignature() {
return serviceShortSignature(argumentTypes);
}

/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.IService#getLongSignature()
*/
@Override
public String getLongSignature() {
final String ePkgNsURI;
Expand All @@ -375,21 +334,11 @@ public String getLongSignature() {
return ePkgNsURI + " " + eCLassName + " " + getShortSignature();
}

/**
* {@inheritDoc}
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
return obj instanceof EOperationService && ((EOperationService)obj).getOrigin().equals(getOrigin());
}

/**
* {@inheritDoc}
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return getOrigin().hashCode();
Expand Down

0 comments on commit 7d8d4a4

Please sign in to comment.