Java Code Examples for org.eclipse.xtext.common.types.JvmVisibility#PRIVATE

The following examples show how to use org.eclipse.xtext.common.types.JvmVisibility#PRIVATE . 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: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkLocalUsageOfDeclaredFields(XtendField field){
	if(doCheckValidMemberName(field) && !isIgnored(UNUSED_PRIVATE_MEMBER)) {
		JvmField jvmField = associations.getJvmField(field);
		if (jvmField == null || jvmField.getVisibility() != JvmVisibility.PRIVATE || jvmField.eContainer() == null)
			return;
		if (isLocallyUsed(jvmField, getOutermostType(field))) 
			return;
		String message;
		if(field.isExtension()) {
			if(field.getName() == null && jvmField.getType() != null)
				message = "The extension " + jvmField.getType().getIdentifier() 
					+ " is not used in " + getDeclaratorName(jvmField);
			else
				message = "The extension " + getDeclaratorName(jvmField) + "."
						+ jvmField.getSimpleName() + " is not used";
		} else {
			message = "The value of the field " + getDeclaratorName(jvmField) + "."
				+ jvmField.getSimpleName() + " is not used";
		}
		addIssueToState(UNUSED_PRIVATE_MEMBER, message, XtendPackage.Literals.XTEND_FIELD__NAME);
	}
}
 
Example 2
Source File: SARLJvmModelInferrer.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
protected JvmOperation deriveGenericDispatchOperationSignature(
		Iterable<JvmOperation> localOperations, JvmGenericType target) {
	final JvmOperation dispatcher = super.deriveGenericDispatchOperationSignature(localOperations, target);
	//
	// Fixing the behavior for determining the visibility of the dispatcher since
	// it does not fit the SARL requirements.
	//
	JvmVisibility higherVisibility = JvmVisibility.PRIVATE;
	for (final JvmOperation jvmOperation : localOperations) {
		final Iterable<XtendFunction> xtendFunctions = Iterables.filter(
				this.sarlAssociations.getSourceElements(jvmOperation), XtendFunction.class);
		for (final XtendFunction func : xtendFunctions) {
			JvmVisibility visibility = func.getVisibility();
			if (visibility == null) {
				visibility = this.defaultVisibilityProvider.getDefaultJvmVisibility(func);
			}
			if (this.visibilityComparator.compare(visibility, higherVisibility) > 0) {
				higherVisibility = visibility;
			}
		}
	}
	dispatcher.setVisibility(higherVisibility);

	return dispatcher;
}
 
Example 3
Source File: JvmMemberDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public void setVisibility(final Visibility visibility) {
  this.checkMutable();
  T _delegate = this.getDelegate();
  JvmVisibility _switchResult = null;
  if (visibility != null) {
    switch (visibility) {
      case DEFAULT:
        _switchResult = JvmVisibility.DEFAULT;
        break;
      case PUBLIC:
        _switchResult = JvmVisibility.PUBLIC;
        break;
      case PRIVATE:
        _switchResult = JvmVisibility.PRIVATE;
        break;
      case PROTECTED:
        _switchResult = JvmVisibility.PROTECTED;
        break;
      default:
        break;
    }
  }
  _delegate.setVisibility(_switchResult);
}
 
Example 4
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isMorePrivateThan(JvmVisibility o1, JvmVisibility o2) {
	if (o1 == o2) {
		return false;
	} else {
		switch (o1) {
			case DEFAULT:
				return o2 != JvmVisibility.PRIVATE;
			case PRIVATE:
				return true;
			case PROTECTED:
				return o2 == JvmVisibility.PUBLIC;
			case PUBLIC:
				return false;
			default:
				throw new IllegalArgumentException("Unknown JvmVisibility " + o1);
		}
	}
}
 
Example 5
Source File: JvmDeclaredTypeSignatureHashProvider.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public SignatureHashBuilder appendSignature(JvmDeclaredType type) {
	if (type.getVisibility() != JvmVisibility.PRIVATE) {
		appendAnnotationReferences(type);
		appendVisibility(type.getVisibility()).append(" ");
		if (type.isAbstract())
			append("abstract ");
		if (type.isStatic())
			append("static ");
		if (type.isFinal())
			append("final ");
		append("class ").append(type.getIdentifier());
		if (type instanceof JvmTypeParameterDeclarator)
			appendTypeParameters((JvmTypeParameterDeclarator) type);
		append("\n").appendSuperTypeSignatures(type).appendMemberSignatures(type, false);
	}
	return this;
}
 
Example 6
Source File: OverrideTester.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isMorePrivateThan(JvmVisibility o1, JvmVisibility o2) {
	if (o1 == o2) {
		return false;
	} else {
		switch (o1) {
			case DEFAULT:
				return o2 != JvmVisibility.PRIVATE;
			case PRIVATE:
				return true;
			case PROTECTED:
				return o2 == JvmVisibility.PUBLIC;
			case PUBLIC:
				return false;
			default:
				throw new IllegalArgumentException("Unknown JvmVisibility " + o1);
		}
	}
}
 
