Java Code Examples for org.eclipse.jdt.core.Flags#isPublic()

The following examples show how to use org.eclipse.jdt.core.Flags#isPublic() . 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: DeltaConverter.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.3
 * @deprecated This method is not used anymore.
 */
@Deprecated
protected IType getPrimaryTypeFrom(ICompilationUnit cu) {
	try {
		if (cu.exists()) {
			IType primaryType = cu.findPrimaryType();
			if (primaryType != null)
				return primaryType;

			// if no exact match is found, return the first public type in CU (if any)
			for (IType type : cu.getTypes()) {
				if (Flags.isPublic(type.getFlags()))
					return type;
			}
		}
	} catch (JavaModelException e) {
		if (LOGGER.isDebugEnabled())
			LOGGER.debug(e, e);
	}
	return null;
}
 
Example 2
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Evaluates if a member in the focus' element hierarchy is visible from
 * elements in a package.
 * @param member The member to test the visibility for
 * @param pack The package of the focus element focus
 * @return returns <code>true</code> if the member is visible from the package
 * @throws JavaModelException thrown when the member can not be accessed
 */
public static boolean isVisibleInHierarchy(IMember member, IPackageFragment pack) throws JavaModelException {
	int type= member.getElementType();
	if  (type == IJavaElement.INITIALIZER ||  (type == IJavaElement.METHOD && member.getElementName().startsWith("<"))) { //$NON-NLS-1$
		return false;
	}

	int otherflags= member.getFlags();

	IType declaringType= member.getDeclaringType();
	if (Flags.isPublic(otherflags) || Flags.isProtected(otherflags) || (declaringType != null && isInterfaceOrAnnotation(declaringType))) {
		return true;
	} else if (Flags.isPrivate(otherflags)) {
		return false;
	}

	IPackageFragment otherpack= (IPackageFragment) member.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
	return (pack != null && pack.equals(otherpack));
}
 
Example 3
Source File: JsniMethodBodyCompletionProposalComputer.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private static JavaCompletionProposal createProposal(int flags,
    String replacementString, int replacementOffset, int numCharsFilled, 
    int numCharsToOverwrite, String displayString) {
  Image image;
  GWTPlugin plugin = GWTPlugin.getDefault();
  if (Flags.isPublic(flags)) {
    image = plugin.getImage(GWTImages.JSNI_PUBLIC_METHOD_SMALL);
  } else if (Flags.isPrivate(flags)) {
    image = plugin.getImage(GWTImages.JSNI_PRIVATE_METHOD_SMALL);
  } else if (Flags.isProtected(flags)) {
    image = plugin.getImage(GWTImages.JSNI_PROTECTED_METHOD_SMALL);
  } else {
    image = plugin.getImage(GWTImages.JSNI_DEFAULT_METHOD_SMALL);
  }
  replacementString = replacementString.substring(numCharsFilled);
  return new JavaCompletionProposal(replacementString, replacementOffset,
      numCharsToOverwrite, image, "/*-{ " + displayString + " }-*/;", 0);
}
 
Example 4
Source File: TypeMatchFilters.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean accept(int modifiers, char[] packageName, char[] simpleTypeName,
		char[][] enclosingTypeNames, String path) {
	if (isInternalClass(simpleTypeName, enclosingTypeNames)) {
		return false;
	}
	return Flags.isPublic(modifiers);
}
 
Example 5
Source File: JdtFlags.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isPublic(IMember member) throws JavaModelException{
	if (isInterfaceOrAnnotationMember(member))
		return true;
	if (isEnumConstant(member))
		return true;
	return Flags.isPublic(member.getFlags());
}
 
Example 6
Source File: JavaElementImageProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static ImageDescriptor getInnerAnnotationImageDescriptor(boolean isInInterfaceOrAnnotation, int flags) {
	if (Flags.isPublic(flags) || isInInterfaceOrAnnotation)
		return JavaPluginImages.DESC_OBJS_ANNOTATION;
	else if (Flags.isPrivate(flags))
		return JavaPluginImages.DESC_OBJS_ANNOTATION_PRIVATE;
	else if (Flags.isProtected(flags))
		return JavaPluginImages.DESC_OBJS_ANNOTATION_PROTECTED;
	else
		return JavaPluginImages.DESC_OBJS_ANNOTATION_DEFAULT;
}
 
