Example usage for org.eclipse.jdt.core Flags isPackageDefault

List of usage examples for org.eclipse.jdt.core Flags isPackageDefault

Introduction

In this page you can find the example usage for org.eclipse.jdt.core Flags isPackageDefault.

Prototype

public static boolean isPackageDefault(int flags) 

Source Link

Document

Returns whether the given integer does not include one of the public, private, or protected flags.

Usage

From source file:at.bestsolution.efxclipse.tooling.model.internal.FXCtrlEventMethod.java

License:Open Source License

@Override
public Visibility getVisibility() {
    try {/* w w  w. ja va2 s  . co m*/
        int flags = method.getFlags();

        if (Flags.isPublic(flags)) {
            return Visibility.PUBLIC;
        } else if (Flags.isPackageDefault(flags)) {
            return Visibility.PACKAGE;
        } else if (Flags.isProtected(flags)) {
            return Visibility.PROTECTED;
        } else {
            return Visibility.PRIVATE;
        }
    } catch (JavaModelException e) {
        FXPlugin.getLogger().log(LogService.LOG_ERROR,
                "Unable to retrieve visibility for method '" + method + "'", e);
    }

    return Visibility.PRIVATE;
}

From source file:ca.ecliptical.pde.ds.search.DescriptorQueryParticipant.java

License:Open Source License

private IMethod findActivateMethod(IType implClassType, String name, IProgressMonitor monitor)
        throws JavaModelException {
    IMethod candidate = null;//from   w  ww. j a  v a 2 s .  c  o  m
    int priority = Integer.MAX_VALUE;

    IType type = implClassType;
    while (type != null) {
        for (IMethod method : type.getMethods()) {
            if (!name.equals(method.getElementName()))
                continue;

            if (!VOID_SIG.equals(method.getReturnType()))
                continue;

            if (type != implClassType && (Flags.isPrivate(method.getFlags())
                    || (Flags.isPackageDefault(method.getFlags()) && !implClassType.getPackageFragment()
                            .equals(method.getDeclaringType().getPackageFragment()))))
                continue;

            String[] paramSigs = resolveParameterTypes(method, Integer.MAX_VALUE);

            if (paramSigs.length == 1 && COMPONENT_CONTEXT_SIG.equals(paramSigs[0])) {
                // best match
                return method;
            }

            if (priority > 1 && paramSigs.length == 1 && COMPONENT_CONTEXT_SIG.equals(paramSigs[0])) {
                candidate = method;
                priority = 1;
                continue;
            }

            if (priority > 2 && paramSigs.length == 1 && MAP_SIG.equals(paramSigs[0])) {
                candidate = method;
                priority = 2;
                continue;
            }

            if (priority > 3 && paramSigs.length >= 2) {
                boolean valid = true;
                for (String paramSig : paramSigs) {
                    if (!COMPONENT_CONTEXT_SIG.equals(paramSig) && !BUNDLE_CONTEXT_SIG.equals(paramSig)
                            && !MAP_SIG.equals(paramSig)) {
                        valid = false;
                        break;
                    }
                }

                if (valid) {
                    candidate = method;
                    priority = 3;
                }

                continue;
            }

            if (priority > 4 && paramSigs.length == 0) {
                candidate = method;
                priority = 4;
                continue;
            }
        }

        type = findSuperclassType(type, implClassType.getJavaProject(), monitor);
    }

    return candidate;
}

From source file:ca.ecliptical.pde.ds.search.DescriptorQueryParticipant.java

License:Open Source License