Example 7
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 8
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 9
Source File: ExtractMethodUserInputPage.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void createAccessModifierSection(Composite result) {
	GridLayout layout;
	Label label = new Label(result, SWT.NONE);
	label.setText("Access modifier:");
	Composite group = new Composite(result, SWT.NONE);
	group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	layout = new GridLayout();
	layout.numColumns = 3;
	layout.marginWidth = 0;
	group.setLayout(layout);
	String[] labels = new String[] { "public", "protected", "private" };
	JvmVisibility[] data = new JvmVisibility[] { JvmVisibility.PUBLIC, JvmVisibility.PROTECTED,
			JvmVisibility.PRIVATE };
	JvmVisibility visibility = refactoring.getVisibility();
	for (int i = 0; i < labels.length; i++) {
		Button radio = new Button(group, SWT.RADIO);
		radio.setText(labels[i]);
		radio.setData(data[i]);
		if (data[i].equals(visibility))
			radio.setSelection(true);
		radio.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				final JvmVisibility selectedModifier = (JvmVisibility) event.widget.getData();
				visibilityModified(selectedModifier);
				updatePreview();
			}
		});
	}
}
 
Example 10
Source File: AbstractResolvedOperation.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IResolvedOperation getOverriddenMethod() {
	if (!getDeclaration().isAbstract() && getDeclaration().getVisibility() != JvmVisibility.PRIVATE) {
		List<IResolvedOperation> overriddenAndImplemented = getOverriddenAndImplementedMethods();
		for(IResolvedOperation candidate: overriddenAndImplemented) {
			if (!candidate.getDeclaration().isAbstract()) {
				return candidate;
			}
		}
	}
	return null;
}
 
Example 11
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isLocallyUsed(EObject target, EObject containerToFindUsage) {
	if (readAndWriteTracking.isRead(target)) {
		return true;
	}
	Collection<Setting> usages = XbaseUsageCrossReferencer.find(target, containerToFindUsage);
	// field and local variables are used when they are not used as the left operand of an assignment operator.
	if (target instanceof XVariableDeclaration || target instanceof JvmField) {
		for (final Setting usage : usages) {
			final EObject object = usage.getEObject();
			if (object instanceof XAssignment) {
				final XAssignment assignment = (XAssignment) object;
				if (assignment.getFeature() != target) {
					return true;
				}
			} else {
				return true;
			}
		}
		return false;
	}
	// for non-private members it is enough to check that there are usages
	if (!(target instanceof JvmOperation) || ((JvmOperation)target).getVisibility()!=JvmVisibility.PRIVATE) {
		return !usages.isEmpty();
	} else {
		// for private members it has to be checked if all usages are within the operation
		EObject targetSourceElem = associations.getPrimarySourceElement(target);
		for (Setting s : usages) {
			if (s.getEObject() instanceof XAbstractFeatureCall) {
				XAbstractFeatureCall fc = (XAbstractFeatureCall) s.getEObject();
				// when the feature call does not call itself or the call is
				// from another function, then it is locally used
				if (fc.getFeature() != target || !EcoreUtil.isAncestor(targetSourceElem, fc))
					return true;
			} else {
				return true;
			}
		}
		return false;
	}
}
 
Example 12
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 13
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 14
Source File: XtendFieldImplCustom.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected JvmVisibility getDefaultVisibility() {
	if(getDeclaringType() instanceof XtendInterface || getDeclaringType() instanceof XtendAnnotationType)
		return JvmVisibility.PUBLIC;
	else 
		return JvmVisibility.PRIVATE;
}
 
Example 15
Source File: XtendMemberImplCustom.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public JvmVisibility getDeclaredVisibility() {
	for(String modifier: getModifiers()) {
		if(equal(modifier, "public")) 
			return JvmVisibility.PUBLIC;
		if(equal(modifier, "package")) 
			return JvmVisibility.DEFAULT;
		if(equal(modifier, "protected")) 
			return JvmVisibility.PROTECTED;
		if(equal(modifier, "private")) 
			return JvmVisibility.PRIVATE;
	}
	return null;
}
 
Example 16
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 17
Source File: IDefaultVisibilityProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the default visibility of a field when inside the given container.
 *
 * @param container the container.
 * @return the default visibility.
 * @since 0.6
 */
static JvmVisibility getFieldDefaultVisibilityIn(EObject container) {
	if (container instanceof SarlEvent
			|| container instanceof XtendInterface
			|| container instanceof XtendAnnotationType) {
		return JvmVisibility.PUBLIC;
	}
	return JvmVisibility.PRIVATE;
}
 
Example 18
Source File: SARLValidator.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("synthetic-access")
protected boolean isPrivateByDefault(XtendMember member) {
	final JvmVisibility defaultVisibility = SARLValidator.this.defaultVisibilityProvider.getDefaultJvmVisibility(member);
	return defaultVisibility == JvmVisibility.PRIVATE;
}
 
Example 19
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;
}
 
Example 20
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;
}