Example 7
Source File: JavaContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isVisible(TypeNameMatch curr, ICompilationUnit cu) {
	int flags= curr.getModifiers();
	if (Flags.isPrivate(flags)) {
		return false;
	}
	if (Flags.isPublic(flags) || Flags.isProtected(flags)) {
		return true;
	}
	return curr.getPackageName().equals(cu.getParent().getElementName());
}
 
Example 8
Source File: SelfEncapsulateFieldInputPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void createAccessModifier(Composite result) {
	int visibility= fRefactoring.getVisibility();
	if (Flags.isPublic(visibility))
		return;

	Label label= new Label(result, SWT.NONE);
	label.setText(RefactoringMessages.SelfEncapsulateFieldInputPage_access_Modifiers);
	fEnablements.add(label);

	Composite group= new Composite(result, SWT.NONE);
	group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));

	GridLayout layout= new GridLayout(4, false);
	layout.marginWidth= 0;
	layout.marginHeight= 0;
	group.setLayout(layout);

	group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

	Object[] info= createData(visibility);
	String[] labels= (String[])info[0];
	Integer[] data= (Integer[])info[1];
	for (int i= 0; i < labels.length; i++) {
		Button radio= new Button(group, SWT.RADIO);
		radio.setText(labels[i]);
		radio.setData(data[i]);
		int iData= data[i].intValue();
		if (iData == visibility)
			radio.setSelection(true);
		radio.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				fRefactoring.setVisibility(((Integer)event.widget.getData()).intValue());
			}
		});
		fEnablements.add(radio);
	}
}
 
Example 9
Source File: JavaElementImageProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static ImageDescriptor getFieldImageDescriptor(boolean isInInterfaceOrAnnotation, int flags) {
	if (Flags.isPublic(flags) || isInInterfaceOrAnnotation || Flags.isEnum(flags))
		return JavaPluginImages.DESC_FIELD_PUBLIC;
	if (Flags.isProtected(flags))
		return JavaPluginImages.DESC_FIELD_PROTECTED;
	if (Flags.isPrivate(flags))
		return JavaPluginImages.DESC_FIELD_PRIVATE;

	return JavaPluginImages.DESC_FIELD_DEFAULT;
}
 
Example 10
Source File: AddImportsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isVisible(TypeNameMatch curr) {
	int flags= curr.getModifiers();
	if (Flags.isPrivate(flags)) {
		return false;
	}
	if (Flags.isPublic(flags) || Flags.isProtected(flags)) {
		return true;
	}
	return curr.getPackageName().equals(fCompilationUnit.getParent().getElementName());
}
 
Example 11
Source File: GenerateToStringDialog.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public IStatus validateBuilderType(IType type) {
	if (fLastValidBuilderType != null && fLastValidBuilderType.equals(type)) {
		return new StatusInfo();
	}

	try {
		IMethod[] methods= type.getMethods();
		boolean foundConstructor= false;
		for (int i= 0; i < methods.length; i++) {
			if (methods[i].isConstructor() && Flags.isPublic(methods[i].getFlags())) {
				String[] parameterTypes= methods[i].getParameterTypes();
				if (parameterTypes.length == 1 && "java.lang.Object".equals(JavaModelUtil.getResolvedTypeName(parameterTypes[0], type))) { //$NON-NLS-1$
					foundConstructor= true;
					break;
				}
			}
		}
		if (!foundConstructor)
			return new StatusInfo(IStatus.ERROR, JavaUIMessages.GenerateToStringDialog_customBuilderConfig_noConstructorError);

		List<String> appendMethodSuggestions= getAppendMethodSuggestions(type);
		if (appendMethodSuggestions.isEmpty())
			return new StatusInfo(IStatus.ERROR, JavaUIMessages.GenerateToStringDialog_customBuilderConfig_noAppendMethodError);

		List<String> resultMethodSuggestions= getResultMethodSuggestions(type);
		if (resultMethodSuggestions.isEmpty())
			return new StatusInfo(IStatus.ERROR, JavaUIMessages.GenerateToStringDialog_customBuilderConfig_noResultMethodError);

		fLastValidBuilderType= type;
		fLastValidAppendMethodSuggestions= appendMethodSuggestions;
		fLastValidResultMethodSuggestions= resultMethodSuggestions;
		return new StatusInfo();
	} catch (JavaModelException e1) {
		return new StatusInfo(IStatus.WARNING, JavaUIMessages.GenerateToStringDialog_customBuilderConfig_typeValidationError);
	}
}
 
