Java Code Examples for org.eclipse.jdt.core.dom.Modifier#isPrivate()

The following examples show how to use org.eclipse.jdt.core.dom.Modifier#isPrivate() . 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: Utils.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
public static void checkModifiers(ProblemCollector collector, BodyDeclaration elem,
		Predicate<Modifier> allowSpecific) {
	for (Object obj : elem.modifiers()) {
		if (!(obj instanceof Modifier)) {
			continue;
		}
		Modifier modifier = (Modifier) obj;
		boolean valid;
		if (allowSpecific.test(modifier)) {
			valid = true;
		} else {
			valid = modifier.isPrivate() || modifier.isPublic() || modifier.isProtected() || modifier.isFinal();
		}
		if (!valid) {
			collector.report(INVALID_MODIFIER.create(collector.getSourceInfo(), modifier));
		}
	}
}
 
Example 2
Source File: InferTypeArgumentsTCModel.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public VariableVariable2 makeVariableVariable(IVariableBinding variableBinding) {
	if (variableBinding == null)
		return null;
	TType type= getBoxedType(variableBinding.getType(), /*no boxing*/null);
	if (type == null)
		return null;
	VariableVariable2 cv= new VariableVariable2(type, variableBinding);
	VariableVariable2 storedCv= (VariableVariable2) storedCv(cv);
	if (storedCv == cv) {
		if (! variableBinding.isField() || Modifier.isPrivate(variableBinding.getModifiers()))
			fCuScopedConstraintVariables.add(storedCv);
		makeElementVariables(storedCv, type);
		makeArrayElementVariable(storedCv);
		if (fStoreToString)
			storedCv.setData(ConstraintVariable2.TO_STRING, '[' + variableBinding.getName() + ']');
	}
	return storedCv;
}
 
