Java Code Examples for org.eclipse.jdt.core.dom.Modifier#PROTECTED

The following examples show how to use org.eclipse.jdt.core.dom.Modifier#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: UMLModelASTReader.java    From RefactoringMiner with MIT License 6 votes vote down vote up
private void processModifiers(CompilationUnit cu, String sourceFile, AbstractTypeDeclaration typeDeclaration, UMLClass umlClass) {
	int modifiers = typeDeclaration.getModifiers();
   	if((modifiers & Modifier.ABSTRACT) != 0)
   		umlClass.setAbstract(true);
   	
   	if((modifiers & Modifier.PUBLIC) != 0)
   		umlClass.setVisibility("public");
   	else if((modifiers & Modifier.PROTECTED) != 0)
   		umlClass.setVisibility("protected");
   	else if((modifiers & Modifier.PRIVATE) != 0)
   		umlClass.setVisibility("private");
   	else
   		umlClass.setVisibility("package");
   	
   	List<IExtendedModifier> extendedModifiers = typeDeclaration.modifiers();
	for(IExtendedModifier extendedModifier : extendedModifiers) {
		if(extendedModifier.isAnnotation()) {
			Annotation annotation = (Annotation)extendedModifier;
			umlClass.addAnnotation(new UMLAnnotation(cu, sourceFile, annotation));
		}
	}
}
 
Example 2
Source File: JdtFlags.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Compares two visibilities.
 *
 * @param newVisibility the 'new' visibility
 * @param oldVisibility the 'old' visibility
 * @return <code>true</code> iff the 'new' visibility is strictly higher than the old visibility
 *
 * @see Modifier#PUBLIC
 * @see Modifier#PROTECTED
 * @see Modifier#NONE
 * @see Modifier#PRIVATE
 */
public static boolean isHigherVisibility(int newVisibility, int oldVisibility){
	assertVisibility(oldVisibility);
	assertVisibility(newVisibility);
	switch (oldVisibility) {
		case Modifier.PRIVATE :
			return 	newVisibility == Modifier.NONE
					||	newVisibility == Modifier.PUBLIC
					||  newVisibility == Modifier.PROTECTED;
		case Modifier.NONE :
			return 	newVisibility == Modifier.PUBLIC
					||  newVisibility == Modifier.PROTECTED;

		case Modifier.PROTECTED :
			return newVisibility == Modifier.PUBLIC;

		case Modifier.PUBLIC :
			return false;
		default:
			Assert.isTrue(false);
			return false;
	}
}
 
Example 3
Source File: PullUpRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int getModifiersWithUpdatedVisibility(final IMember member, final int modifiers, final Map<IMember, IncomingMemberVisibilityAdjustment> adjustments, final IProgressMonitor monitor, final boolean considerReferences, final RefactoringStatus status) throws JavaModelException {
	if (needsVisibilityAdjustment(member, considerReferences, monitor, status)) {
		final MemberVisibilityAdjustor.OutgoingMemberVisibilityAdjustment adjustment= new MemberVisibilityAdjustor.OutgoingMemberVisibilityAdjustment(member, Modifier.ModifierKeyword.PROTECTED_KEYWORD, RefactoringStatus.createWarningStatus(Messages.format(MemberVisibilityAdjustor.getMessage(member), new String[] { MemberVisibilityAdjustor.getLabel(member), MemberVisibilityAdjustor.getLabel(Modifier.ModifierKeyword.PROTECTED_KEYWORD)})));
		adjustment.setNeedsRewriting(false);
		adjustments.put(member, adjustment);
		return JdtFlags.clearAccessModifiers(modifiers) | Modifier.PROTECTED;
	}
	if (getDestinationType().isInterface()) {
		final int flags= JdtFlags.clearAccessModifiers(modifiers) | Modifier.PUBLIC;
		if (member instanceof IMethod)
			return JdtFlags.clearFlag(Modifier.STATIC, flags);
		return flags;
	}
	return modifiers;
}
 
Example 4
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public int[] getAvailableVisibilities() throws JavaModelException{
	if (fTopMethod.getDeclaringType().isInterface())
		return new int[]{Modifier.PUBLIC};
	else if (fTopMethod.getDeclaringType().isEnum() && fTopMethod.isConstructor())
		return new int[]{	Modifier.NONE,
							Modifier.PRIVATE};
	else
		return new int[]{	Modifier.PUBLIC,
							Modifier.PROTECTED,
							Modifier.NONE,
							Modifier.PRIVATE};
}
 
