Java Code Examples for org.eclipse.xtext.common.types.JvmExecutable#getParameters()

The following examples show how to use org.eclipse.xtext.common.types.JvmExecutable#getParameters() . 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: CrySLParser.java    From CogniCrypt with Eclipse Public License 2.0 6 votes vote down vote up
private List<CrySLForbiddenMethod> getForbiddenMethods(final EList<ForbMethod> methods) {
	final List<CrySLForbiddenMethod> methodSignatures = new ArrayList<>();
	for (final ForbMethod fm : methods) {
		final JvmExecutable meth = fm.getJavaMeth();
		final List<Entry<String, String>> pars = new ArrayList<>();
		for (final JvmFormalParameter par : meth.getParameters()) {
			pars.add(new SimpleEntry<>(par.getSimpleName(), par.getParameterType().getSimpleName()));
		}
		final List<CrySLMethod> crysl = new ArrayList<>();

		final Event alternative = fm.getRep();
		if (alternative != null) {
			crysl.addAll(CrySLParserUtils.resolveAggregateToMethodeNames(alternative));
		}
		methodSignatures.add(new CrySLForbiddenMethod(
				new CrySLMethod(meth.getDeclaringType().getIdentifier() + "." + meth.getSimpleName(), pars, null, new SimpleEntry<>(UNDERSCORE, ANY_TYPE)), false, crysl));
	}
	return methodSignatures;
}
 
Example 2
Source File: ExpressionScope.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void appendParameters(JvmExecutable executable, StringBuilder result, boolean extension) {
	List<JvmFormalParameter> parameters = executable.getParameters();
	int start = extension ? 1 : 0;
	int end = parameters.size();
	if (start != end) {
		result.append('(');
		for(int i = start; i < end; i++) {
			if (i != start) {
				result.append(',');
			}
			JvmFormalParameter parameter = parameters.get(i);
			LightweightTypeReference parameterType = getParameterType(parameter);
			if (parameterType != null)
				result.append(parameterType.getIdentifier());
			else
				result.append("[null]");
		}
		result.append(')');
	}
}
 
Example 3
Source File: ReflectionTypeFactory.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected <T extends Member & GenericDeclaration> void enhanceExecutable(JvmExecutable result, T member,
		String simpleName, Type[] parameterTypes, Annotation[][] annotations, int offset) {
	StringBuilder fqName = new StringBuilder(48);
	fqName.append(member.getDeclaringClass().getName());
	fqName.append('.');
	fqName.append(simpleName);
	fqName.append('(');
	InternalEList<JvmFormalParameter> parameters = (InternalEList<JvmFormalParameter>)result.getParameters();
	for (int typeIdx = offset, annotationIdx = annotations.length - parameterTypes.length + offset; typeIdx < parameterTypes.length; typeIdx++, annotationIdx++) {
		if (typeIdx != offset)
			fqName.append(',');
		Type parameterType = parameterTypes[typeIdx];
		uriHelper.computeTypeName(parameterType, fqName);
		parameters.addUnique(
				createFormalParameter(parameterType, "arg" + (typeIdx - offset), result, member,
						annotations[annotationIdx]));
	}
	fqName.append(')');
	result.internalSetIdentifier(fqName.toString());
	result.setSimpleName(simpleName);
	setVisibility(result, member.getModifiers());
}
 
Example 4
Source File: XtypeProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void appendParameters(StyledString result, JvmExecutable executable, int insignificantParameters, LightweightTypeReferenceFactory ownedConverter) {
	List<JvmFormalParameter> declaredParameters = executable.getParameters();
	List<JvmFormalParameter> relevantParameters = declaredParameters.subList(Math.min(insignificantParameters, declaredParameters.size()), declaredParameters.size());
	for(int i = 0; i < relevantParameters.size(); i++) {
		JvmFormalParameter parameter = relevantParameters.get(i);
		if (i != 0)
			result.append(", ");
		if (i == relevantParameters.size() - 1 && executable.isVarArgs() && parameter.getParameterType() instanceof JvmGenericArrayTypeReference) {
			JvmGenericArrayTypeReference parameterType = (JvmGenericArrayTypeReference) parameter.getParameterType();
			result.append(ownedConverter.toLightweightReference(parameterType.getComponentType()).getHumanReadableName());
			result.append("...");
		} else {
			if (parameter.getParameterType()!= null) {
				String simpleName = ownedConverter.toLightweightReference(parameter.getParameterType()).getHumanReadableName();
				if (simpleName != null) // is null if the file is not on the class path
					result.append(simpleName);
			}
		}
		result.append(' ');
		result.append(notNull(parameter.getName()));
	}
}
 
