Java Code Examples for org.eclipse.xtext.common.types.JvmOperation#getVisibility()

The following examples show how to use org.eclipse.xtext.common.types.JvmOperation#getVisibility() . 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: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns <code>null</code> if the given operation declares it's own return type or if it does not override
 * another operation.
 */
/* @Nullable */
@SuppressWarnings("unused")
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);
		}
		LightweightTypeReference result = overrideHelper.getReturnTypeOfOverriddenOperation(operation, declaringType);
		return result;
	}
	return null;
}
 
Example 2
Source File: OverrideHelper.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public JvmOperation findOverriddenOperation(JvmOperation operation) {
	if (operation.getVisibility() == JvmVisibility.PRIVATE) {
		return null;
	}
	ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, operation.eResource().getResourceSet());
	LightweightTypeReference declaringType = owner.toLightweightTypeReference(operation.getDeclaringType());
	TypeParameterSubstitutor<?> substitutor = createSubstitutor(owner, declaringType);
	return findOverriddenOperation(operation, declaringType, substitutor, owner, new ContextualVisibilityHelper(visibilityHelper, declaringType));
}
 
Example 3
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendOperationVisibility(final ITreeAppendable b, JvmOperation operation) {
	b.newLine();
	JvmDeclaredType declaringType = operation.getDeclaringType();
	GeneratorConfig config = b.getGeneratorConfig();
	if (config != null && config.getJavaSourceVersion().isAtLeast(JAVA6)
			|| declaringType instanceof JvmGenericType && !((JvmGenericType) declaringType).isInterface()) {
		b.append("@").append(Override.class).newLine();
	}
	switch(operation.getVisibility()) {
		case DEFAULT: break;
		case PUBLIC: b.append("public "); return;
		case PROTECTED: b.append("protected "); return;
		case PRIVATE: b.append("private "); return;
	}
}
 
Example 4
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkLocalUsageOfDeclaredXtendFunction(XtendFunction function){
	if(doCheckValidMemberName(function) && !isIgnored(UNUSED_PRIVATE_MEMBER)) {
		JvmOperation jvmOperation = function.isDispatch()?associations.getDispatchOperation(function):associations.getDirectlyInferredOperation(function);
		if(jvmOperation != null && jvmOperation.getVisibility() == JvmVisibility.PRIVATE && !isLocallyUsed(jvmOperation, getOutermostType(function))) {
			String message = "The method " + jvmOperation.getSimpleName() 
					+  uiStrings.parameters(jvmOperation)  
					+ " from the type "+ getDeclaratorName(jvmOperation)+" is never used locally.";
			addIssueToState(UNUSED_PRIVATE_MEMBER, message, XtendPackage.Literals.XTEND_FUNCTION__NAME);
		}
	}
}
 
Example 5
Source File: XtendReentrantTypeResolver.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@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());
		// guard against active annotations that put an expression into a second method
		// namely in a synthesized super type - in that case, the expression should not be
		// inferred in the context of the super type but the subtype thus the return type
		// of a super method has to be ignored
		
		// 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;
}
 
Example 6
Source File: SARLValidator.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Check
@Override
public void checkLocalUsageOfDeclaredXtendFunction(XtendFunction function) {
	if (doCheckValidMemberName(function) && !isIgnored(UNUSED_PRIVATE_MEMBER)) {
		final JvmOperation mainOperation;
		if (function.isDispatch()) {
			mainOperation = this.associations.getDispatchOperation(function);
		} else {
			mainOperation = this.associations.getDirectlyInferredOperation(function);
		}
		if (mainOperation != null && mainOperation.getVisibility() == JvmVisibility.PRIVATE) {
			final EObject outerType = getOutermostType(function);
			boolean isUsed = isLocallyUsed(mainOperation, outerType);
			if (!isUsed && isDefaultValuedParameterFunction(function)) {
				for (final EObject jvmElement : this.associations.getJvmElements(function)) {
					if (jvmElement != mainOperation && jvmElement instanceof JvmOperation
							&& isLocallyUsed(jvmElement, outerType)) {
						isUsed = true;
						// break the loop
						break;
					}
				}
			}
			if (!isUsed) {
				final String message = MessageFormat.format(Messages.SARLValidator_94,
						mainOperation.getSimpleName(), this.uiStrings.parameters(mainOperation),
						getDeclaratorName(mainOperation));
				addIssueToState(UNUSED_PRIVATE_MEMBER, message, XtendPackage.Literals.XTEND_FUNCTION__NAME);
			}
		}
	}
}
 
Example 7
Source File: OverrideHelper.java    From xtext-extras with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Returns <code>null</code> if the given operation declares it's own return type or if it does not override
 * another operation.
 * 
 * TODO support this case:
 * 
 * <pre>
 * interface I {
 *   String m()
 *   String m2()
 * }
 * class A {
 *   CharSequence m()
 *   int m2()
 * }
 * class B extends A implements I {
 *   m() will expect String since this is the best choice
 *   m2() will expect int since this is actually overridden and not compatible to String from I#m2
 * }
 * </pre>
 */
/* @Nullable */
public LightweightTypeReference getReturnTypeOfOverriddenOperation(JvmOperation operation, ITypeReferenceOwner owner, IVisibilityHelper visibilityHelper) {
	if (operation.getVisibility() == JvmVisibility.PRIVATE || !InferredTypeIndicator.isInferred(operation.getReturnType())) {
		return null;
	}
	LightweightTypeReference declaringType = owner.newParameterizedTypeReference(operation.getDeclaringType());
	TypeParameterSubstitutor<?> substitutor = createSubstitutor(owner, declaringType);
	JvmOperation overriddenOperation = findOverriddenOperation(operation, declaringType, substitutor, owner, visibilityHelper);
	if (overriddenOperation != null) {
		return substitutor.substitute(owner.toLightweightTypeReference(overriddenOperation.getReturnType()));
	}
	return null;
}
 
Example 8
Source File: OverrideHelper.java    From xtext-extras with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Returns <code>null</code> if the given operation declares it's own return type or if it does not override
 * another operation.
 * 
 * TODO support this case:
 * 
 * <pre>
 * interface I {
 *   String m()
 *   String m2()
 * }
 * class A {
 *   CharSequence m()
 *   int m2()
 * }
 * class B extends A implements I {
 *   m() will expect String since this is the best choice
 *   m2() will expect int since this is actually overridden and not compatible to String from I#m2
 * }
 * </pre>
 */
/* @Nullable */
public LightweightTypeReference getReturnTypeOfOverriddenOperation(JvmOperation operation, LightweightTypeReference context) {
	if (operation.getVisibility() == JvmVisibility.PRIVATE || !InferredTypeIndicator.isInferred(operation.getReturnType())) {
		return null;
	}
	BottomResolvedOperation resolvedOperation = new BottomResolvedOperation(operation, context, overrideTester);
	List<IResolvedOperation> overriddenMethods = resolvedOperation.getOverriddenAndImplementedMethods();
	if (overriddenMethods.isEmpty())
		return null;
	LightweightTypeReference result = overriddenMethods.get(0).getResolvedReturnType();
	return result;
}