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

The following examples show how to use org.eclipse.xtext.common.types.JvmVisibility#PROTECTED . 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: ExtractMethodRefactoring.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public boolean initialize(XtextEditor editor, List<XExpression> selectedExpressions, boolean doSave) {
	if(selectedExpressions.isEmpty() || editor.getDocument() == null)
		return false;
	this.document = editor.getDocument();
	this.doSave = doSave;
	this.editor = editor;
	this.expressions = calculateExpressions(selectedExpressions);
	this.firstExpression = this.expressions.get(0);
	this.originalMethod = EcoreUtil2.getContainerOfType(firstExpression, XtendFunction.class);
	this.lastExpression = this.expressions.get(this.expressions.size()-1);
	this.resourceURI = EcoreUtil2.getPlatformResourceOrNormalizedURI(firstExpression).trimFragment();
	this.xtendClass = EcoreUtil2.getContainerOfType(firstExpression, XtendClass.class);
	if (xtendClass == null || originalMethod == null)
		return false;
	this.visibility = JvmVisibility.PROTECTED;
	this.isStatic = originalMethod.isStatic();
	this.isExplicitlyDeclareReturnType = true;
	XExpression successorExpression = expressionUtil
			.findSuccessorExpressionForVariableDeclaration(lastExpression);
	nameUtil.setFeatureScopeContext(successorExpression);
	rewriter = rewriterFactory.create(document, (XtextResource) firstExpression.eResource());
	return true;
}
 
Example 2
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 3
Source File: ContextualVisibilityHelper.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean isVisible(JvmMember member) {
	// TODO private visibility?
	JvmVisibility visibility = member.getVisibility();
	if (visibility == JvmVisibility.PUBLIC) {
		return true;
	}
	JvmDeclaredType type = member instanceof JvmDeclaredType ? (JvmDeclaredType) member : member.getDeclaringType();
	if (type == rawContextType || EcoreUtil.isAncestor(rawContextType, type)) {
		return true;
	}
	if (type != null && visibility == JvmVisibility.PROTECTED) {
		if (superTypeNames == null) {
			this.superTypeNames = computeSuperTypeNames();
		}
		if (superTypeNames.contains(type.getIdentifier())) {
			return true;
		}
		if (type == member) {
			JvmDeclaredType declaringType = member.getDeclaringType();
			if (declaringType != null && superTypeNames.contains(declaringType.getIdentifier())) {
				return true;
			}
		}
	}
	if (type != null 
			&& (rawContextType == null || rawContextType instanceof JvmDeclaredType) 
			&& (visibility == JvmVisibility.DEFAULT || visibility == JvmVisibility.PROTECTED)) {
		if (Strings.isEmpty(packageName) && Strings.isEmpty(type.getPackageName())
				|| (packageName != null && packageName.equals(type.getPackageName()))) {
			return true;
		}
	}
	return parent.isVisible(member);
}
 