private IMethod findDeactivateMethod(IType implClassType, String name, IProgressMonitor monitor)
        throws JavaModelException {
    IMethod candidate = null;//  w  w w.  jav  a  2 s.  c  o m
    int priority = Integer.MAX_VALUE;

    IType type = implClassType;
    while (type != null) {
        for (IMethod method : type.getMethods()) {
            if (!name.equals(method.getElementName()))
                continue;

            if (!VOID_SIG.equals(method.getReturnType()))
                continue;

            if (type != implClassType && (Flags.isPrivate(method.getFlags())
                    || (Flags.isPackageDefault(method.getFlags()) && !implClassType.getPackageFragment()
                            .equals(method.getDeclaringType().getPackageFragment()))))
                continue;

            String[] paramSigs = resolveParameterTypes(method, Integer.MAX_VALUE);

            if (paramSigs.length == 1 && COMPONENT_CONTEXT_SIG.equals(paramSigs[0])) {
                // best match
                return method;
            }

            if (priority > 1 && paramSigs.length == 1 && COMPONENT_CONTEXT_SIG.equals(paramSigs[0])) {
                candidate = method;
                priority = 1;
                continue;
            }

            if (priority > 2 && paramSigs.length == 1 && MAP_SIG.equals(paramSigs[0])) {
                candidate = method;
                priority = 2;
                continue;
            }

            if (priority > 3 && paramSigs.length == 1 && INT_SIG.equals(paramSigs[0])) {
                candidate = method;
                priority = 3;
                continue;
            }

            if (priority > 4 && paramSigs.length == 1 && INTEGER_SIG.equals(paramSigs[0])) {
                candidate = method;
                priority = 4;
                continue;
            }

            if (priority > 5 && paramSigs.length >= 2) {
                boolean valid = true;
                for (String paramSig : paramSigs) {
                    if (!COMPONENT_CONTEXT_SIG.equals(paramSig) && !BUNDLE_CONTEXT_SIG.equals(paramSig)
                            && !MAP_SIG.equals(paramSig) && !INT_SIG.equals(paramSig)
                            && !INTEGER_SIG.equals(paramSig)) {
                        valid = false;
                        break;
                    }
                }

                if (valid) {
                    candidate = method;
                    priority = 5;
                }

                continue;
            }

            if (priority > 6 && paramSigs.length == 0) {
                candidate = method;
                priority = 6;
                continue;
            }
        }

        type = findSuperclassType(type, implClassType.getJavaProject(), monitor);
    }

    return candidate;
}

From source file:ca.ecliptical.pde.ds.search.DescriptorQueryParticipant.java

License:Open Source License

private IMethod findBindMethod(IType implClassType, String name, String referenceTypeName,
        IProgressMonitor monitor) throws JavaModelException {
    IMethod candidate = null;/*from   w w  w.j  av a2s  . com*/
    int priority = Integer.MAX_VALUE;

    String referenceTypeSig = Signature.createTypeSignature(referenceTypeName, true);
    IType referenceType = null;
    IType arg0Type = null;

    IType type = implClassType;
    while (type != null) {
        for (IMethod method : type.getMethods()) {
            if (!name.equals(method.getElementName()))
                continue;

            if (!VOID_SIG.equals(method.getReturnType()))
                continue;

            if (type != implClassType && (Flags.isPrivate(method.getFlags())
                    || (Flags.isPackageDefault(method.getFlags()) && !implClassType.getPackageFragment()
                            .equals(method.getDeclaringType().getPackageFragment()))))
                continue;

            String[] paramSigs = resolveParameterTypes(method, 2);

            if (paramSigs.length == 1 && SERVICE_REFERENCE_SIG.equals(paramSigs[0])) {
                // best match
                return method;
            }

            if (priority > 1 && paramSigs.length == 1 && referenceTypeSig.equals(paramSigs[0])) {
                candidate = method;
                priority = 1;
                continue;
            }

            if (priority > 2 && paramSigs.length == 1) {
                if (referenceType == null)
                    referenceType = implClassType.getJavaProject().findType(referenceTypeName, monitor);

                if (arg0Type == null)
                    arg0Type = implClassType.getJavaProject().findType(Signature.toString(paramSigs[0]),
                            monitor);

                if (isAssignableFrom(arg0Type, referenceType, monitor)) {
                    candidate = method;
                    priority = 2;
                }

                continue;
            }

            if (priority > 3 && paramSigs.length == 2 && referenceTypeSig.equals(paramSigs[0])
                    && MAP_SIG.equals(paramSigs[1])) {
                candidate = method;
                priority = 3;
                continue;
            }

            if (priority > 4 && paramSigs.length == 2 && MAP_SIG.equals(paramSigs[1])) {
                if (referenceType == null)
                    referenceType = implClassType.getJavaProject().findType(referenceTypeName, monitor);

                if (arg0Type == null)
                    arg0Type = implClassType.getJavaProject().findType(Signature.toString(paramSigs[0]),
                            monitor);

                if (isAssignableFrom(arg0Type, referenceType, monitor)) {
                    candidate = method;
                    priority = 4;
                }

                continue;
            }
        }

        type = findSuperclassType(type, implClassType.getJavaProject(), monitor);
    }

    return candidate;
}