Example 3
Source File: InferTypeArgumentsTCModel.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public ParameterTypeVariable2 makeParameterTypeVariable(IMethodBinding methodBinding, int parameterIndex) {
	if (methodBinding == null)
		return null;
	TType type= getBoxedType(methodBinding.getParameterTypes() [parameterIndex], /*no boxing*/null);
	if (type == null)
		return null;

	ParameterTypeVariable2 cv= new ParameterTypeVariable2(type, parameterIndex, methodBinding);
	ParameterTypeVariable2 storedCv= (ParameterTypeVariable2) storedCv(cv);
	if (storedCv == cv) {
		if (methodBinding.getDeclaringClass().isLocal() || Modifier.isPrivate(methodBinding.getModifiers()))
			fCuScopedConstraintVariables.add(cv);
		makeElementVariables(storedCv, type);
		makeArrayElementVariable(storedCv);
		if (fStoreToString)
			storedCv.setData(ConstraintVariable2.TO_STRING, "[Parameter(" + parameterIndex + "," + Bindings.asString(methodBinding) + ")]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}
	return storedCv;
}
 
Example 4
Source File: AbstractToStringGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected String createNameSuggestion(String baseName, int variableKind) {
	if (excluded == null) {
		excluded= new HashSet<String>();
		IVariableBinding[] fields= fContext.getTypeBinding().getDeclaredFields();
		for (int i= 0; i < fields.length; i++) {
			excluded.add(fields[i].getName());
		}
		ITypeBinding superType= fContext.getTypeBinding().getSuperclass();
		while (superType != null) {
			fields= superType.getDeclaredFields();
			for (int i= 0; i < fields.length; i++) {
				if (!Modifier.isPrivate(fields[i].getModifiers())) {
					excluded.add(fields[i].getName());
				}
			}
			superType= superType.getSuperclass();
		}
		ITypeBinding[] types= fContext.getTypeBinding().getDeclaredTypes();
		for (int i= 0; i < types.length; i++) {
			excluded.add(types[i].getName());
		}
		superType= fContext.getTypeBinding().getSuperclass();
		while (superType != null) {
			types= superType.getDeclaredTypes();
			for (int i= 0; i < types.length; i++) {
				if (!Modifier.isPrivate(types[i].getModifiers())) {
					excluded.add(types[i].getName());
				}
			}
			superType= superType.getSuperclass();
		}
	}
	return StubUtility.getVariableNameSuggestions(variableKind, fContext.getCompilationUnit().getJavaProject(), baseName, 0, excluded, true)[0];
}
 
Example 5
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isVisibleInHierarchy(IMethodBinding member, IPackageBinding pack) {
	int otherflags= member.getModifiers();
	ITypeBinding declaringType= member.getDeclaringClass();
	if (Modifier.isPublic(otherflags) || Modifier.isProtected(otherflags) || (declaringType != null && declaringType.isInterface())) {
		return true;
	} else if (Modifier.isPrivate(otherflags)) {
		return false;
	}
	return declaringType != null && pack == declaringType.getPackage();
}
 
Example 6
Source File: InJavaImporter.java    From jdt2famix with Eclipse Public License 1.0 5 votes vote down vote up
private void extractBasicModifiersFromBinding(int modifiers, NamedEntity entity) {
	Boolean publicModifier = Modifier.isPublic(modifiers);
	Boolean protectedModifier = Modifier.isProtected(modifiers);
	Boolean privateModifier = Modifier.isPrivate(modifiers);
	if (publicModifier)
		entity.addModifiers("public");
	if (protectedModifier)
		entity.addModifiers("protected");
	if (privateModifier)
		entity.addModifiers("private");
	if (!(publicModifier || protectedModifier || privateModifier))
		entity.addModifiers("package");
	if (Modifier.isFinal(modifiers))
		entity.addModifiers("final");
	if (Modifier.isAbstract(modifiers))
		entity.addModifiers("abstract");
	if (Modifier.isNative(modifiers))
		entity.addModifiers("native");
	if (Modifier.isSynchronized(modifiers))
		entity.addModifiers("synchronized");
	if (Modifier.isTransient(modifiers))
		entity.addModifiers("transient");
	if (Modifier.isVolatile(modifiers))
		entity.addModifiers("volatile");
	/*
	 * We do not extract the static modifier here because we want to set the
	 * hasClassScope property and we do that specifically only for attributes and
	 * methods
	 */
}
 
Example 7
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void getOverridableMethods(AST ast, ITypeBinding superBinding, List<IMethodBinding> allMethods) {
	IMethodBinding[] methods= superBinding.getDeclaredMethods();
	for (int offset= 0; offset < methods.length; offset++) {
		final int modifiers= methods[offset].getModifiers();
		if (!methods[offset].isConstructor() && !Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers)) {
			if (findOverridingMethod(methods[offset], allMethods) == null && !Modifier.isStatic(modifiers))
				allMethods.add(methods[offset]);
		}
	}
	ITypeBinding[] superInterfaces= superBinding.getInterfaces();
	for (int index= 0; index < superInterfaces.length; index++) {
		getOverridableMethods(ast, superInterfaces[index], allMethods);
	}
}
 
Example 8
Source File: NumberOfMethods.java    From ck with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(MethodDeclaration node) {
	methods++;

	// visibility
	boolean isPublic = Modifier.isPublic(node.getModifiers());
	boolean isPrivate = Modifier.isPrivate(node.getModifiers());
	boolean isProtected = Modifier.isProtected(node.getModifiers());

	if(isPublic)
		publicMethods++;
	else if(isPrivate)
		privateMethods++;
	else if(isProtected)
		protectedMethods++;
	else
		defaultMethods++;

	// other characteristics

	boolean isFinal = Modifier.isFinal(node.getModifiers());
	boolean isSynchronized = Modifier.isSynchronized(node.getModifiers());
	boolean isAbstract = Modifier.isAbstract(node.getModifiers());
	boolean isStatic = Modifier.isStatic(node.getModifiers());

	if(isStatic)
		staticMethods++;

	if(isAbstract)
		abstractMethods++;

	if(isFinal)
		finalMethods++;

	if(isSynchronized)
		synchronizedMethods++;
}
 
Example 9
Source File: MethodChecks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns <code>true</code> iff the method could be a virtual method,
 * i.e. if it is not a constructor, is private, or is static.
 *
 * @param methodBinding a method
 * @return <code>true</code> iff the method could a virtual method
 */
public static boolean isVirtual(IMethodBinding methodBinding){
	if (methodBinding.isConstructor())
		return false;
	if (Modifier.isPrivate(methodBinding.getModifiers()))
		return false;
	if (Modifier.isStatic(methodBinding.getModifiers()))
		return false;
	return true;
}
 
Example 10
Source File: MethodChecks.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns <code>true</code> iff the method could be a virtual method,
 * i.e. if it is not a constructor, is private, or is static.
 *
 * @param methodBinding a method
 * @return <code>true</code> iff the method could a virtual method
 */
public static boolean isVirtual(IMethodBinding methodBinding){
	if (methodBinding.isConstructor()) {
		return false;
	}
	if (Modifier.isPrivate(methodBinding.getModifiers())) {
		return false;
	}
	if (Modifier.isStatic(methodBinding.getModifiers())) {
		return false;
	}
	return true;
}
 
Example 11
Source File: NewDefiningMethodProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int evaluateModifiers() {
	if (getSenderBinding().isInterface()) {
		return 0;
	} else {
		int modifiers= fMethod.getModifiers();
		if (Modifier.isPrivate(modifiers)) {
			modifiers |= Modifier.PROTECTED;
		}
		return modifiers & (Modifier.PUBLIC | Modifier.PROTECTED | Modifier.ABSTRACT | Modifier.STRICTFP);
	}
}
 
Example 12
Source File: BindingLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static ImageDescriptor getFieldImageDescriptor(IVariableBinding binding) {
	final int modifiers= binding.getModifiers();
	if (Modifier.isPublic(modifiers) || binding.isEnumConstant())
		return JavaPluginImages.DESC_FIELD_PUBLIC;
	if (Modifier.isProtected(modifiers))
		return JavaPluginImages.DESC_FIELD_PROTECTED;
	if (Modifier.isPrivate(modifiers))
		return JavaPluginImages.DESC_FIELD_PRIVATE;

	return JavaPluginImages.DESC_FIELD_DEFAULT;
}
 
Example 13
Source File: OrganizeImportsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean needsImport(ITypeBinding typeBinding, SimpleName ref) {
	if (!typeBinding.isTopLevel() && !typeBinding.isMember() || typeBinding.isRecovered()) {
		return false; // no imports for anonymous, local, primitive types or parameters types
	}
	int modifiers= typeBinding.getModifiers();
	if (Modifier.isPrivate(modifiers)) {
		return false; // imports for privates are not required
	}
	ITypeBinding currTypeBinding= Bindings.getBindingOfParentType(ref);
	if (currTypeBinding == null) {
		if (ASTNodes.getParent(ref, ASTNode.PACKAGE_DECLARATION) != null) {
			return true; // reference in package-info.java
		}
		return false; // not in a type
	}
	if (!Modifier.isPublic(modifiers)) {
		if (!currTypeBinding.getPackage().getName().equals(typeBinding.getPackage().getName())) {
			return false; // not visible
		}
	}

	ASTNode parent= ref.getParent();
	while (parent instanceof Type) {
		parent= parent.getParent();
	}
	if (parent instanceof AbstractTypeDeclaration && parent.getParent() instanceof CompilationUnit) {
		return true;
	}

	if (typeBinding.isMember()) {
		if (fAnalyzer.isDeclaredInScope(typeBinding, ref, ScopeAnalyzer.TYPES | ScopeAnalyzer.CHECK_VISIBILITY))
			return false;
	}
	return true;
}
 
Example 14
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void setVisibility(JvmMember result, int modifiers) {
	if (Modifier.isPrivate(modifiers))
		result.setVisibility(JvmVisibility.PRIVATE);
	else if (Modifier.isProtected(modifiers))
		result.setVisibility(JvmVisibility.PROTECTED);
	else if (Modifier.isPublic(modifiers))
		result.setVisibility(JvmVisibility.PUBLIC);
	else
		result.setVisibility(JvmVisibility.DEFAULT);
}
 
Example 15
Source File: BindingLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static ImageDescriptor getMethodImageDescriptor(int modifiers) {
	if (Modifier.isPublic(modifiers))
		return JavaPluginImages.DESC_MISC_PUBLIC;
	if (Modifier.isProtected(modifiers))
		return JavaPluginImages.DESC_MISC_PROTECTED;
	if (Modifier.isPrivate(modifiers))
		return JavaPluginImages.DESC_MISC_PRIVATE;

	return JavaPluginImages.DESC_MISC_DEFAULT;
}
 
Example 16
Source File: JavaASTVisitor.java    From SnowGraph with Apache License 2.0 5 votes vote down vote up
private static String getVisibility(MethodDeclaration decl) {
    int modifiers = decl.getModifiers();
    if (Modifier.isPrivate(modifiers))
        return "private";
    if (Modifier.isProtected(modifiers))
        return "protected";
    if (Modifier.isPublic(modifiers))
        return "public";
    return "package";
}
 
Example 17
Source File: JavaASTVisitor.java    From SnowGraph with Apache License 2.0 5 votes vote down vote up
private static String getVisibility(TypeDeclaration decl) {
    int modifiers = decl.getModifiers();
    if (Modifier.isPrivate(modifiers))
        return "private";
    if (Modifier.isProtected(modifiers))
        return "protected";
    if (Modifier.isPublic(modifiers))
        return "public";
    return "package";
}
 
Example 18
Source File: MemberVisibilityAdjustor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Do the specified modifiers represent a lower visibility than the required threshold?
 *
 * @param modifiers the modifiers to test
 * @param threshold the visibility threshold to compare with
 * @return <code>true</code> if the visibility is lower than required, <code>false</code> otherwise
 */
public static boolean hasLowerVisibility(final int modifiers, final int threshold) {
	if (Modifier.isPrivate(threshold))
		return false;
	else if (Modifier.isPublic(threshold))
		return !Modifier.isPublic(modifiers);
	else if (Modifier.isProtected(threshold))
		return !Modifier.isProtected(modifiers) && !Modifier.isPublic(modifiers);
	else
		return Modifier.isPrivate(modifiers);
}
 
Example 19
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void createCompilationUnitRewrite(final ITypeBinding[] parameters, final CompilationUnitRewrite targetRewrite, final Map<ICompilationUnit, SearchMatch[]> typeReferences, final Map<ICompilationUnit, SearchMatch[]> constructorReferences, boolean visibilityWasAdjusted, final ICompilationUnit sourceUnit, final ICompilationUnit targetUnit, final boolean remove, final RefactoringStatus status, final IProgressMonitor monitor) throws CoreException {
	Assert.isNotNull(parameters);
	Assert.isNotNull(targetRewrite);
	Assert.isNotNull(typeReferences);
	Assert.isNotNull(constructorReferences);
	Assert.isNotNull(sourceUnit);
	Assert.isNotNull(targetUnit);
	final CompilationUnit root= targetRewrite.getRoot();
	final ASTRewrite rewrite= targetRewrite.getASTRewrite();
	if (targetUnit.equals(sourceUnit)) {
		final AbstractTypeDeclaration declaration= findTypeDeclaration(fType, root);
		final TextEditGroup qualifierGroup= fSourceRewrite.createGroupDescription(RefactoringCoreMessages.MoveInnerToTopRefactoring_change_qualifier);
		ITypeBinding binding= declaration.resolveBinding();
		if (!remove) {
			if (!JdtFlags.isStatic(fType) && fCreateInstanceField) {
				if (JavaElementUtil.getAllConstructors(fType).length == 0)
					createConstructor(declaration, rewrite);
				else
					modifyConstructors(declaration, rewrite);
				addInheritedTypeQualifications(declaration, targetRewrite, qualifierGroup);
				addEnclosingInstanceDeclaration(declaration, rewrite);
			}
			fTypeImports= new HashSet<ITypeBinding>();
			fStaticImports= new HashSet<IBinding>();
			ImportRewriteUtil.collectImports(fType.getJavaProject(), declaration, fTypeImports, fStaticImports, false);
			if (binding != null)
				fTypeImports.remove(binding);
		}
		addEnclosingInstanceTypeParameters(parameters, declaration, rewrite);
		modifyAccessToEnclosingInstance(targetRewrite, declaration, monitor);
		if (binding != null) {
			modifyInterfaceMemberModifiers(binding);
			final ITypeBinding declaring= binding.getDeclaringClass();
			if (declaring != null)
				declaration.accept(new TypeReferenceQualifier(binding, null));
		}
		final TextEditGroup groupMove= targetRewrite.createGroupDescription(RefactoringCoreMessages.MoveInnerToTopRefactoring_change_label);
		if (remove) {
			rewrite.remove(declaration, groupMove);
			targetRewrite.getImportRemover().registerRemovedNode(declaration);
		} else {
			// Bug 101017/96308: Rewrite the visibility of the element to be
			// moved and add a warning.

			// Note that this cannot be done in the MemberVisibilityAdjustor, as the private and
			// static flags must always be cleared when moving to new type.
			int newFlags= JdtFlags.clearFlag(Modifier.STATIC, declaration.getModifiers());

			if (!visibilityWasAdjusted) {
				if (Modifier.isPrivate(declaration.getModifiers()) || Modifier.isProtected(declaration.getModifiers())) {
					newFlags= JdtFlags.clearFlag(Modifier.PROTECTED | Modifier.PRIVATE, newFlags);
					final RefactoringStatusEntry entry= new RefactoringStatusEntry(RefactoringStatus.WARNING, Messages.format(RefactoringCoreMessages.MoveInnerToTopRefactoring_change_visibility_type_warning, new String[] { BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED)}), JavaStatusContext.create(fSourceRewrite.getCu()));
					if (!containsStatusEntry(status, entry))
						status.addEntry(entry);
				}
			}

			ModifierRewrite.create(rewrite, declaration).setModifiers(newFlags, groupMove);
		}
	}
	ASTNode[] references= getReferenceNodesIn(root, typeReferences, targetUnit);
	for (int index= 0; index < references.length; index++)
		updateTypeReference(parameters, references[index], targetRewrite, targetUnit);
	references= getReferenceNodesIn(root, constructorReferences, targetUnit);
	for (int index= 0; index < references.length; index++)
		updateConstructorReference(parameters, references[index], targetRewrite, targetUnit);
}
 