Example 4
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 5
Source File: InferredJvmModelTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testDispatchFunction_05() throws Exception {
	XtendFile xtendFile = file("class Foo {" + 
			"  def private dispatch private_private  (Integer x) {} def private   dispatch private_private  (Double x) {}" +
			"  def private dispatch private_protected(Integer x) {} def protected dispatch private_protected(Double x) {}" +
			"  def private dispatch private_public   (Integer x) {} def public    dispatch private_public   (Double x) {}" +
			"  def private dispatch private_default  (Integer x) {} def           dispatch private_default  (Double x) {}" +
			"  def protected dispatch protected_private  (Integer x) {} def private   dispatch protected_private  (Double x) {}" +
			"  def protected dispatch protected_protected(Integer x) {} def protected dispatch protected_protected(Double x) {}" +
			"  def protected dispatch protected_public   (Integer x) {} def public    dispatch protected_public   (Double x) {}" +
			"  def protected dispatch protected_default  (Integer x) {} def           dispatch protected_default  (Double x) {}" +
			"  def public dispatch public_private  (Integer x) {} def private   dispatch public_private  (Double x) {}" +
			"  def public dispatch public_protected(Integer x) {} def protected dispatch public_protected(Double x) {}" +
			"  def public dispatch public_public   (Integer x) {} def public    dispatch public_public   (Double x) {}" +
			"  def public dispatch public_default  (Integer x) {} def           dispatch public_default  (Double x) {}" +
			"  def dispatch default_private  (Integer x) {} def private   dispatch default_private  (Double x) {}" +
			"  def dispatch default_protected(Integer x) {} def protected dispatch default_protected(Double x) {}" +
			"  def dispatch default_public   (Integer x) {} def public    dispatch default_public   (Double x) {}" +
			"  def dispatch default_default  (Integer x) {} def           dispatch default_default  (Double x) {}" +
			"}");
	JvmGenericType inferredType = getInferredType(xtendFile);
	for(JvmOperation op: inferredType.getDeclaredOperations()) {
		String[] split = op.getSimpleName().toUpperCase().split("_");
		JvmVisibility expectedVisibility = null;
		if(!op.getSimpleName().startsWith("_")) {
			if(equal(split[0], split[1]))
				expectedVisibility = "DEFAULT".equals(split[0]) ? JvmVisibility.PUBLIC : JvmVisibility.get(split[0]);
			else 	
				expectedVisibility = JvmVisibility.PUBLIC;
		}
		else if(equal(op.getParameters().get(0).getParameterType().getIdentifier(), "java.lang.Integer"))
			expectedVisibility = "DEFAULT".equals(split[1]) ? JvmVisibility.PROTECTED : JvmVisibility.get(split[1]);
		else
			expectedVisibility = "DEFAULT".equals(split[2]) ? JvmVisibility.PROTECTED : JvmVisibility.get(split[2]);
		assertEquals(op.getIdentifier(), expectedVisibility, op.getVisibility());
	}
}
 
Example 6
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isApi(XtendTypeDeclaration type) {
	if (type.isAnonymous())
		return false;
	boolean api = type.getVisibility() == JvmVisibility.PUBLIC;
	if (type.getDeclaringType() != null) {
		api = api || (type.getVisibility() == JvmVisibility.PROTECTED && ! type.getDeclaringType().isFinal()); 
		api = api && isApi(type.getDeclaringType());
	}
	return api;
}
 
Example 7
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 8
Source File: IDefaultVisibilityProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the default visibility of an action when inside the given container.
 *
 * @param container the container.
 * @return the default visibility.
 * @since 0.6
 */
static JvmVisibility getActionDefaultVisibilityIn(EObject container) {
	if (container instanceof SarlAgent) {
		return JvmVisibility.PROTECTED;
	}
	return JvmVisibility.PUBLIC;
}
 
Example 9
Source File: IDefaultVisibilityProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the default visibility of an annotation type when inside the given container.
 *
 * @param container the container.
 * @return the default visibility.
 * @since 0.6
 */
static JvmVisibility getAnnotationTypeDefaultVisibilityIn(EObject container) {
	if (container instanceof SarlAgent) {
		return JvmVisibility.PROTECTED;
	}
	return JvmVisibility.PUBLIC;
}
 
Example 10
Source File: IDefaultVisibilityProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the default visibility of a class when inside the given container.
 *
 * @param container the container.
 * @return the default visibility.
 * @since 0.6
 */
static JvmVisibility getClassDefaultVisibilityIn(EObject container) {
	if (container instanceof SarlAgent) {
		return JvmVisibility.PROTECTED;
	}
	return JvmVisibility.PUBLIC;
}
 
Example 11
Source File: IDefaultVisibilityProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the default visibility of an enumeration when inside the given container.
 *
 * @param container the container.
 * @return the default visibility.
 * @since 0.6
 */
static JvmVisibility getEnumerationDefaultVisibilityIn(EObject container) {
	if (container instanceof SarlAgent) {
		return JvmVisibility.PROTECTED;
	}
	return JvmVisibility.PUBLIC;
}
 
Example 12
Source File: IDefaultVisibilityProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the default visibility of an interface when inside the given container.
 *
 * @param container the container.
 * @return the default visibility.
 * @since 0.6
 */
static JvmVisibility getInterfaceDefaultVisibilityIn(EObject container) {
	if (container instanceof SarlAgent) {
		return JvmVisibility.PROTECTED;
	}
	return JvmVisibility.PUBLIC;
}
 