Example 5
Source File: MoveStaticMembersProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isVisibleFrom(IType newMemberDeclaringType, IType accessingType) throws JavaModelException {
	int memberVisibility= JdtFlags.getVisibilityCode(newMemberDeclaringType);

	IType declaringType= newMemberDeclaringType.getDeclaringType();
	while (declaringType != null) { //get lowest visibility in all parent types of newMemberDeclaringType
		memberVisibility= JdtFlags.getLowerVisibility(
				memberVisibility, JdtFlags.getVisibilityCode(declaringType));
		declaringType= declaringType.getDeclaringType();
	}

	switch (memberVisibility) {
		case Modifier.PRIVATE :
			return isEqualOrEnclosedType(accessingType, newMemberDeclaringType);

		case Modifier.NONE :
			return JavaModelUtil.isSamePackage(accessingType.getPackageFragment(), newMemberDeclaringType.getPackageFragment());

		case Modifier.PROTECTED :
			return JavaModelUtil.isSamePackage(accessingType.getPackageFragment(), newMemberDeclaringType.getPackageFragment())
					|| accessingType.newSupertypeHierarchy(null).contains(newMemberDeclaringType);

		case Modifier.PUBLIC :
			return true;

		default:
			Assert.isTrue(false);
			return false;
	}
}
 
Example 6
Source File: SelfEncapsulateFieldRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int createModifiers() throws JavaModelException {
	int result= 0;
	if (Flags.isPublic(fVisibility))
		result |= Modifier.PUBLIC;
	else if (Flags.isProtected(fVisibility))
		result |= Modifier.PROTECTED;
	else if (Flags.isPrivate(fVisibility))
		result |= Modifier.PRIVATE;
	if (JdtFlags.isStatic(fField))
		result |= Modifier.STATIC;
	return result;
}
 
Example 7
Source File: PushDownRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
int getNewModifiersForCopyInSubclass(int oldModifiers) throws JavaModelException {
	if (isFieldInfo())
		return oldModifiers;
	if (isToBeDeletedFromDeclaringClass())
		return oldModifiers;
	int modifiers= oldModifiers;
	if (isNewMethodToBeDeclaredAbstract()) {
		if (!JdtFlags.isPublic(fMember))
			modifiers= Modifier.PROTECTED | JdtFlags.clearAccessModifiers(modifiers);
	}
	return modifiers;
}
 
Example 8
Source File: UMLModelASTReader.java    From RefactoringMiner with MIT License 5 votes vote down vote up
private List<UMLAttribute> processFieldDeclaration(CompilationUnit cu, FieldDeclaration fieldDeclaration, boolean isInterfaceField, String sourceFile) {
	UMLJavadoc javadoc = generateJavadoc(fieldDeclaration);
	List<UMLAttribute> attributes = new ArrayList<UMLAttribute>();
	Type fieldType = fieldDeclaration.getType();
	List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
	for(VariableDeclarationFragment fragment : fragments) {
		UMLType type = UMLType.extractTypeObject(cu, sourceFile, fieldType, fragment.getExtraDimensions());
		String fieldName = fragment.getName().getFullyQualifiedName();
		LocationInfo locationInfo = generateLocationInfo(cu, sourceFile, fragment, CodeElementType.FIELD_DECLARATION);
		UMLAttribute umlAttribute = new UMLAttribute(fieldName, type, locationInfo);
		VariableDeclaration variableDeclaration = new VariableDeclaration(cu, sourceFile, fragment);
		variableDeclaration.setAttribute(true);
		umlAttribute.setVariableDeclaration(variableDeclaration);
		umlAttribute.setJavadoc(javadoc);
		
		int fieldModifiers = fieldDeclaration.getModifiers();
		if((fieldModifiers & Modifier.PUBLIC) != 0)
			umlAttribute.setVisibility("public");
		else if((fieldModifiers & Modifier.PROTECTED) != 0)
			umlAttribute.setVisibility("protected");
		else if((fieldModifiers & Modifier.PRIVATE) != 0)
			umlAttribute.setVisibility("private");
		else if(isInterfaceField)
			umlAttribute.setVisibility("public");
		else
			umlAttribute.setVisibility("package");
		
		if((fieldModifiers & Modifier.FINAL) != 0)
			umlAttribute.setFinal(true);
		
		if((fieldModifiers & Modifier.STATIC) != 0)
			umlAttribute.setStatic(true);
		
		attributes.add(umlAttribute);
	}
	return attributes;
}
 