Example 5
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private void setParameterNamesAndAnnotations(IMethodBinding method, ITypeBinding[] parameterTypes,
		String[] parameterNames, JvmExecutable result) {
	InternalEList<JvmFormalParameter> parameters = (InternalEList<JvmFormalParameter>)result.getParameters();
	for (int i = 0; i < parameterTypes.length; i++) {
		IAnnotationBinding[] parameterAnnotations;
		try {
			parameterAnnotations = method.getParameterAnnotations(i);
		} catch(AbortCompilation aborted) {
			parameterAnnotations = null;
		}
		ITypeBinding parameterType = parameterTypes[i];
		String parameterName = parameterNames == null ? null /* lazy */ : i < parameterNames.length ? parameterNames[i] : "arg" + i;
		JvmFormalParameter formalParameter = createFormalParameter(parameterType, parameterName, parameterAnnotations);
		parameters.addUnique(formalParameter);
	}
}
 
Example 6
Source File: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void recordAnnotationExpressions(JvmExecutable executable) {
	List<JvmFormalParameter> parameters = executable.getParameters();
	for(JvmFormalParameter parameter: parameters) {
		recordAnnotationExpressions(parameter);
	}
	recordAnnotationExpressions((JvmAnnotationTarget) executable);
}
 
Example 7
Source File: AbstractPendingLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected String getFeatureParameterTypesAsString(JvmExecutable executable) {
	ITypeReferenceOwner referenceOwner = getState().getReferenceOwner();
	List<JvmFormalParameter> parameters = executable.getParameters();
	StringBuilder b = new StringBuilder();
	b.append("(");
	for(int i=0; i<parameters.size(); ++i) {
		JvmTypeReference parameterType = parameters.get(i).getParameterType();
		LightweightTypeReference typeReference = referenceOwner.toLightweightTypeReference(parameterType);
		b.append(typeReference.getHumanReadableName());
		if(i < parameters.size()-1)
			b.append(", ");
	}
	b.append(")");
	return b.toString();
}
 
Example 8
Source File: AbstractResolvedExecutable.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public List<LightweightTypeReference> getResolvedParameterTypes() {
	JvmExecutable declaration = getDeclaration();
	if (declaration.getParameters().isEmpty())
		return Collections.emptyList();
	if (parameterTypes != null)
		return parameterTypes;
	List<JvmFormalParameter> parameters = declaration.getParameters();
	List<JvmTypeReference> unresolvedParameterTypes = Lists.newArrayListWithCapacity(parameters.size());
	for(JvmFormalParameter parameter: parameters) {
		unresolvedParameterTypes.add(parameter.getParameterType());
	}
	return parameterTypes = getResolvedReferences(unresolvedParameterTypes);
}
 
Example 9
Source File: BucketedEObjectDescription.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void computeShadowingKey(JvmIdentifiableElement identifiable, StringBuilder result) {
	if (identifiable instanceof JvmExecutable) {
		JvmExecutable executable = (JvmExecutable) identifiable;
		result.append('(');
		boolean first = true;
		for(JvmFormalParameter parameter: executable.getParameters()) {
			if (!first) {
				result.append(',');
			} else {
				first = false;
			}
			if (parameter.getParameterType() != null && parameter.getParameterType().getType() != null)
				result.append(parameter.getParameterType().getType().getIdentifier());
			else
				result.append("null");
		}
		result.append(')');
	}
	if (getImplicitFirstArgument() != null) {
		result.append(":implicitFirstArgument");
	}
	if (getImplicitReceiver() != null) {
		result.append(":implicitReceiver");
	}
	if (isTypeLiteral()) {
		result.append(":typeLiteral");
	}
	if (isVisible()) {
		result.append('+');
	} else {
		result.append('-');
	}
}
 
