use of org.eclipse.xtext.common.types.JvmFormalParameter in project xtext-xtend by eclipse.
the class XtendValidator method getParamTypes.
protected List<JvmType> getParamTypes(JvmOperation jvmOperation, boolean wrapPrimitives) {
List<JvmType> types = newArrayList();
for (JvmFormalParameter p : jvmOperation.getParameters()) {
LightweightTypeReference typeReference = toLightweightTypeReference(p.getParameterType());
if (wrapPrimitives) {
typeReference = typeReference.getWrapperTypeIfPrimitive();
}
types.add(typeReference.getType());
}
return types;
}
use of org.eclipse.xtext.common.types.JvmFormalParameter in project xtext-xtend by eclipse.
the class CacheMethodCompileStrategy method apply.
@Override
public void apply(ITreeAppendable appendable) {
JvmOperation cacheMethod = (JvmOperation) logicalContainerProvider.getLogicalContainer(createExtensionInfo.getCreateExpression());
JvmDeclaredType containerType = cacheMethod.getDeclaringType();
IResolvedTypes resolvedTypes = typeResolver.resolveTypes(containerType);
final ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, containerType);
LightweightTypeReference listType = owner.newReferenceTo(ArrayList.class, new TypeReferenceInitializer<ParameterizedTypeReference>() {
@Override
public LightweightTypeReference enhance(ParameterizedTypeReference reference) {
reference.addTypeArgument(owner.newWildcardTypeReference());
return reference;
}
});
String cacheVarName = cacheField.getSimpleName();
String cacheKeyVarName = appendable.declareSyntheticVariable("CacheKey", "_cacheKey");
appendable.append("final ").append(listType).append(" ").append(cacheKeyVarName).append(" = ").append(CollectionLiterals.class).append(".newArrayList(");
List<JvmFormalParameter> list = cacheMethod.getParameters();
for (Iterator<JvmFormalParameter> iterator = list.iterator(); iterator.hasNext(); ) {
JvmFormalParameter jvmFormalParameter = iterator.next();
appendable.append(getVarName(jvmFormalParameter));
if (iterator.hasNext()) {
appendable.append(", ");
}
}
appendable.append(");");
// declare result variable
LightweightTypeReference returnType = resolvedTypes.getActualType(initializerMethod.getParameters().get(0));
if (returnType != null) {
appendable.newLine().append("final ").append(returnType);
} else {
appendable.newLine().append("final Object");
}
String resultVarName = "_result";
appendable.append(" ").append(resultVarName).append(";");
// open synchronize block
appendable.newLine().append("synchronized (").append(cacheVarName).append(") {");
appendable.increaseIndentation();
// if the cache contains the key return the previously created object.
appendable.newLine().append("if (").append(cacheVarName).append(".containsKey(").append(cacheKeyVarName).append(")) {");
appendable.increaseIndentation();
appendable.newLine().append("return ").append(cacheVarName).append(".get(").append(cacheKeyVarName).append(");");
appendable.decreaseIndentation().newLine().append("}");
// execute the creation
compiler.toJavaStatement(createExtensionInfo.getCreateExpression(), appendable, true);
appendable.newLine();
appendable.append(resultVarName).append(" = ");
compiler.toJavaExpression(createExtensionInfo.getCreateExpression(), appendable);
appendable.append(";");
// store the newly created object in the cache
appendable.newLine().append(cacheVarName).append(".put(").append(cacheKeyVarName).append(", ");
LightweightTypeReference fieldType = resolvedTypes.getActualType(cacheField);
LightweightTypeReference declaredResultType = fieldType.getTypeArguments().get(1);
boolean castRequired = false;
if (!declaredResultType.isAssignableFrom(returnType)) {
castRequired = true;
appendable.append("(").append(declaredResultType).append(")");
}
appendable.append(resultVarName).append(");");
// close synchronize block
appendable.decreaseIndentation();
appendable.newLine().append("}");
appendable.newLine().append(initializerMethod.getSimpleName()).append("(").append(resultVarName);
for (JvmFormalParameter parameter : cacheMethod.getParameters()) {
appendable.append(", ").append(parameter.getName());
}
appendable.append(");");
// return the result
appendable.newLine().append("return ");
if (castRequired) {
appendable.append("(").append(declaredResultType).append(")");
}
appendable.append(resultVarName).append(";");
}
use of org.eclipse.xtext.common.types.JvmFormalParameter in project xtext-xtend by eclipse.
the class DispatchMethodCompileStrategy method generateActualDispatchCall.
protected void generateActualDispatchCall(JvmOperation dispatchOperation, JvmOperation actualOperationToCall, ITreeAppendable a, ITypeReferenceOwner owner) {
a.append(actualOperationToCall.getSimpleName()).append("(");
Iterator<JvmFormalParameter> iter1 = dispatchOperation.getParameters().iterator();
for (Iterator<JvmFormalParameter> iter2 = actualOperationToCall.getParameters().iterator(); iter2.hasNext(); ) {
JvmFormalParameter p1 = iter1.next();
JvmFormalParameter p2 = iter2.next();
LightweightTypeReference type1 = owner.toLightweightTypeReference(p1.getParameterType());
LightweightTypeReference type2 = owner.toLightweightTypeReference(p2.getParameterType());
if (!type2.isAssignableFrom(type1, new TypeConformanceComputationArgument(true, false, true, true, false, false))) {
a.append("(").append(type2.getWrapperTypeIfPrimitive()).append(")");
}
if (typeReferences.is(p2.getParameterType(), Void.class)) {
a.append("null");
} else {
a.append(getVarName(p1, a));
}
if (iter2.hasNext()) {
a.append(", ");
}
}
a.append(")");
}
use of org.eclipse.xtext.common.types.JvmFormalParameter in project xtext-xtend by eclipse.
the class MemberFromSuperImplementor method initializeExecutableBuilder.
protected void initializeExecutableBuilder(final AbstractExecutableBuilder builder, final JvmDeclaredType overrider, final IResolvedExecutable overridden) {
final JvmExecutable executable = overridden.getDeclaration();
builder.setContext(overrider);
builder.setVisibility(overridden.getDeclaration().getVisibility());
final Procedure2<LightweightTypeReference, Integer> _function = (LightweightTypeReference it, Integer index) -> {
final JvmFormalParameter declaredParameter = executable.getParameters().get((index).intValue());
final AbstractParameterBuilder parameterBuilder = builder.newParameterBuilder();
parameterBuilder.setName(declaredParameter.getSimpleName());
parameterBuilder.setType(it);
JvmAnnotationReference _findAnnotation = this.annotationLookup.findAnnotation(declaredParameter, Extension.class);
boolean _tripleNotEquals = (_findAnnotation != null);
parameterBuilder.setExtensionFlag(_tripleNotEquals);
};
IterableExtensions.<LightweightTypeReference>forEach(overridden.getResolvedParameterTypes(), _function);
builder.setVarArgsFlag(executable.isVarArgs());
builder.setExceptions(overridden.getResolvedExceptions());
}
use of org.eclipse.xtext.common.types.JvmFormalParameter in project xtext-xtend by eclipse.
the class ExtractMethodRefactoring method checkInitialConditions.
@Override
public RefactoringStatus checkInitialConditions(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
StatusWrapper status = statusProvider.get();
IResolvedTypes resolvedTypes = typeResolver.resolveTypes(firstExpression, new CancelIndicator() {
@Override
public boolean isCanceled() {
return pm.isCanceled();
}
});
try {
Set<String> calledExternalFeatureNames = newHashSet();
returnType = calculateReturnType(resolvedTypes);
if (returnType != null && !equal("void", returnType.getIdentifier()))
returnExpression = lastExpression;
boolean isReturnAllowed = isEndOfOriginalMethod();
for (EObject element : EcoreUtil2.eAllContents(originalMethod.getExpression())) {
if (pm.isCanceled()) {
throw new OperationCanceledException();
}
boolean isLocalExpression = EcoreUtil.isAncestor(expressions, element);
if (element instanceof XFeatureCall) {
XFeatureCall featureCall = (XFeatureCall) element;
JvmIdentifiableElement feature = featureCall.getFeature();
LightweightTypeReference featureType = resolvedTypes.getActualType(featureCall);
boolean isLocalFeature = EcoreUtil.isAncestor(expressions, feature);
if (!isLocalFeature && isLocalExpression) {
// call-out
if (feature instanceof JvmFormalParameter || feature instanceof XVariableDeclaration) {
if (!calledExternalFeatureNames.contains(feature.getSimpleName())) {
calledExternalFeatureNames.add(feature.getSimpleName());
ParameterInfo parameterInfo = new ParameterInfo(featureType.getIdentifier(), feature.getSimpleName(), parameterInfos.size());
parameterInfos.add(parameterInfo);
parameter2type.put(parameterInfo, featureType);
}
externalFeatureCalls.put(feature.getSimpleName(), featureCall);
}
} else if (isLocalFeature && !isLocalExpression) {
// call-in
if (returnExpression != null) {
status.add(RefactoringStatus.FATAL, "Ambiguous return value: Multiple local variables are accessed in subsequent code.");
break;
}
returnExpression = featureCall;
returnType = featureType;
}
} else if (isLocalExpression) {
if (element instanceof XReturnExpression && !isReturnAllowed) {
status.add(RefactoringStatus.FATAL, "Extracting method would break control flow due to return statements.");
break;
} else if (element instanceof JvmTypeReference) {
JvmType type = ((JvmTypeReference) element).getType();
if (type instanceof JvmTypeParameter) {
JvmOperation operation = associations.getDirectlyInferredOperation(originalMethod);
if (operation != null) {
List<JvmTypeParameter> typeParameters = operation.getTypeParameters();
if (typeParameters.contains(type))
neededTypeParameters.add((JvmTypeParameter) type);
}
}
} else if (element instanceof JvmFormalParameter)
localFeatureNames.add(((JvmFormalParameter) element).getName());
else if (element instanceof XVariableDeclaration)
localFeatureNames.add(((XVariableDeclaration) element).getIdentifier());
}
}
} catch (OperationCanceledException e) {
throw e;
} catch (Exception exc) {
handleException(exc, status);
}
return status.getRefactoringStatus();
}
Aggregations