Example 9
Source File: NewVariableCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private int evaluateFieldModifiers(ASTNode newTypeDecl) {
	if (fSenderBinding.isAnnotation()) {
		return 0;
	}
	if (fSenderBinding.isInterface()) {
		// for interface members copy the modifiers from an existing field
		FieldDeclaration[] fieldDecls= ((TypeDeclaration) newTypeDecl).getFields();
		if (fieldDecls.length > 0) {
			return fieldDecls[0].getModifiers();
		}
		return 0;
	}
	int modifiers= 0;

	if (fVariableKind == CONST_FIELD) {
		modifiers |= Modifier.FINAL | Modifier.STATIC;
	} else {
		ASTNode parent= fOriginalNode.getParent();
		if (parent instanceof QualifiedName) {
			IBinding qualifierBinding= ((QualifiedName)parent).getQualifier().resolveBinding();
			if (qualifierBinding instanceof ITypeBinding) {
				modifiers |= Modifier.STATIC;
			}
		} else if (ASTResolving.isInStaticContext(fOriginalNode)) {
			modifiers |= Modifier.STATIC;
		}
	}
	ASTNode node= ASTResolving.findParentType(fOriginalNode, true);
	if (newTypeDecl.equals(node)) {
		modifiers |= Modifier.PRIVATE;
	} else if (node instanceof AnonymousClassDeclaration) {
		modifiers |= Modifier.PROTECTED;
	} else {
		modifiers |= Modifier.PUBLIC;
	}

	return modifiers;
}
 
Example 10
Source File: SelfEncapsulateFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private int createModifiers() throws JavaModelException {
	int result = 0;
	if (Flags.isPublic(fVisibility)) {
		result |= Modifier.PUBLIC;
	} else if (Flags.isProtected(fVisibility)) {
		result |= Modifier.PROTECTED;
	} else if (Flags.isPrivate(fVisibility)) {
		result |= Modifier.PRIVATE;
	}
	if (JdtFlags.isStatic(fField)) {
		result |= Modifier.STATIC;
	}
	return result;
}
 
Example 11
Source File: SelfEncapsulateFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private TextEditGroup makeDeclarationPrivate(ASTRewrite rewriter, FieldDeclaration decl) {
	TextEditGroup description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_change_visibility);
	VariableDeclarationFragment[] vdfs = new VariableDeclarationFragment[] { fFieldDeclaration };
	int includedModifiers = Modifier.PRIVATE;
	int excludedModifiers = Modifier.PROTECTED | Modifier.PUBLIC;
	VariableDeclarationRewrite.rewriteModifiers(decl, vdfs, includedModifiers, excludedModifiers, rewriter, description);
	return description;
}
 
Example 12
Source File: ChangeSignatureWizard.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String getAccessModifierString(int modifier) {
	switch (modifier) {
		case Modifier.PUBLIC :
			return JdtFlags.VISIBILITY_STRING_PUBLIC;
		case Modifier.PROTECTED :
			return JdtFlags.VISIBILITY_STRING_PROTECTED;
		case Modifier.NONE :
			return RefactoringMessages.ChangeSignatureInputPage_default;
		case Modifier.PRIVATE :
			return JdtFlags.VISIBILITY_STRING_PRIVATE;
		default :
			throw new IllegalArgumentException("\"" + modifier + "\" is not a Modifier constant"); //$NON-NLS-1$ //$NON-NLS-2$
	}
}
 
Example 13
Source File: JdtFlags.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static int getVisibilityCode(IBinding binding) {
	if (isPublic(binding)) {
		return Modifier.PUBLIC;
	} else if (isProtected(binding)) {
		return Modifier.PROTECTED;
	} else if (isPackageVisible(binding)) {
		return Modifier.NONE;
	} else if (isPrivate(binding)) {
		return Modifier.PRIVATE;
	}
	Assert.isTrue(false);
	return VISIBILITY_CODE_INVALID;
}
 