Example 20
Source File: ExtractClassRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private FieldDeclaration performFieldRewrite(IType type, ParameterObjectFactory pof, RefactoringStatus status) throws CoreException {
	fBaseCURewrite= new CompilationUnitRewrite(type.getCompilationUnit());
	SimpleName name= (SimpleName) NodeFinder.perform(fBaseCURewrite.getRoot(), type.getNameRange());
	TypeDeclaration typeNode= (TypeDeclaration) ASTNodes.getParent(name, ASTNode.TYPE_DECLARATION);
	ASTRewrite rewrite= fBaseCURewrite.getASTRewrite();
	int modifier= Modifier.PRIVATE;
	TextEditGroup removeFieldGroup= fBaseCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractClassRefactoring_group_remove_field);
	FieldDeclaration lastField= null;
	initializeDeclaration(typeNode);
	for (Iterator<FieldInfo> iter= fVariables.values().iterator(); iter.hasNext();) {
		FieldInfo pi= iter.next();
		if (isCreateField(pi)) {
			VariableDeclarationFragment vdf= pi.declaration;
			FieldDeclaration parent= (FieldDeclaration) vdf.getParent();
			if (lastField == null)
				lastField= parent;
			else if (lastField.getStartPosition() < parent.getStartPosition())
				lastField= parent;

			ListRewrite listRewrite= rewrite.getListRewrite(parent, FieldDeclaration.FRAGMENTS_PROPERTY);
			removeNode(vdf, removeFieldGroup, fBaseCURewrite);
			if (listRewrite.getRewrittenList().size() == 0) {
				removeNode(parent, removeFieldGroup, fBaseCURewrite);
			}

			if (fDescriptor.isCreateTopLevel()) {
				IVariableBinding binding= vdf.resolveBinding();
				ITypeRoot typeRoot= fBaseCURewrite.getCu();
				if (binding == null || binding.getType() == null){
					status.addFatalError(Messages.format(RefactoringCoreMessages.ExtractClassRefactoring_fatal_error_cannot_resolve_binding, BasicElementLabels.getJavaElementName(pi.name)), JavaStatusContext.create(typeRoot, vdf));
				} else {
					ITypeBinding typeBinding= binding.getType();
					if (Modifier.isPrivate(typeBinding.getModifiers())){
						status.addError(Messages.format(RefactoringCoreMessages.ExtractClassRefactoring_error_referencing_private_class, BasicElementLabels.getJavaElementName(typeBinding.getName())), JavaStatusContext.create(typeRoot, vdf));
					} else if (Modifier.isProtected(typeBinding.getModifiers())){
						ITypeBinding declaringClass= typeBinding.getDeclaringClass();
						if (declaringClass != null) {
							IPackageBinding package1= declaringClass.getPackage();
							if (package1 != null && !fDescriptor.getPackage().equals(package1.getName())){
								status.addError(Messages.format(RefactoringCoreMessages.ExtractClassRefactoring_error_referencing_protected_class, new String[] {BasicElementLabels.getJavaElementName(typeBinding.getName()), BasicElementLabels.getJavaElementName(fDescriptor.getPackage())}), JavaStatusContext.create(typeRoot, vdf));
							}
						}
					}
				}
			}
			Expression initializer= vdf.getInitializer();
			if (initializer != null)
				pi.initializer= initializer;
			int modifiers= parent.getModifiers();
			if (!MemberVisibilityAdjustor.hasLowerVisibility(modifiers, modifier)){
				modifier= modifiers;
			}
		}
	}
	FieldDeclaration fieldDeclaration= createParameterObjectField(pof, typeNode, modifier);
	ListRewrite bodyDeclList= rewrite.getListRewrite(typeNode, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
	if (lastField != null)
		bodyDeclList.insertAfter(fieldDeclaration, lastField, null);
	else
		bodyDeclList.insertFirst(fieldDeclaration, null);
	return fieldDeclaration;
}