Example 13
Source File: FeatureScopeSessionWithContext.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean isVisible(JvmMember member, /* @Nullable */ LightweightTypeReference receiverType, /* @Nullable */ JvmIdentifiableElement receiverFeature) {
	boolean result = isVisible(member);
	if (result && JvmVisibility.PROTECTED == member.getVisibility()) {
		if (receiverFeature != null) {
			// We bypass this check for qualified.this and qualified.super in the scope provider
			// they are considered to be always visible
			/*
			 * class A {
			 *   class B {
			 *     {
			 *       A.super.toString
			 *     }
			 *   }
			 * }
			 */
			if (isThisSuperOrTypeLiteral(receiverFeature)) {
				if (receiverType == null || !receiverType.isType(Class.class)) {
					return true;
				}
			}
		}
		JvmType contextType = visibilityHelper.getRawContextType();
		if (contextType instanceof JvmDeclaredType) {
			String packageName = visibilityHelper.getPackageName();
			JvmDeclaredType declaringType = member.getDeclaringType();
			String memberPackageName = declaringType.getPackageName();
			if (Strings.equal(packageName, memberPackageName)) {
				return true;
			}
		}
		if (receiverType != null) {
			if (receiverType.isSubtypeOf(contextType)) {
				return true;
			}
			EObject container = contextType.eContainer();
			while (container instanceof JvmType) {
				if (receiverType.isSubtypeOf((JvmType)container)) {
					return true;
				}
				container = container.eContainer();
			}
		}
		return false;
	}
	return result;
}
 
Example 14
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean isApi(XtendMember member) {
	if (!isApi(member.getDeclaringType())) return false;
	return member.getVisibility() == JvmVisibility.PUBLIC 
			|| member.getVisibility() == JvmVisibility.PROTECTED && !member.getDeclaringType().isFinal();
}
 
Example 15
Source File: XtendJvmModelInferrer.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected void transform(XtendFunction source, JvmGenericType container, boolean allowDispatch) {
	JvmOperation operation = typesFactory.createJvmOperation();
	operation.setAbstract(source.isAbstract());
	operation.setNative(source.isNative());
	operation.setSynchronized(source.isSynchonized());
	operation.setStrictFloatingPoint(source.isStrictFloatingPoint());
	if (!source.isAbstract())
		operation.setFinal(source.isFinal());
	container.getMembers().add(operation);
	associator.associatePrimary(source, operation);
	String sourceName = source.getName();
	JvmVisibility visibility = source.getVisibility();
	if (allowDispatch && source.isDispatch()) {
		if (source.getDeclaredVisibility() == null)
			visibility = JvmVisibility.PROTECTED;
		sourceName = "_" + sourceName;
	}
	operation.setSimpleName(sourceName);
	operation.setVisibility(visibility);
	operation.setStatic(source.isStatic());
	if (!operation.isAbstract() && !operation.isStatic() && container.isInterface())
		operation.setDefault(true);
	for (XtendParameter parameter : source.getParameters()) {
		translateParameter(operation, parameter);
	}
	XExpression expression = source.getExpression();
	CreateExtensionInfo createExtensionInfo = source.getCreateExtensionInfo();
	
	JvmTypeReference returnType = null;
	if (source.getReturnType() != null) {
		returnType = jvmTypesBuilder.cloneWithProxies(source.getReturnType());
	} else if (createExtensionInfo != null) {
		returnType = jvmTypesBuilder.inferredType(createExtensionInfo.getCreateExpression());
	} else if (expression != null) {
		returnType = jvmTypesBuilder.inferredType(expression);
	} else {
		returnType = jvmTypesBuilder.inferredType();
	}
	
	operation.setReturnType(returnType);
	copyAndFixTypeParameters(source.getTypeParameters(), operation);
	for (JvmTypeReference exception : source.getExceptions()) {
		operation.getExceptions().add(jvmTypesBuilder.cloneWithProxies(exception));
	}
	translateAnnotationsTo(source.getAnnotations(), operation);
	if (source.isOverride() && typeReferences.findDeclaredType(Override.class, source) != null)
		setOverride(operation);
	if (createExtensionInfo != null) {
		transformCreateExtension(source, createExtensionInfo, container, operation, returnType);
	} else {
		setBody(operation, expression);
	}
	jvmTypesBuilder.copyDocumentationTo(source, operation);
}
 
Example 16
Source File: SARLValidator.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("synthetic-access")
protected boolean isProtectedByDefault(XtendMember member) {
	final JvmVisibility defaultVisibility = SARLValidator.this.defaultVisibilityProvider.getDefaultJvmVisibility(member);
	return defaultVisibility == JvmVisibility.PROTECTED;
}