Example 14
Source File: JdtFlags.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static int getVisibilityCode(IBinding binding) {
	if (isPublic(binding))
		return Modifier.PUBLIC;
	else if (isProtected(binding))
		return Modifier.PROTECTED;
	else if (isPackageVisible(binding))
		return Modifier.NONE;
	else if (isPrivate(binding))
		return Modifier.PRIVATE;
	Assert.isTrue(false);
	return VISIBILITY_CODE_INVALID;
}
 
Example 15
Source File: NewVariableCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int evaluateFieldModifiers(ASTNode newTypeDecl) {
	if (fSenderBinding.isAnnotation()) {
		return 0;
	}
	if (fSenderBinding.isInterface()) {
		// for interface members copy the modifiers from an existing field
		FieldDeclaration[] fieldDecls= ((TypeDeclaration) newTypeDecl).getFields();
		if (fieldDecls.length > 0) {
			return fieldDecls[0].getModifiers();
		}
		return 0;
	}
	int modifiers= 0;

	if (fVariableKind == CONST_FIELD) {
		modifiers |= Modifier.FINAL | Modifier.STATIC;
	} else {
		ASTNode parent= fOriginalNode.getParent();
		if (parent instanceof QualifiedName) {
			IBinding qualifierBinding= ((QualifiedName)parent).getQualifier().resolveBinding();
			if (qualifierBinding instanceof ITypeBinding) {
				modifiers |= Modifier.STATIC;
			}
		} else if (ASTResolving.isInStaticContext(fOriginalNode)) {
			modifiers |= Modifier.STATIC;
		}
	}
	ASTNode node= ASTResolving.findParentType(fOriginalNode, true);
	if (newTypeDecl.equals(node)) {
		modifiers |= Modifier.PRIVATE;
	} else if (node instanceof AnonymousClassDeclaration) {
		modifiers |= Modifier.PROTECTED;
	} else {
		modifiers |= Modifier.PUBLIC;
	}

	return modifiers;
}
 
Example 16
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public int[] getAvailableVisibilities() {
    if (isLocalInnerType()) {
        return new int[] { Modifier.NONE };
    } else {
        return new int[] { Modifier.PUBLIC, Modifier.PROTECTED, Modifier.NONE, Modifier.PRIVATE };
    }
}
 
Example 17
Source File: TokenModifiers.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean applies(IBinding binding) {
    int flags = binding.getModifiers();
    if ((flags & Modifier.PROTECTED) != 0) {
        return true;
    }
    return false;
}
 
Example 18
Source File: InputPageUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static Composite createVisibilityControl(Composite parent, final IVisibilityChangeListener visibilityChangeListener, int[] availableVisibilities, int correctVisibility) {
	List<Integer> allowedVisibilities= convertToIntegerList(availableVisibilities);
	if (allowedVisibilities.size() == 1)
		return null;

	Group group= new Group(parent, SWT.NONE);
	group.setText(RefactoringMessages.VisibilityControlUtil_Access_modifier);
	GridData gd= new GridData(GridData.FILL_HORIZONTAL);
	group.setLayoutData(gd);
	GridLayout layout= new GridLayout();
	layout.makeColumnsEqualWidth= true;
	layout.numColumns= 4;
	group.setLayout(layout);

	String[] labels= new String[] {
		"&public", //$NON-NLS-1$
		"pro&tected", //$NON-NLS-1$
		RefactoringMessages.VisibilityControlUtil_defa_ult_4,
		"pri&vate" //$NON-NLS-1$
	};
	Integer[] data= new Integer[] {
				new Integer(Modifier.PUBLIC),
				new Integer(Modifier.PROTECTED),
				new Integer(Modifier.NONE),
				new Integer(Modifier.PRIVATE)};
	Integer initialVisibility= new Integer(correctVisibility);
	for (int i= 0; i < labels.length; i++) {
		Button radio= new Button(group, SWT.RADIO);
		Integer visibilityCode= data[i];
		radio.setText(labels[i]);
		radio.setData(visibilityCode);
		radio.setSelection(visibilityCode.equals(initialVisibility));
		radio.setEnabled(allowedVisibilities.contains(visibilityCode));
		radio.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				visibilityChangeListener.visibilityChanged(((Integer)event.widget.getData()).intValue());
			}
		});
	}
	group.setLayoutData((new GridData(GridData.FILL_HORIZONTAL)));
	return group;
}
 