From source file:ca.ecliptical.pde.ds.search.DescriptorQueryParticipant.java

License:Open Source License

private IMethod findUpdatedMethod(IType implClassType, String name, IProgressMonitor monitor)
        throws JavaModelException {
    IMethod candidate = null;/*from   w  ww.  j  av  a  2  s . c o m*/

    IType type = implClassType;
    while (type != null) {
        for (IMethod method : type.getMethods()) {
            if (!name.equals(method.getElementName()))
                continue;

            if (!VOID_SIG.equals(method.getReturnType()))
                continue;

            if (type != implClassType && (Flags.isPrivate(method.getFlags())
                    || (Flags.isPackageDefault(method.getFlags()) && !implClassType.getPackageFragment()
                            .equals(method.getDeclaringType().getPackageFragment()))))
                continue;

            String[] paramSigs = resolveParameterTypes(method, 1);

            if (paramSigs.length != 1)
                continue;

            if (SERVICE_REFERENCE_SIG.equals(paramSigs[0])) {
                // best match
                return method;
            }

            if (candidate == null && MAP_SIG.equals(paramSigs[0])) {
                candidate = method;
            }
        }

        type = findSuperclassType(type, implClassType.getJavaProject(), monitor);
    }

    return candidate;
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.editors.JavaSequenceLabelProvider.java

License:Open Source License

public Color getBackground(Object element) {
    if (element instanceof IAdaptable) {
        IJavaElement javaElement = (IJavaElement) ((IAdaptable) element).getAdapter(IJavaElement.class);
        if (javaElement instanceof IMember) {
            try {
                int flags = ((IMember) javaElement).getFlags();
                if (Flags.isAbstract(flags) || Flags.isInterface(flags)) {
                    return Display.getCurrent().getSystemColor(SWT.COLOR_GRAY);
                }//from  w w  w.  j a  v  a2 s  .  com
                if (javaElement instanceof IMethod) {
                    if (Flags.isPrivate(flags)) {
                        return ISketchColorConstants.PRIVATE_BG;
                    } else if (Flags.isProtected(flags)) {
                        return ISketchColorConstants.PROTECTED_BG;
                    } else if (Flags.isPackageDefault(flags)) {
                        return ISketchColorConstants.LIGHT_BLUE;
                    } else if (Flags.isPublic(flags)) {
                        return ISketchColorConstants.PUBLIC_BG;
                    }
                }
            } catch (JavaModelException e) {
                //just return null
            }
        } else if (javaElement instanceof IPackageFragment) {
            return ISketchColorConstants.PRIVATE_BG;
        }
    }
    return null;
}

From source file:com.google.gwt.eclipse.core.uibinder.contentassist.ElExpressionFirstFragmentComputer.java

License:Open Source License

/**
 * Computes possible first fragments for EL expressions.
 * /*from w  w  w.  j  av  a 2s.c  om*/
 * @param document The document to compute the first fragments from
 * @param xmlFile The xml file the document came from. This is used to
 *          determine the java files that references this ui.xml file so that
 *          "package" of this ui.xml file can be determined for importing
 *          protected or package default fields. May be null, but protected /
 *          package default fields can't be checked for visibility.
 * @param javaProject The java project the document belongs to.
 * @param problemMarkerManager The problem marker manage to mark problems as
 *          the fragments are identified. May be null.
 * @return An ElExpressionFirstFragmentComputer that contains first fragments
 *         and duplicate first fragments.
 */
public static ElExpressionFirstFragmentComputer compute(Document document, final IFile xmlFile,
        final IJavaProject javaProject, final UiBinderProblemMarkerManager problemMarkerManager) {
    final Set<ElExpressionFirstFragment> fragments = new HashSet<ElExpressionFirstFragment>();
    final Set<ElExpressionFirstFragment> dupFragments = new HashSet<ElExpressionFirstFragment>();

    XmlUtilities.visitNodes(document, new NodeVisitor() {

        public boolean visitNode(Node node) {
            if (node.getNodeType() != Node.ELEMENT_NODE) {
                return true;
            }

            List<ElExpressionFirstFragment> currentFirstFragments = new ArrayList<ElExpressionFirstFragment>();

            if (!UiBinderXmlModelUtilities.isImportElement(node)) {
                ElExpressionFirstFragment elExpressionFirstFragment = createFirstFragmentWithFieldAttribute(
                        node);
                if (elExpressionFirstFragment == null) {
                    if (UiBinderXmlModelUtilities.isStyleElement(node)) {
                        elExpressionFirstFragment = new ElExpressionFirstFragment(
                                UiBinderConstants.UI_BINDER_STYLE_ELEMENT_IMPLICIT_FIELD_VALUE, node);
                    }
                }

                if (elExpressionFirstFragment != null) {
                    currentFirstFragments.add(elExpressionFirstFragment);
                }
            } else {
                List<ElExpressionFirstFragment> imports = getFirstFragmentsFromImport(node);
                currentFirstFragments.addAll(imports);
            }

            for (ElExpressionFirstFragment elExpressionFirstFragment : currentFirstFragments) {
                ElExpressionFirstFragment fragment = findFragment(elExpressionFirstFragment.getValue());
                if (fragment != null) {
                    /**
                     * The original is placed in fragments and dupFragments to reduce
                     * popping up misleading problems on {style.blah} where it would
                     * complain the fragment blah is not found.
                     */
                    dupFragments.add(fragment);
                    dupFragments.add(elExpressionFirstFragment);
                } else {
                    fragments.add(elExpressionFirstFragment);
                }
            }

            return true;
        }

        private ImportResult canImportField(IField field) {

            try {
                int flags = field.getFlags();

                if (!Flags.isStatic(flags)) {

                    // if it's an enum, then it doesn't have any of these modifiers, but
                    // can still be imported
                    if (Flags.isEnum(flags)) {
                        return ImportResult.YES;
                    } else {
                        // must be static
                        return ImportResult.FIELD_NOT_STATIC;
                    }

                } else if (Flags.isPrivate(flags)) {

                    return ImportResult.FIELD_NOT_VISIBLE;

                } else if (Flags.isProtected(flags) || Flags.isPackageDefault(flags)) {
                    // if it's protected / package default, packages must be the same
                    IPackageDeclaration[] decs = field.getCompilationUnit().getPackageDeclarations();
                    if (decs.length > 0 && xmlFile != null) {
                        String enclosingTypeDec = decs[0].getElementName();
                        Set<IType> ownerTypes = UiBinderUtilities.getSubtypesFromXml(xmlFile, javaProject);
                        for (IType type : ownerTypes) {
                            if (!enclosingTypeDec.equals(type.getPackageFragment().getElementName())) {
                                return ImportResult.FIELD_NOT_VISIBLE;
                            }
                        }

                    } else {
                        // default package
                        return ImportResult.TYPE_NOT_VISIBLE;
                    }
                }
            } catch (JavaModelException e) {
                return ImportResult.ERROR;
            }

            return ImportResult.YES;
        }

        /**
         * Checks if t1 is accessible from t2 for the purposes of UiBinder. In the
         * case of nested inner classes, t1 and all of its enclosing classes are
         * recursively checked for accessibility from t2, and the first
         * inaccessible type is returned.
         * 
         * @param t1 The target type to check for accessibility
         * @param t2 The source type to check for accessibility from
         * @return null if t1 is accessible from t2, or the first type found that
         *         is inaccessible
         */
        private IType computeFirstInaccessibleType(IType t1, IType t2) {

            int flags;
            try {
                flags = t1.getFlags();
            } catch (JavaModelException e) {
                return t1;
            }

            if (Flags.isPrivate(flags)) {
                return t1;
            } else if (Flags.isProtected(flags) || Flags.isPackageDefault(flags)) {
                // check if the type's packages are the same if t1 is protected or
                // package default.
                // don't need to worry about inheritance with the protected case
                // because UiBinder subtypes
                // can't inherit from arbitrary classes.

                boolean samePackage = t1.getPackageFragment().getElementName()
                        .equals(t2.getPackageFragment().getElementName());

                if (!samePackage) {
                    return t1;
                }
            }

            // don't need to check public because we're going to recurse up into
            // possible container classes until we hit null, which means we haven't
            // any problems (ie, public is never a problem)

            if (t1.getParent() instanceof IType) {
                return computeFirstInaccessibleType((IType) t1.getParent(), t2);
            } else {
                return null;
            }
        }

        private ElExpressionFirstFragment createFirstFragmentWithFieldAttribute(Node node) {

            Node fieldAttr = UiBinderXmlModelUtilities.getFieldAttribute(node);
            if (fieldAttr == null || fieldAttr.getNodeValue() == null) {
                return null;
            }

            ElExpressionFirstFragment elExpressionFirstFragment = new ElExpressionFirstFragment(
                    fieldAttr.getNodeValue(), node);
            return elExpressionFirstFragment;
        }

        private ElExpressionFirstFragment findFragment(String fragmentStr) {
            for (ElExpressionFirstFragment curFragment : fragments) {
                if (curFragment.getValue().equals(fragmentStr)) {
                    return curFragment;
                }
            }

            return null;
        }

        private List<ElExpressionFirstFragment> getFirstFragmentsFromImport(Node node) {

            Node importFieldNode = UiBinderXmlModelUtilities.getFieldAttribute(node);
            if (importFieldNode == null) {
                markProblem(node, ImportResult.IMPORT_MISSING_FIELD_ATTR);
                return Collections.emptyList();
            }

            String importFieldValue = importFieldNode.getNodeValue();
            String lastFragment = importFieldValue.substring(importFieldValue.lastIndexOf('.') + 1);

            // didn't enter anything!
            if (lastFragment.length() == 0) {
                markProblem(importFieldNode, ImportResult.TYPE_UNDEFINED);
                return Collections.emptyList();
            }

            // the class from which we're importing fields
            IType enclosingType = UiBinderXmlModelUtilities.resolveElementToJavaType((IDOMElement) node,
                    javaProject);

            // make sure the enclosing type exists
            if (enclosingType == null) {
                markProblem(importFieldNode, ImportResult.TYPE_UNDEFINED);
                return Collections.emptyList();
            }

            if (xmlFile != null) {
                boolean problemFound = false;
                // make sure the subtype can access the enclosing type
                Set<IType> subtypes = UiBinderUtilities.getSubtypesFromXml(xmlFile, javaProject);
                for (IType type : subtypes) {
                    IType inaccessibleType = computeFirstInaccessibleType(enclosingType, type);
                    if (inaccessibleType != null) {
                        markProblem(importFieldNode, ImportResult.TYPE_NOT_VISIBLE,
                                inaccessibleType.getFullyQualifiedName('.'));
                        problemFound = true;
                    }
                }

                if (problemFound) {
                    return Collections.emptyList();
                }
            }

            List<ElExpressionFirstFragment> fragments = new ArrayList<ElExpressionFirstFragment>();

            IField[] fields;
            try {
                fields = enclosingType.getFields();
            } catch (JavaModelException e) {
                GWTPluginLog.logError(e);
                return Collections.emptyList();
            }

            // import a glob, import everything we can from the type
            if ("*".equals(lastFragment)) {
                for (IField field : fields) {
                    if (canImportField(field) == ImportResult.YES) {
                        ElExpressionFirstFragment elExpressionFirstFragment = new ElExpressionFirstFragment(
                                field.getElementName(), node);
                        fragments.add(elExpressionFirstFragment);
                    }
                }

            } else { // import a single field

                IField field = JavaModelSearch.findField(enclosingType, lastFragment);
                if (field != null) {

                    ImportResult ir;
                    if ((ir = canImportField(field)) == ImportResult.YES) {

                        ElExpressionFirstFragment elExpressionFirstFragment = new ElExpressionFirstFragment(
                                lastFragment, node);
                        fragments.add(elExpressionFirstFragment);

                    } else {
                        markProblem(importFieldNode, ir);
                    }
                } else {
                    markProblem(importFieldNode, ImportResult.FIELD_UNDEFINED);
                }
            }
            return fragments;
        }

        private void markProblem(Node node, ImportResult ir) {
            markProblem(node, ir, null);
        }

        private void markProblem(Node node, ImportResult ir, String text) {
            if (problemMarkerManager != null) {

                if (text == null) {
                    text = node.getNodeValue();
                }

                IRegion region;
                if (node instanceof IDOMAttr) {
                    region = XmlUtilities.getAttributeValueRegion((IDOMAttr) node);
                } else if (node instanceof IDOMElement) {
                    int start = ((IDOMElement) node).getStartOffset();
                    int end = ((IDOMElement) node).getStartEndOffset();
                    region = new Region(start, end - start);
                } else {
                    return;
                }

                switch (ir) {
                case FIELD_NOT_STATIC:
                    problemMarkerManager.setFieldNotStaticError(region, text);
                    break;
                case FIELD_NOT_VISIBLE:
                    problemMarkerManager.setFieldNotVisibleError(region, text);
                    break;
                case FIELD_UNDEFINED:
                    problemMarkerManager.setFieldUndefinedError(region, text);
                    break;
                case TYPE_NOT_VISIBLE:
                    problemMarkerManager.setTypeNotVisibleError(region, text);
                    break;
                case TYPE_UNDEFINED:
                    problemMarkerManager.setTypeUndefinedError(region, text);
                    break;
                case IMPORT_MISSING_FIELD_ATTR:
                    problemMarkerManager.setImportMissingFieldAttr(region, text);
                    break;
                default:
                    GWTPluginLog.logWarning("Unknown UiBinder XML problem type: " + ir.toString());
                    break;
                }
            }
        }
    });

    return new ElExpressionFirstFragmentComputer(fragments, dupFragments);
}

From source file:fr.obeo.ariadne.ide.connector.java.internal.explorer.JavaExplorer.java

License:Open Source License

/**
 * Returns the visibility of the given member. If it is located within an interface, the lack of
 * visibility should be treated as public and not package.
 * // w ww .j  av a  2s. c om
 * @param iMember
 *            The member
 * @param isInInterface
 *            Indicates if the given member is located in an interface
 * @return The visibility of the given member
 */
private VisibilityKind getVisibility(IMember iMember, boolean isInInterface) {
    VisibilityKind visibility = VisibilityKind.PUBLIC;
    try {
        int flags = iMember.getFlags();
        if (Flags.isPublic(flags)) {
            visibility = VisibilityKind.PUBLIC;
        } else if (Flags.isPackageDefault(flags)) {
            if (isInInterface) {
                visibility = VisibilityKind.PUBLIC;
            } else {
                visibility = VisibilityKind.PACKAGE;
            }
        } else if (Flags.isProtected(flags)) {
            visibility = VisibilityKind.PROTECTED;
        } else if (Flags.isPrivate(flags)) {
            visibility = VisibilityKind.PRIVATE;
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    return visibility;
}

From source file:org.dyno.visual.swing.parser.adapters.WidgetParser.java

License:Open Source License

private int getAccessModifier(int flags) {
    int access_code = ACCESS_PRIVATE;
    if (Flags.isPublic(flags)) {
        access_code = ACCESS_PUBLIC;/*from w  ww .  j  a  v  a 2 s. c om*/
    } else if (Flags.isProtected(flags)) {
        access_code = ACCESS_PROTECTED;
    } else if (Flags.isPackageDefault(flags)) {
        access_code = ACCESS_DEFAULT;
    } else if (Flags.isPrivate(flags)) {
        access_code = ACCESS_PRIVATE;
    }
    return access_code;
}

From source file:org.eclipse.fx.ide.model.internal.FXCtrlEventMethod.java

License:Open Source License

@Override
public Visibility getVisibility() {
    try {//ww w  . j av  a  2  s  .c o  m
        int flags = this.method.getFlags();

        if (Flags.isPublic(flags)) {
            return Visibility.PUBLIC;
        } else if (Flags.isPackageDefault(flags)) {
            return Visibility.PACKAGE;
        } else if (Flags.isProtected(flags)) {
            return Visibility.PROTECTED;
        } else {
            return Visibility.PRIVATE;
        }
    } catch (JavaModelException e) {
        FXPlugin.getLogger().log(LogService.LOG_ERROR,
                "Unable to retrieve visibility for method '" + method + "'", e);
    }

    return Visibility.PRIVATE;
}