Example 10
Source File: XbaseIdeCrossrefProposalProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendParameters(StringBuilder result, JvmExecutable executable, int insignificantParameters,
		LightweightTypeReferenceFactory ownedConverter) {
	List<JvmFormalParameter> declaredParameters = executable.getParameters();
	List<JvmFormalParameter> relevantParameters = declaredParameters
			.subList(Math.min(insignificantParameters, declaredParameters.size()), declaredParameters.size());
	for (int i = 0; i < relevantParameters.size(); i++) {
		JvmFormalParameter parameter = relevantParameters.get(i);
		if (i != 0) {
			result.append(", ");
		}
		if (i == relevantParameters.size() - 1 && executable.isVarArgs()
				&& parameter.getParameterType() instanceof JvmGenericArrayTypeReference) {
			JvmGenericArrayTypeReference parameterType = (JvmGenericArrayTypeReference) parameter
					.getParameterType();
			result.append(
					ownedConverter.toLightweightReference(parameterType.getComponentType()).getHumanReadableName());
			result.append("...");
		} else {
			if (parameter.getParameterType() != null) {
				String simpleName = ownedConverter.toLightweightReference(parameter.getParameterType())
						.getHumanReadableName();
				if (simpleName != null) {
					result.append(simpleName);
				}
			}
		}
		result.append(" ");
		result.append(String.valueOf(parameter.getName()));
	}
}
 
Example 11
Source File: AbstractTypeProviderPerformanceTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected JvmDeclaredType loadAndResolve(String name, boolean accessMembers, boolean accessAnnotations, boolean accessTypeParams, boolean accessParameter, boolean accessParameterNames) {
	JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(name);
	EcoreUtil.resolveAll(type.eResource());
	EcoreUtil.resolveAll(type.eResource().getResourceSet());
	Assert.assertNotNull(name, type);
	
	if (accessAnnotations) {
		type.getAnnotations();
	}
	
	if (accessMembers) {
		EList<JvmMember> members = type.getMembers();
		for (JvmMember member : members) {
			if (accessAnnotations) {
				member.getAnnotations();
			}
			if (member instanceof JvmExecutable) {
				JvmExecutable operation = (JvmExecutable) member;
				if (accessParameter) {
					EList<JvmFormalParameter> parameters = operation.getParameters();
					for (JvmFormalParameter jvmFormalParameter : parameters) {
						if (accessAnnotations) {
							jvmFormalParameter.getAnnotations();
						}
						if (accessParameterNames) {
							jvmFormalParameter.getName();
						}
					}
				}
			}
		}
	}
	return type;
}
 
Example 12
Source File: JavaReflectAccess.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Class<?>[] getParamTypes(JvmExecutable exe) {
	List<JvmFormalParameter> parameters = exe.getParameters();
	List<Class<?>> result = Lists.newArrayList();
	for (JvmFormalParameter p : parameters) {
		result.add(getRawType(p.getParameterType()));
	}
	return result.toArray(new Class<?>[result.size()]);
}
 
Example 13
Source File: AbstractTypeProviderPerformanceTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected JvmDeclaredType loadAndResolve(String name, boolean accessMembers, boolean accessAnnotations, boolean accessTypeParams, boolean accessParameter, boolean accessParameterNames) {
	JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(name);
	EcoreUtil.resolveAll(type.eResource());
	EcoreUtil.resolveAll(type.eResource().getResourceSet());
	Assert.assertNotNull(name, type);
	
	if (accessAnnotations) {
		type.getAnnotations();
	}
	
	if (accessMembers) {
		EList<JvmMember> members = type.getMembers();
		for (JvmMember member : members) {
			if (accessAnnotations) {
				member.getAnnotations();
			}
			if (member instanceof JvmExecutable) {
				JvmExecutable operation = (JvmExecutable) member;
				if (accessParameter) {
					EList<JvmFormalParameter> parameters = operation.getParameters();
					for (JvmFormalParameter jvmFormalParameter : parameters) {
						if (accessAnnotations) {
							jvmFormalParameter.getAnnotations();
						}
						if (accessParameterNames) {
							jvmFormalParameter.getName();
						}
					}
				}
			}
		}
	}
	return type;
}
 