Example 19
Source File: IntroduceFactoryRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Look "in the vicinity" of the given range to find the <code>ClassInstanceCreation</code>
 * node that this search hit identified. Necessary because the <code>SearchEngine</code>
 * doesn't always cough up text extents that <code>NodeFinder.perform()</code> agrees with.
 * @param start
 * @param length
 * @param unitAST
 * @return return a {@link ClassInstanceCreation} or a {@link MethodRef} or <code>null</code> if this is really a constructor->constructor call (e.g. "this(...)")
 * @throws CoreException
 */
private ASTNode getCtorCallAt(int start, int length, CompilationUnit unitAST) throws CoreException {
	ICompilationUnit unitHandle= ASTCreator.getCu(unitAST);
	ASTNode node= NodeFinder.perform(unitAST, start, length);

	if (node == null)
		throw new CoreException(JavaUIStatus.createError(IStatus.ERROR,
				Messages.format(RefactoringCoreMessages.IntroduceFactory_noASTNodeForConstructorSearchHit,
						new Object[] { Integer.toString(start), Integer.toString(start + length),
						    BasicElementLabels.getJavaCodeString(unitHandle.getSource().substring(start, start + length)),
							BasicElementLabels.getFileName(unitHandle) }),
				null));

	if (node instanceof ClassInstanceCreation) {
		if (((ClassInstanceCreation)node).getAnonymousClassDeclaration() != null) {
			// Cannot replace anonymous inner class, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=250660
			fConstructorVisibility= Modifier.PROTECTED;
			return null;
		}
		return node;
	} else if (node instanceof VariableDeclaration) {
		Expression	init= ((VariableDeclaration) node).getInitializer();

		if (init instanceof ClassInstanceCreation) {
			return init;
		} else if (init != null)
			throw new CoreException(JavaUIStatus.createError(IStatus.ERROR,
					Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedInitializerNodeType,
							new Object[] { BasicElementLabels.getJavaCodeString(init.toString()), BasicElementLabels.getFileName(unitHandle) }),
					null));
		else
			throw new CoreException(JavaUIStatus.createError(IStatus.ERROR,
					Messages.format(RefactoringCoreMessages.IntroduceFactory_noConstructorCallNodeInsideFoundVarbleDecl,
							BasicElementLabels.getJavaCodeString(node.toString())),
					null));
	} else if (node instanceof ConstructorInvocation) {
		// This is a call we can bypass; it's from one constructor flavor
		// to another flavor on the same class.
		return null;
	} else if (node instanceof SuperConstructorInvocation) {
		// This is a call we can bypass; it's from one constructor flavor
		// to another flavor on the same class.
		fConstructorVisibility= Modifier.PROTECTED;
		return null;
	} else if (node instanceof ExpressionStatement) {
		Expression	expr= ((ExpressionStatement) node).getExpression();

		if (expr instanceof ClassInstanceCreation)
			return expr;
		else
			throw new CoreException(JavaUIStatus.createError(IStatus.ERROR,
					Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit,
							new Object[] { BasicElementLabels.getJavaCodeString(expr.toString()), BasicElementLabels.getFileName(unitHandle) }),
					null));
	} else if (node instanceof SimpleName && (node.getParent() instanceof MethodDeclaration || node.getParent() instanceof AbstractTypeDeclaration)) {
		// We seem to have been given a hit for an implicit call to the base-class constructor.
		// Do nothing with this (implicit) call, but have to make sure we make the derived class
		// doesn't lose access to the base-class constructor (so make it 'protected', not 'private').
		fConstructorVisibility= Modifier.PROTECTED;
		return null;
	} else if (node instanceof MethodRef) {
		return node;
	} else
		throw new CoreException(JavaUIStatus.createError(IStatus.ERROR,
				Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit,
						new Object[] { BasicElementLabels.getJavaElementName(node.getClass().getName() + "('" + node.toString() + "')"), BasicElementLabels.getFileName(unitHandle) }), //$NON-NLS-1$ //$NON-NLS-2$
				null));
}
 
Example 20
Source File: MemberVisibilityAdjustor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Is the specified modifier a visibility modifier?
 *
 * @param modifier the keyword to test
 * @return <code>true</code> if it is a visibility modifier, <code>false</code> otherwise
 */
private static boolean isVisibilityModifier(final int modifier) {
	return modifier == Modifier.NONE || modifier == Modifier.PUBLIC || modifier == Modifier.PROTECTED || modifier == Modifier.PRIVATE;
}