use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.
the class MutableJvmAnnotationTypeDeclarationImpl method addAnnotationTypeElement.
@Override
public MutableAnnotationTypeElementDeclaration addAnnotationTypeElement(final String name, final Procedure1<MutableAnnotationTypeElementDeclaration> initializer) {
this.checkMutable();
ConditionUtils.checkJavaIdentifier(name, "name");
Preconditions.checkArgument((initializer != null), "initializer cannot be null");
final JvmOperation newAnnotationElement = TypesFactory.eINSTANCE.createJvmOperation();
newAnnotationElement.setSimpleName(name);
newAnnotationElement.setVisibility(JvmVisibility.PUBLIC);
this.getDelegate().getMembers().add(newAnnotationElement);
MemberDeclaration _memberDeclaration = this.getCompilationUnit().toMemberDeclaration(newAnnotationElement);
final MutableAnnotationTypeElementDeclaration mutableAnnotationTypeElementDeclaration = ((MutableAnnotationTypeElementDeclaration) _memberDeclaration);
initializer.apply(mutableAnnotationTypeElementDeclaration);
return mutableAnnotationTypeElementDeclaration;
}
use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.
the class XtendJvmModelInferrer method transformCreateExtension.
protected void transformCreateExtension(XtendFunction source, CreateExtensionInfo createExtensionInfo, JvmGenericType container, JvmOperation operation, /* @Nullable */
JvmTypeReference returnType) {
JvmField cacheVar = jvmTypesBuilder.toField(source, CREATE_CHACHE_VARIABLE_PREFIX + source.getName(), jvmTypesBuilder.inferredType());
if (cacheVar != null) {
cacheVar.setFinal(true);
jvmTypesBuilder.setInitializer(cacheVar, compileStrategies.forCacheVariable(source));
container.getMembers().add(cacheVar);
JvmOperation initializer = typesFactory.createJvmOperation();
container.getMembers().add(initializer);
initializer.setSimpleName(CREATE_INITIALIZER_PREFIX + source.getName());
initializer.setVisibility(JvmVisibility.PRIVATE);
initializer.setReturnType(typeReferences.getTypeForName(Void.TYPE, source));
for (JvmTypeReference exception : source.getExceptions()) {
initializer.getExceptions().add(jvmTypesBuilder.cloneWithProxies(exception));
}
jvmTypesBuilder.setBody(operation, compileStrategies.forCacheMethod(createExtensionInfo, cacheVar, initializer));
// the first parameter is the created object
JvmFormalParameter jvmParam = typesFactory.createJvmFormalParameter();
jvmParam.setName(createExtensionInfo.getName());
// TODO consider type parameters
jvmParam.setParameterType(jvmTypesBuilder.inferredType());
initializer.getParameters().add(jvmParam);
associator.associate(createExtensionInfo, jvmParam);
// add all others
for (XtendParameter parameter : source.getParameters()) {
jvmParam = typesFactory.createJvmFormalParameter();
jvmParam.setName(parameter.getName());
jvmParam.setParameterType(jvmTypesBuilder.cloneWithProxies(parameter.getParameterType()));
initializer.getParameters().add(jvmParam);
associator.associate(parameter, jvmParam);
}
associator.associate(source, initializer);
setBody(operation, createExtensionInfo.getCreateExpression());
setBody(initializer, source.getExpression());
}
}
use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.
the class XtendJvmModelInferrer method initialize.
protected void initialize(XtendAnnotationType source, JvmAnnotationType inferredJvmType) {
inferredJvmType.setVisibility(source.getVisibility());
inferredJvmType.setStatic(source.isStatic() && !isTopLevel(source));
inferredJvmType.setAbstract(true);
translateAnnotationsTo(source.getAnnotations(), inferredJvmType);
jvmTypesBuilder.copyDocumentationTo(source, inferredJvmType);
for (XtendMember member : source.getMembers()) {
if (member instanceof XtendField) {
XtendField field = (XtendField) member;
if (!Strings.isEmpty(field.getName())) {
JvmOperation operation = typesFactory.createJvmOperation();
associator.associatePrimary(member, operation);
operation.setSimpleName(field.getName());
JvmTypeReference returnType = null;
XExpression initialValue = field.getInitialValue();
if (field.getType() != null) {
returnType = jvmTypesBuilder.cloneWithProxies(field.getType());
} else if (initialValue != null) {
returnType = jvmTypesBuilder.inferredType(initialValue);
}
operation.setReturnType(returnType);
if (initialValue != null) {
JvmAnnotationValue jvmAnnotationValue = jvmTypesBuilder.toJvmAnnotationValue(initialValue);
if (jvmAnnotationValue != null) {
operation.setDefaultValue(jvmAnnotationValue);
jvmAnnotationValue.setOperation(operation);
}
jvmTypesBuilder.setBody(operation, initialValue);
}
operation.setVisibility(JvmVisibility.PUBLIC);
translateAnnotationsTo(member.getAnnotations(), operation);
jvmTypesBuilder.copyDocumentationTo(member, operation);
inferredJvmType.getMembers().add(operation);
}
}
}
}
use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.
the class XtendJvmModelInferrer method deriveGenericDispatchOperationSignature.
/**
* @return a {@link JvmOperation} with common denominator argument types of all given operations
*/
/* @Nullable */
protected JvmOperation deriveGenericDispatchOperationSignature(Iterable<JvmOperation> localOperations, JvmGenericType target) {
final Iterator<JvmOperation> iterator = localOperations.iterator();
if (!iterator.hasNext())
return null;
JvmOperation first = iterator.next();
JvmOperation result = typesFactory.createJvmOperation();
target.getMembers().add(result);
for (int i = 0; i < first.getParameters().size(); i++) {
JvmFormalParameter parameter = typesFactory.createJvmFormalParameter();
result.getParameters().add(parameter);
parameter.setParameterType(jvmTypesBuilder.inferredType());
JvmFormalParameter parameter2 = first.getParameters().get(i);
parameter.setName(parameter2.getName());
}
jvmTypesBuilder.setBody(result, compileStrategies.forDispatcher(result));
JvmVisibility commonVisibility = null;
boolean isFirst = true;
boolean allStatic = true;
for (JvmOperation jvmOperation : localOperations) {
Iterable<XtendFunction> xtendFunctions = Iterables.filter(associations.getSourceElements(jvmOperation), XtendFunction.class);
for (XtendFunction func : xtendFunctions) {
JvmVisibility xtendVisibility = func.getDeclaredVisibility();
if (isFirst) {
commonVisibility = xtendVisibility;
isFirst = false;
} else if (commonVisibility != xtendVisibility) {
commonVisibility = null;
}
associator.associate(func, result);
if (!func.isStatic())
allStatic = false;
}
for (JvmTypeReference declaredException : jvmOperation.getExceptions()) result.getExceptions().add(jvmTypesBuilder.cloneWithProxies(declaredException));
}
if (commonVisibility == null)
result.setVisibility(JvmVisibility.PUBLIC);
else
result.setVisibility(commonVisibility);
result.setStatic(allStatic);
return result;
}
use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.
the class XtendReentrantTypeResolver method getReturnTypeOfOverriddenOperation.
@Override
protected LightweightTypeReference getReturnTypeOfOverriddenOperation(JvmOperation operation, ResolvedTypes resolvedTypes, IFeatureScopeSession session) {
if (operation.getVisibility() == JvmVisibility.PRIVATE)
return null;
if (InferredTypeIndicator.isInferred(operation.getReturnType())) {
LightweightTypeReference declaringType = resolvedTypes.getActualType(operation.getDeclaringType());
if (declaringType == null) {
throw new IllegalStateException("Cannot determine declaring type of operation: " + operation);
}
BottomResolvedOperation resolvedOperation = new BottomResolvedOperation(operation, declaringType, overrideTester);
List<IResolvedOperation> overriddenMethods = resolvedOperation.getOverriddenAndImplementedMethods();
if (overriddenMethods.isEmpty())
return null;
IResolvedOperation overriddenMethod = overriddenMethods.get(0);
JvmOperation declaration = overriddenMethod.getDeclaration();
XExpression inferredFrom = getInferredFrom(declaration.getReturnType());
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=439535
if (inferredFrom != null && (inferredFrom == getInferredFrom(operation.getReturnType()) || isHandled(inferredFrom))) {
return null;
}
LightweightTypeReference result = overriddenMethod.getResolvedReturnType();
return result;
}
return null;
}
Aggregations