Java Code Examples for org.eclipse.xtext.common.types.JvmFormalParameter#setParameterType()
The following examples show how to use
org.eclipse.xtext.common.types.JvmFormalParameter#setParameterType() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractClosureTypeHelper.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
@Override public void applyToModel(IResolvedTypes resolvedTypes) { if (!closure.isExplicitSyntax()) { List<JvmFormalParameter> parametersToAdd = getParameters(); InternalEList<JvmFormalParameter> targetList = (InternalEList<JvmFormalParameter>) closure.getImplicitFormalParameters(); if (!targetList.isEmpty()) { // things are already available, do nothing return; } for(int i = 0; i < parametersToAdd.size(); i++) { JvmFormalParameter parameter = parametersToAdd.get(i); LightweightTypeReference parameterType = resolvedTypes.getActualType(parameter); if (parameterType == null) { throw new IllegalStateException("Cannot find type for parameter " + parameter.getSimpleName()); } JvmTypeReference typeReference = parameterType.toTypeReference(); parameter.setParameterType(typeReference); targetList.addUnique(parameter); } } ((XClosureImplCustom) closure).setLinked(true); }
Example 2
Source File: ReflectionTypeFactory.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
protected JvmFormalParameter createFormalParameter(Type parameterType, String paramName, JvmMember container, GenericDeclaration member, Annotation[] annotations) { JvmFormalParameter result = TypesFactory.eINSTANCE.createJvmFormalParameter(); result.setName(paramName); if (isLocal(parameterType, member)) { result.setParameterType(createLocalTypeReference(parameterType, (JvmTypeParameterDeclarator) container, member)); } else { result.setParameterType(createTypeReference(parameterType)); } if (annotations.length != 0) { InternalEList<JvmAnnotationReference> annotationsReferences = (InternalEList<JvmAnnotationReference>)result.getAnnotations(); for (Annotation annotation : annotations) { annotationsReferences.addUnique(createAnnotationReference(annotation)); } } return result; }
Example 3
Source File: XtendJvmModelInferrer.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
protected void translateParameter(JvmExecutable executable, XtendParameter parameter) { JvmFormalParameter jvmParam = typesFactory.createJvmFormalParameter(); jvmParam.setName(parameter.getName()); if (parameter.isVarArg()) { executable.setVarArgs(true); JvmGenericArrayTypeReference arrayType = typeReferences.createArrayType(jvmTypesBuilder .cloneWithProxies(parameter.getParameterType())); jvmParam.setParameterType(arrayType); } else { jvmParam.setParameterType(jvmTypesBuilder.cloneWithProxies(parameter.getParameterType())); } associator.associate(parameter, jvmParam); translateAnnotationsTo(parameter.getAnnotations(), jvmParam); if (parameter.isExtension() && typeReferences.findDeclaredType(Extension.class, parameter) != null) { jvmParam.getAnnotations().add(_annotationTypesBuilder.annotationRef(Extension.class)); } executable.getParameters().add(jvmParam); }
Example 4
Source File: JvmTypesBuilder.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
/** * Creates and returns a formal parameter for the given name and type, which is associated to the given source * element. * * @return a Java parameter given name, <code>null</code> if sourceElement or name are <code>null</code>. */ /* @Nullable */ public JvmFormalParameter toParameter(/* @Nullable */ EObject sourceElement, /* @Nullable */ String name, /* @Nullable */ JvmTypeReference typeRef) { if(sourceElement == null || name == null) return null; JvmFormalParameter result = typesFactory.createJvmFormalParameter(); result.setName(name); result.setParameterType(cloneWithProxies(typeRef)); return associate(sourceElement, result); }
Example 5
Source File: JvmModelCompleter.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected void completeJvmEnumerationType(JvmEnumerationType element) { if (element.getSuperTypes().isEmpty()) { JvmTypeReference objectType = references.getTypeForName(Enum.class, element, references.createTypeRef(element)); if (objectType != null) element.getSuperTypes().add(objectType); } addAnnotations(element); EObject primarySourceElement = associations.getPrimarySourceElement(element); JvmOperation values = typesFactory.createJvmOperation(); values.setVisibility(JvmVisibility.PUBLIC); values.setStatic(true); values.setSimpleName("values"); values.setReturnType(references.createArrayType(references.createTypeRef(element))); typeExtensions.setSynthetic(values, true); if (primarySourceElement != null) { associator.associate(primarySourceElement, values); } element.getMembers().add(values); JvmOperation valueOf = typesFactory.createJvmOperation(); valueOf.setVisibility(JvmVisibility.PUBLIC); valueOf.setStatic(true); valueOf.setSimpleName("valueOf"); valueOf.setReturnType(references.createTypeRef(element)); JvmFormalParameter param = typesFactory.createJvmFormalParameter(); param.setName("name"); param.setParameterType(references.getTypeForName(String.class, element)); valueOf.getParameters().add(param); typeExtensions.setSynthetic(valueOf, true); if (primarySourceElement != null) { associator.associate(primarySourceElement, valueOf); } element.getMembers().add(valueOf); }
Example 6
Source File: JvmExecutableBuilder.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected JvmFormalParameter createFormalParameter(BinaryTypeSignature parameterType, String paramName, JvmMember container, Map<String, JvmTypeParameter> typeParameters) { JvmFormalParameter result = TypesFactory.eINSTANCE.createJvmFormalParameter(); result.setName(paramName); result.setParameterType(proxies.createTypeReference(parameterType, typeParameters)); return result; }
Example 7
Source File: JavaElementFinderTest.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
@Test public void testParameterWithoutType_01() throws Exception { JvmOperation operation = findOperation(Object.class, "equals", 1); JvmFormalParameter parameter = operation.getParameters().get(0); parameter.setParameterType(null); IJavaElement foundElement = elementFinder.findExactElementFor(operation); assertNull(foundElement); }
Example 8
Source File: JavaElementFinderTest.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
@Test public void testParameterWithoutType_02() throws Exception { JvmOperation operation = findOperation(Object.class, "equals", 1); JvmFormalParameter parameter = operation.getParameters().get(0); parameter.setParameterType(TypesFactory.eINSTANCE.createJvmParameterizedTypeReference()); IJavaElement foundElement = elementFinder.findExactElementFor(operation); assertNull(foundElement); }
Example 9
Source File: JdtBasedTypeFactory.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
protected JvmFormalParameter createFormalParameter(ITypeBinding parameterType, String paramName, IAnnotationBinding[] annotations) { JvmFormalParameter result = TypesFactory.eINSTANCE.createJvmFormalParameter(); if (paramName != null) result.setName(paramName); result.setParameterType(createTypeReference(parameterType)); if (annotations != null && annotations.length > 0) { InternalEList<JvmAnnotationReference> parameterAnnotations = (InternalEList<JvmAnnotationReference>)result.getAnnotations(); for (IAnnotationBinding annotation : annotations) { parameterAnnotations.addUnique(createAnnotationReference(annotation)); } } return result; }
Example 10
Source File: JvmExecutableDeclarationImpl.java From xtext-xtend with Eclipse Public License 2.0 | 5 votes |
public MutableParameterDeclaration addParameter(final String name, final TypeReference type) { this.checkMutable(); ConditionUtils.checkJavaIdentifier(name, "name"); Preconditions.checkArgument((type != null), "type cannot be null"); boolean _isInferred = type.isInferred(); if (_isInferred) { throw new IllegalArgumentException("Cannot use inferred type as parameter type."); } final JvmFormalParameter param = TypesFactory.eINSTANCE.createJvmFormalParameter(); param.setName(name); param.setParameterType(this.getCompilationUnit().toJvmTypeReference(type)); this.getDelegate().getParameters().add(param); ParameterDeclaration _parameterDeclaration = this.getCompilationUnit().toParameterDeclaration(param); return ((MutableParameterDeclaration) _parameterDeclaration); }
Example 11
Source File: SARLJvmModelInferrer.java From sarl with Apache License 2.0 | 5 votes |
/** Generate a list of formal parameters with annotations for the default values. * * @param owner the JVM element to change. * @param actionContainer the container of the action. * @param varargs indicates if the signature has variadic parameter. * @param signature the description of the parameters. * @return the arguments to pass to the original function. */ protected List<String> translateSarlFormalParametersForSyntheticOperation(JvmExecutable owner, JvmGenericType actionContainer, boolean varargs, List<InferredStandardParameter> signature) { final List<String> arguments = CollectionLiterals.newArrayList(); for (final InferredStandardParameter parameterSpec : signature) { final JvmTypeReference paramType = parameterSpec.getType(); if (parameterSpec instanceof InferredValuedParameter) { final StringBuilder argumentValue = new StringBuilder(); if (paramType.getType() instanceof JvmTypeParameter) { argumentValue.append("("); //$NON-NLS-1$ argumentValue.append(paramType.getSimpleName()); argumentValue.append(") "); //$NON-NLS-1$ } argumentValue.append(this.sarlSignatureProvider.toJavaArgument( actionContainer.getIdentifier(), ((InferredValuedParameter) parameterSpec).getCallingArgument())); arguments.add(argumentValue.toString()); } else { final EObject param = parameterSpec.getParameter(); final String paramName = parameterSpec.getName(); if (!Strings.isNullOrEmpty(paramName) && paramType != null) { final JvmFormalParameter lastParam = this.typesFactory.createJvmFormalParameter(); owner.getParameters().add(lastParam); lastParam.setName(paramName); if (owner instanceof JvmOperation) { lastParam.setParameterType(cloneWithTypeParametersAndProxies(paramType, owner)); } else { lastParam.setParameterType(this.typeBuilder.cloneWithProxies(paramType)); } this.associator.associate(param, lastParam); arguments.add(paramName); } } } return arguments; }
Example 12
Source File: XtendJvmModelInferrer.java From xtext-xtend with Eclipse Public License 2.0 | 4 votes |
/** * @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; boolean override = false; 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; if (func.isOverride()) override = true; } 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); if (override) setOverride(result); return result; }
Example 13
Source File: XtendJvmModelInferrer.java From xtext-xtend with Eclipse Public License 2.0 | 4 votes |
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()); } }
Example 14
Source File: SARLJvmModelInferrer.java From sarl with Apache License 2.0 | 4 votes |
/** Append the guard evaluators. * * @param container the container type. */ protected void appendEventGuardEvaluators(JvmGenericType container) { final GenerationContext context = getContext(container); if (context != null) { final Collection<Pair<SarlBehaviorUnit, Collection<Procedure1<? super ITreeAppendable>>>> allEvaluators = context.getGuardEvaluationCodes(); if (allEvaluators == null || allEvaluators.isEmpty()) { return; } final JvmTypeReference voidType = this._typeReferenceBuilder.typeRef(Void.TYPE); final JvmTypeReference runnableType = this._typeReferenceBuilder.typeRef(Runnable.class); final JvmTypeReference collectionType = this._typeReferenceBuilder.typeRef(Collection.class, runnableType); for (final Pair<SarlBehaviorUnit, Collection<Procedure1<? super ITreeAppendable>>> evaluators : allEvaluators) { final SarlBehaviorUnit source = evaluators.getKey(); // Determine the name of the operation for the behavior output final String behName = Utils.createNameForHiddenGuardGeneralEvaluatorMethod(source.getName().getSimpleName()); // Create the main function final JvmOperation operation = this.typesFactory.createJvmOperation(); // Annotation for the event bus appendGeneratedAnnotation(operation, context); addAnnotationSafe(operation, PerceptGuardEvaluator.class); // Guard evaluator unit parameters // - Event occurrence JvmFormalParameter jvmParam = this.typesFactory.createJvmFormalParameter(); jvmParam.setName(this.grammarKeywordAccess.getOccurrenceKeyword()); jvmParam.setParameterType(this.typeBuilder.cloneWithProxies(source.getName())); this.associator.associate(source, jvmParam); operation.getParameters().add(jvmParam); // - List of runnables jvmParam = this.typesFactory.createJvmFormalParameter(); jvmParam.setName(RUNNABLE_COLLECTION); jvmParam.setParameterType(this.typeBuilder.cloneWithProxies(collectionType)); operation.getParameters().add(jvmParam); operation.setAbstract(false); operation.setNative(false); operation.setSynchronized(false); operation.setStrictFloatingPoint(false); operation.setFinal(false); operation.setVisibility(JvmVisibility.PRIVATE); operation.setStatic(false); operation.setSimpleName(behName); operation.setReturnType(this.typeBuilder.cloneWithProxies(voidType)); container.getMembers().add(operation); setBody(operation, it -> { it.append("assert "); //$NON-NLS-1$ it.append(this.grammarKeywordAccess.getOccurrenceKeyword()); it.append(" != null;"); //$NON-NLS-1$ it.newLine(); it.append("assert "); //$NON-NLS-1$ it.append(RUNNABLE_COLLECTION); it.append(" != null;"); //$NON-NLS-1$ for (final Procedure1<? super ITreeAppendable> code : evaluators.getValue()) { it.newLine(); code.apply(it); } }); this.associator.associatePrimary(source, operation); this.typeBuilder.copyDocumentationTo(source, operation); } } }
Example 15
Source File: SARLJvmModelInferrer.java From sarl with Apache License 2.0 | 4 votes |
/** Copy the JVM operations from the source to the destination. * * @param source the source. * @param target the destination. * @param createdActions the set of actions that are created before (input) or during (output) the invocation. * @param bodyBuilder the builder of the target's operations. * @since 0.5 */ @SuppressWarnings("checkstyle:npathcomplexity") protected void copyNonStaticPublicJvmOperations(JvmGenericType source, JvmGenericType target, Set<ActionPrototype> createdActions, Procedure2<? super JvmOperation, ? super ITreeAppendable> bodyBuilder) { final Iterable<JvmOperation> operations = Iterables.transform(Iterables.filter(source.getMembers(), it -> { if (it instanceof JvmOperation) { final JvmOperation op = (JvmOperation) it; return !op.isStatic() && op.getVisibility() == JvmVisibility.PUBLIC; } return false; }), it -> (JvmOperation) it); for (final JvmOperation operation : operations) { final ActionParameterTypes types = this.sarlSignatureProvider.createParameterTypesFromJvmModel( operation.isVarArgs(), operation.getParameters()); final ActionPrototype actSigKey = this.sarlSignatureProvider.createActionPrototype( operation.getSimpleName(), types); if (createdActions.add(actSigKey)) { final JvmOperation newOp = this.typesFactory.createJvmOperation(); target.getMembers().add(newOp); newOp.setAbstract(false); newOp.setFinal(false); newOp.setNative(false); newOp.setStatic(false); newOp.setSynchronized(false); newOp.setVisibility(JvmVisibility.PUBLIC); newOp.setDefault(operation.isDefault()); newOp.setDeprecated(operation.isDeprecated()); newOp.setSimpleName(operation.getSimpleName()); newOp.setStrictFloatingPoint(operation.isStrictFloatingPoint()); copyTypeParametersFromJvmOperation(operation, newOp); for (final JvmTypeReference exception : operation.getExceptions()) { newOp.getExceptions().add(cloneWithTypeParametersAndProxies(exception, newOp)); } for (final JvmFormalParameter parameter : operation.getParameters()) { final JvmFormalParameter newParam = this.typesFactory.createJvmFormalParameter(); newOp.getParameters().add(newParam); newParam.setName(parameter.getSimpleName()); newParam.setParameterType(cloneWithTypeParametersAndProxies(parameter.getParameterType(), newOp)); } newOp.setVarArgs(operation.isVarArgs()); newOp.setReturnType(cloneWithTypeParametersAndProxies(operation.getReturnType(), newOp)); setBody(newOp, it -> bodyBuilder.apply(operation, it)); } } }
Example 16
Source File: SARLJvmModelInferrer.java From sarl with Apache License 2.0 | 4 votes |
/** Initialize the SARL capacity context-aware wrapper. * * @param source the source. * @param inferredJvmType the JVM type. * @since 0.6 */ protected void appendCapacityContextAwareWrapper(SarlCapacity source, JvmGenericType inferredJvmType) { final JvmGenericType innerType = this.typesFactory.createJvmGenericType(); innerType.setInterface(false); innerType.setAbstract(false); innerType.setVisibility(JvmVisibility.PUBLIC); innerType.setStatic(true); innerType.setStrictFloatingPoint(false); innerType.setFinal(false); final String innerTypeName = Capacity.ContextAwareCapacityWrapper.class.getSimpleName(); innerType.setSimpleName(innerTypeName); inferredJvmType.getMembers().add(innerType); this.typeBuilder.setDocumentation(innerType, "@ExcludeFromApidoc"); //$NON-NLS-1$ final JvmTypeParameter typeParameter = this.typesFactory.createJvmTypeParameter(); typeParameter.setName("C"); //$NON-NLS-1$ final JvmUpperBound constraint = this.typesFactory.createJvmUpperBound(); constraint.setTypeReference(this._typeReferenceBuilder.typeRef(inferredJvmType)); typeParameter.getConstraints().add(constraint); innerType.getTypeParameters().add(typeParameter); final Iterator<JvmTypeReference> extendedTypeIterator = inferredJvmType.getExtendedInterfaces().iterator(); if (extendedTypeIterator.hasNext()) { final JvmTypeReference extendedType = extendedTypeIterator.next(); final JvmTypeReference superType = this._typeReferenceBuilder.typeRef( extendedType.getQualifiedName() + "$" + innerTypeName, //$NON-NLS-1$ this._typeReferenceBuilder.typeRef(typeParameter)); innerType.getSuperTypes().add(superType); } innerType.getSuperTypes().add(this._typeReferenceBuilder.typeRef(inferredJvmType)); final JvmConstructor constructor = this.typesFactory.createJvmConstructor(); constructor.setVisibility(JvmVisibility.PUBLIC); innerType.getMembers().add(constructor); final JvmFormalParameter parameter1 = this.typesFactory.createJvmFormalParameter(); parameter1.setName("capacity"); //$NON-NLS-1$ parameter1.setParameterType(this._typeReferenceBuilder.typeRef(typeParameter)); constructor.getParameters().add(parameter1); final JvmFormalParameter parameter2 = this.typesFactory.createJvmFormalParameter(); parameter2.setName("caller"); //$NON-NLS-1$ parameter2.setParameterType(this._typeReferenceBuilder.typeRef(AgentTrait.class)); constructor.getParameters().add(parameter2); setBody(constructor, it -> { it.append("super(capacity, caller);"); //$NON-NLS-1$ }); final Set<ActionPrototype> createdActions = new TreeSet<>(); for (final JvmGenericType sourceType : Iterables.concat( Collections.singletonList(inferredJvmType), Iterables.transform(Iterables.skip(inferredJvmType.getExtendedInterfaces(), 1), it -> { return (JvmGenericType) it.getType(); }))) { copyNonStaticPublicJvmOperations(sourceType, innerType, createdActions, (operation, it) -> { it.append("try {"); //$NON-NLS-1$ it.newLine(); it.append(" ensureCallerInLocalThread();"); //$NON-NLS-1$ it.newLine(); it.append(" "); //$NON-NLS-1$ if (operation.getReturnType() != null && !Objects.equal("void", operation.getReturnType().getIdentifier())) { //$NON-NLS-1$ it.append("return "); //$NON-NLS-1$ } it.append("this.capacity."); //$NON-NLS-1$ it.append(operation.getSimpleName()); it.append("("); //$NON-NLS-1$ boolean first = true; for (final JvmFormalParameter fparam : operation.getParameters()) { if (first) { first = false; } else { it.append(", "); //$NON-NLS-1$ } it.append(fparam.getName()); } it.append(");"); //$NON-NLS-1$ it.newLine(); it.append("} finally {"); //$NON-NLS-1$ it.newLine(); it.append(" resetCallerInLocalThread();"); //$NON-NLS-1$ it.newLine(); it.append("}"); //$NON-NLS-1$ }); } }