Example 14
Source File: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void computeAnnotationTypes(ResolvedTypes resolvedTypes, IFeatureScopeSession sessions, JvmExecutable operation) {
	computeAnnotationTypes(resolvedTypes, sessions, (JvmAnnotationTarget) operation);
	for(JvmFormalParameter parameter: operation.getParameters()) {
		computeAnnotationTypes(resolvedTypes, sessions, parameter);
	}
}
 
Example 15
Source File: XbaseHoverDocumentationProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected void handleLink(List<?> fragments) {
	if (fragments == null || fragments.isEmpty()) {
		return;
	}
	URI elementURI = null;
	String firstFragment = fragments.get(0).toString();
	int hashIndex = firstFragment.indexOf("#");
	if (hashIndex != -1) {
		JvmDeclaredType resolvedDeclarator = getResolvedDeclarator(firstFragment.substring(0, hashIndex));
		if (resolvedDeclarator != null && !resolvedDeclarator.eIsProxy()) {
			String signature = firstFragment.substring(hashIndex + 1);
			int indexOfOpenBracket = signature.indexOf("(");
			int indexOfClosingBracket = signature.indexOf(")");
			String simpleNameOfFeature = indexOfOpenBracket != -1 ? signature.substring(0, indexOfOpenBracket)
					: signature;
			Iterable<JvmConstructor> constructorCandidates = new ArrayList<JvmConstructor>();
			if (resolvedDeclarator.getSimpleName().equals(simpleNameOfFeature)) {
				constructorCandidates = resolvedDeclarator.getDeclaredConstructors();
			}
			Iterable<JvmFeature> possibleCandidates = Iterables.concat(
				resolvedDeclarator.findAllFeaturesByName(simpleNameOfFeature),
				constructorCandidates
			);
			List<String> parameterNames = null;
			if (indexOfOpenBracket != -1 && indexOfClosingBracket != -1) {
				parameterNames = Strings.split(signature.substring(indexOfOpenBracket + 1, indexOfClosingBracket),
						",");
			}
			Iterator<JvmFeature> featureIterator = possibleCandidates.iterator();
			while (elementURI == null && featureIterator.hasNext()) {
				JvmFeature feature = featureIterator.next();
				boolean valid = false;
				if (feature instanceof JvmField) {
					valid = true;
				} else if (feature instanceof JvmExecutable) {
					JvmExecutable executable = (JvmExecutable) feature;
					EList<JvmFormalParameter> parameters = executable.getParameters();
					if (parameterNames == null) {
						valid = true;
					} else if (parameters.size() == parameterNames.size()) {
						valid = true;
						for (int i = 0; (i < parameterNames.size() && valid); i++) {
							URI parameterTypeURI = EcoreUtil.getURI(parameters.get(i).getParameterType().getType());
							IEObjectDescription expectedParameter = scopeProvider.getScope(context,
									new HoverReference(TypesPackage.Literals.JVM_TYPE)).getSingleElement(
									qualifiedNameConverter.toQualifiedName(parameterNames.get(i)));
							if (expectedParameter == null
									|| !expectedParameter.getEObjectURI().equals(parameterTypeURI)) {
								valid = false;
							}
						}
					}
				}
				if (valid)
					elementURI = EcoreUtil.getURI(feature);
			}
		}
	} else {
		IScope scope = scopeProvider.getScope(context, new HoverReference(TypesPackage.Literals.JVM_TYPE));
		IEObjectDescription singleElement = scope.getSingleElement(qualifiedNameConverter
				.toQualifiedName(firstFragment));
		if (singleElement != null)
			elementURI = singleElement.getEObjectURI();
	}
	String label = "";
	if (fragments.size() > 1) {
		for (int i = 1; i < fragments.size(); i++) {
			String portentialLabel = fragments.get(i).toString();
			if (portentialLabel.trim().length() > 0)
				label += portentialLabel;
		}
	}
	if (label.length() == 0)
		label = firstFragment;
	if (elementURI == null)
		buffer.append(label);
	else {
		buffer.append(createLinkWithLabel(XtextElementLinks.XTEXTDOC_SCHEME, elementURI, label));
	}
}