Example 12
Source File: JDTMethodHelper.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isValidGetterMethod(final List<IMethod> methods, final IMethod method) {
    try {
        return Flags.isPublic(method.getFlags())
                && method.getParameterTypes().length == 0
                && !method.isConstructor()
                && !method.getReturnType().equals("V")
                && doesNotContainSignature(method, methods);
    } catch (JavaModelException e) {
        return false;
    }
}
 
Example 13
Source File: DialogFactoryHelperImpl.java    From jenerate with Eclipse Public License 1.0 5 votes vote down vote up
private IField[] getNonStaticNonCacheFieldsAndAccessibleNonStaticFieldsOfSuperclasses(IType objectClass,
        PreferencesManager preferencesManager) throws JavaModelException {
    List<IField> result = new ArrayList<>();

    ITypeHierarchy typeHierarchy = objectClass.newSupertypeHierarchy(null);
    IType[] superclasses = typeHierarchy.getAllSuperclasses(objectClass);

    for (int i = 0; i < superclasses.length; i++) {
        IField[] fields = superclasses[i].getFields();

        boolean samePackage = objectClass.getPackageFragment().getElementName()
                .equals(superclasses[i].getPackageFragment().getElementName());

        for (int j = 0; j < fields.length; j++) {

            if (!samePackage && !Flags.isPublic(fields[j].getFlags()) && !Flags.isProtected(fields[j].getFlags())) {
                continue;
            }

            if (!Flags.isPrivate(fields[j].getFlags()) && !Flags.isStatic(fields[j].getFlags())) {
                result.add(fields[j]);
            }
        }
    }

    result.addAll(Arrays.asList(getNonStaticNonCacheFields(objectClass, preferencesManager)));

    return result.toArray(new IField[result.size()]);
}
 
Example 14
Source File: JavaElementImageProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static ImageDescriptor getInnerEnumImageDescriptor(boolean isInInterfaceOrAnnotation, int flags) {
	if (Flags.isPublic(flags) || isInInterfaceOrAnnotation)
		return JavaPluginImages.DESC_OBJS_ENUM;
	else if (Flags.isPrivate(flags))
		return JavaPluginImages.DESC_OBJS_ENUM_PRIVATE;
	else if (Flags.isProtected(flags))
		return JavaPluginImages.DESC_OBJS_ENUM_PROTECTED;
	else
		return JavaPluginImages.DESC_OBJS_ENUM_DEFAULT;
}
 
Example 15
Source File: MemberFilter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
	try {
		if (element instanceof IMember) {
			IMember member= (IMember) element;
			int memberType= member.getElementType();

			if (hasFilter(FILTER_FIELDS) && memberType == IJavaElement.FIELD) {
				return false;
			}

			if (hasFilter(FILTER_LOCALTYPES) && memberType == IJavaElement.TYPE && isLocalType((IType) member)) {
				return false;
			}

			if (member.getElementName().startsWith("<")) { // filter out <clinit> //$NON-NLS-1$
				return false;
			}
			int flags= member.getFlags();
			if (hasFilter(FILTER_STATIC) && (Flags.isStatic(flags) || isFieldInInterfaceOrAnnotation(member)) && memberType != IJavaElement.TYPE) {
				return false;
			}
			if (hasFilter(FILTER_NONPUBLIC) && !Flags.isPublic(flags) && !isMemberInInterfaceOrAnnotation(member) && !isTopLevelType(member) && !isEnumConstant(member)) {
				return false;
			}
		}
	} catch (JavaModelException e) {
		// ignore
	}
	return true;
}
 
Example 16
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 17
Source File: JdtFlags.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isPublic(IMember member) throws JavaModelException{
	if (isInterfaceOrAnnotationMember(member)) {
		return true;
	}
	if (isEnumConstant(member)) {
		return true;
	}
	return Flags.isPublic(member.getFlags());
}
 
Example 18
Source File: IJUnitMethodTester.java    From jdt-codemining with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isMethod(IMethod method, boolean onlyPublicMethod) {
	try {
		int flags = method.getFlags();
		if (onlyPublicMethod && !Flags.isPublic(flags)) {
			return false;
		}
		// 'V' is void signature
		return !(method.isConstructor() || Flags.isAbstract(flags) || Flags.isStatic(flags)
				|| !"V".equals(method.getReturnType()));
	} catch (JavaModelException e) {
		// ignore
		return false;
	}
}
 
Example 19
Source File: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected void appendMembers(final IType type, final IProgressMonitor monitor) throws JavaModelException {
	try {
		monitor.beginTask(RefactoringCoreMessages.StubCreationOperation_creating_type_stubs, 1);
		final IJavaElement[] children= type.getChildren();
		for (int index= 0; index < children.length; index++) {
			final IMember child= (IMember) children[index];
			final int flags= child.getFlags();
			final boolean isPrivate= Flags.isPrivate(flags);
			final boolean isDefault= !Flags.isPublic(flags) && !Flags.isProtected(flags) && !isPrivate;
			final boolean stub= fStubInvisible || (!isPrivate && !isDefault);
			if (child instanceof IType) {
				if (stub)
					appendTypeDeclaration((IType) child, new SubProgressMonitor(monitor, 1));
			} else if (child instanceof IField) {
				if (stub && !Flags.isEnum(flags) && !Flags.isSynthetic(flags))
					appendFieldDeclaration((IField) child);
			} else if (child instanceof IMethod) {
				final IMethod method= (IMethod) child;
				final String name= method.getElementName();
				if (method.getDeclaringType().isEnum()) {
					final int count= method.getNumberOfParameters();
					if (count == 0 && "values".equals(name)) //$NON-NLS-1$
						continue;
					if (count == 1 && "valueOf".equals(name) && "Ljava.lang.String;".equals(method.getParameterTypes()[0])) //$NON-NLS-1$ //$NON-NLS-2$
						continue;
					if (method.isConstructor())
						continue;
				}
				boolean skip= !stub || name.equals("<clinit>"); //$NON-NLS-1$
				if (method.isConstructor())
					skip= false;
				skip= skip || Flags.isSynthetic(flags) || Flags.isBridge(flags);
				if (!skip)
					appendMethodDeclaration(method);
			}
			fBuffer.append("\n"); //$NON-NLS-1$
		}
	} finally {
		monitor.done();
	}
}
 
Example 20
Source File: CustomBuilderGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Searches through methods with proper name and for each argument type remembers the best
 * option
 */
private void fillAppendMethodsMap() {
	try {
		IJavaProject javaProject= getContext().getTypeBinding().getJavaElement().getJavaProject();
		IType type= javaProject.findType(getContext().getCustomBuilderClass());
		IType[] types= type.newSupertypeHierarchy(null).getAllClasses();
		for (int i= 0; i < types.length; i++) {
			IMethod[] methods= types[i].getMethods();
			for (int j= 0; j < methods.length; j++) {
				if (!Flags.isPublic(methods[j].getFlags()) || !methods[j].getElementName().equals(getContext().getCustomBuilderAppendMethod()))
					continue;
				String[] parameterTypes= methods[j].getParameterTypes();
				AppendMethodInformation appendMethodInformation= new AppendMethodInformation();
				String specyficType;
				if (parameterTypes.length == 1) {
					specyficType= JavaModelUtil.getResolvedTypeName(parameterTypes[0], types[i]);
					appendMethodInformation.methodType= 1;
				} else if (parameterTypes.length == 2) {
					String resolvedParameterTypeName1= JavaModelUtil.getResolvedTypeName(parameterTypes[0], types[i]);
					String resolvedParameterTypeName2= JavaModelUtil.getResolvedTypeName(parameterTypes[1], types[i]);
					if (resolvedParameterTypeName1.equals("java.lang.String")) {//$NON-NLS-1$
						specyficType= resolvedParameterTypeName2;
						appendMethodInformation.methodType= 3;
					} else if (resolvedParameterTypeName2.equals("java.lang.String")) {//$NON-NLS-1$
						specyficType= resolvedParameterTypeName1;
						appendMethodInformation.methodType= 2;
					} else
						continue;
				} else
					continue;

				String returnTypeName= JavaModelUtil.getResolvedTypeName(methods[j].getReturnType(), types[i]);
				IType returnType= javaProject.findType(returnTypeName);
				appendMethodInformation.returnsBuilder= (returnType != null) && returnType.newSupertypeHierarchy(null).contains(type);

				AppendMethodInformation oldAMI= appendMethodSpecificTypes.get(specyficType);
				if (oldAMI == null || oldAMI.methodType < appendMethodInformation.methodType) {
					appendMethodSpecificTypes.put(specyficType, appendMethodInformation);
				}
			}
		}
	} catch (JavaModelException e) {
		throw new RuntimeException("couldn't initialize custom toString() builder generator", e); //$NON-NLS-1$
	}
}