Example usage for org.eclipse.jdt.core IType getPackageFragment

List of usage examples for org.eclipse.jdt.core IType getPackageFragment

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType getPackageFragment.

Prototype

IPackageFragment getPackageFragment();

Source Link

Document

Returns the package fragment in which this element is defined.

Usage

From source file:at.bestsolution.efxclipse.tooling.fxgraph.ui.wizards.FXGraphWizardPage.java

License:Open Source License

@Override
protected void createFields(Composite parent, DataBindingContext dbc) {
    {/*from  w  ww  .ja  v a2s. c om*/
        Label l = new Label(parent, SWT.NONE);
        l.setText("Root Element");

        final ComboViewer viewer = new ComboViewer(parent);
        viewer.setLabelProvider(new LabelProvider() {
            @Override
            public String getText(Object element) {
                IType t = (IType) element;
                return t.getElementName() + " - " + t.getPackageFragment().getElementName();
            }
        });
        viewer.setContentProvider(new ArrayContentProvider());
        List<IType> types = getTypes();
        viewer.setInput(types);
        viewer.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        Button button = new Button(parent, SWT.PUSH);
        button.setText("Browse ...");
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                IType type = findContainerType();
                if (type != null) {
                    customSelection = type;
                    viewer.setInput(getTypes());
                    viewer.setSelection(new StructuredSelection(type));
                }
            }
        });

        FXGraphElement element = getClazz();
        element.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if ("fragmentRoot".equals(evt.getPropertyName())) {
                    viewer.setInput(getTypes());
                }
            }
        });
        dbc.bindValue(ViewerProperties.singleSelection().observe(viewer),
                BeanProperties.value("rootElement").observe(getClazz()));

        if (types.size() > 0) {
            viewer.setSelection(new StructuredSelection(types.get(0)));
        }
    }

    {
        Label l = new Label(parent, SWT.NONE);
        l.setText("Dynamic Root (fx:root)");

        Button b = new Button(parent, SWT.CHECK);
        dbc.bindValue(WidgetProperties.selection().observe(b),
                BeanProperties.value("dynamic").observe(getClazz()));
    }
}

From source file:at.bestsolution.efxclipse.tooling.fxml.wizards.FXMLWizardPage.java

License:Open Source License

@Override
protected void createFields(Composite parent, DataBindingContext dbc) {
    {/*from  w ww.  ja  v a2  s .c o  m*/
        Label l = new Label(parent, SWT.NONE);
        l.setText("Root Element");

        final ComboViewer viewer = new ComboViewer(parent);
        viewer.setLabelProvider(new LabelProvider() {
            @Override
            public String getText(Object element) {
                IType t = (IType) element;
                return t.getElementName() + " - " + t.getPackageFragment().getElementName();
            }
        });
        viewer.setContentProvider(new ArrayContentProvider());
        List<IType> types = getTypes();
        viewer.setInput(types);
        viewer.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        Button button = new Button(parent, SWT.PUSH);
        button.setText("Browse ...");
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                IType type = findContainerType();
                if (type != null) {
                    customSelection = type;
                    viewer.setInput(getTypes());
                    viewer.setSelection(new StructuredSelection(type));
                }
            }
        });

        FXMLElement element = getClazz();
        element.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if ("fragmentRoot".equals(evt.getPropertyName())) {
                    viewer.setInput(getTypes());
                }
            }
        });
        dbc.bindValue(ViewerProperties.singleSelection().observe(viewer),
                BeanProperties.value("rootElement").observe(getClazz()));

        if (types.size() > 0) {
            viewer.setSelection(new StructuredSelection(types.get(0)));
        }
    }

    {
        Label l = new Label(parent, SWT.NONE);
        l.setText("Dynamic Root (fx:root)");

        Button b = new Button(parent, SWT.CHECK);
        dbc.bindValue(WidgetProperties.selection().observe(b),
                BeanProperties.value("fxRoot").observe(getClazz()));
    }
}

From source file:at.bestsolution.fxide.jdt.corext.util.MethodOverrideTester.java

License:Open Source License

/**
 * Finds the method that is overridden by the given method.
 * First the super class is examined and then the implemented interfaces.
 * @param overriding the overriding method
 * @param testVisibility If true the result is tested on visibility. Null is returned if the method is not visible.
 * @return a method that is directly overridden by the given method, or <code>null</code>
 * @throws JavaModelException if a problem occurs
 *//*from ww w.jav  a  2  s . c o  m*/
public IMethod findOverriddenMethod(IMethod overriding, boolean testVisibility) throws JavaModelException {
    int flags = overriding.getFlags();
    if (Flags.isPrivate(flags) || Flags.isStatic(flags) || overriding.isConstructor()) {
        return null;
    }

    IType type = overriding.getDeclaringType();
    IType superClass = fHierarchy.getSuperclass(type);
    if (superClass != null) {
        IMethod res = findOverriddenMethodInHierarchy(superClass, overriding);
        if (res != null) {
            if (!testVisibility || JavaModelUtil.isVisibleInHierarchy(res, type.getPackageFragment())) {
                return res;
            }
        }
    }
    IType[] interfaces = fHierarchy.getSuperInterfaces(type);
    for (int i = 0; i < interfaces.length; i++) {
        IMethod res = findOverriddenMethodInHierarchy(interfaces[i], overriding);
        if (res != null) {
            return res; // methods from interfaces are always public and therefore visible
        }
    }
    return null;
}

From source file:at.bestsolution.fxide.jdt.text.viewersupport.JavaElementLabelComposer.java

License:Open Source License

/**
 * Appends the label for a type. Considers the T_* flags.
 *
 * @param type the element to render// w w w.j  a v a2s. co m
 * @param flags the rendering flags. Flags with names starting with 'T_' are considered.
 */
public void appendTypeLabel(IType type, long flags) {

    if (getFlag(flags, JavaElementLabels.T_FULLY_QUALIFIED)) {
        IPackageFragment pack = type.getPackageFragment();
        if (!pack.isDefaultPackage()) {
            appendPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }
    }
    IJavaElement parent = type.getParent();
    if (getFlag(flags, JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.T_CONTAINER_QUALIFIED)) {
        IType declaringType = type.getDeclaringType();
        if (declaringType != null) {
            appendTypeLabel(declaringType, JavaElementLabels.T_CONTAINER_QUALIFIED | (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }
        int parentType = parent.getElementType();
        if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD
                || parentType == IJavaElement.INITIALIZER) { // anonymous or local
            appendElementLabel(parent, 0);
            fBuffer.append('.');
        }
    }

    String typeName;
    boolean isAnonymous = false;
    if (type.isLambda()) {
        typeName = "() -> {...}"; //$NON-NLS-1$
        try {
            String[] superInterfaceSignatures = type.getSuperInterfaceTypeSignatures();
            if (superInterfaceSignatures.length > 0) {
                typeName = typeName + ' ' + getSimpleTypeName(type, superInterfaceSignatures[0]);
            }
        } catch (JavaModelException e) {
            //ignore
        }

    } else {
        typeName = getElementName(type);
        try {
            isAnonymous = type.isAnonymous();
        } catch (JavaModelException e1) {
            // should not happen, but let's play safe:
            isAnonymous = typeName.length() == 0;
        }
        if (isAnonymous) {
            try {
                if (parent instanceof IField && type.isEnum()) {
                    typeName = '{' + JavaElementLabels.ELLIPSIS_STRING + '}';
                } else {
                    String supertypeName = null;
                    String[] superInterfaceSignatures = type.getSuperInterfaceTypeSignatures();
                    if (superInterfaceSignatures.length > 0) {
                        supertypeName = getSimpleTypeName(type, superInterfaceSignatures[0]);
                    } else {
                        String supertypeSignature = type.getSuperclassTypeSignature();
                        if (supertypeSignature != null) {
                            supertypeName = getSimpleTypeName(type, supertypeSignature);
                        }
                    }
                    if (supertypeName == null) {
                        typeName = JavaUIMessages.JavaElementLabels_anonym;
                    } else {
                        typeName = Messages.format(JavaUIMessages.JavaElementLabels_anonym_type, supertypeName);
                    }
                }
            } catch (JavaModelException e) {
                //ignore
                typeName = JavaUIMessages.JavaElementLabels_anonym;
            }
        }
    }
    fBuffer.append(typeName);

    if (getFlag(flags, JavaElementLabels.T_TYPE_PARAMETERS)) {
        if (getFlag(flags, JavaElementLabels.USE_RESOLVED) && type.isResolved()) {
            BindingKey key = new BindingKey(type.getKey());
            if (key.isParameterizedType()) {
                String[] typeArguments = key.getTypeArguments();
                appendTypeArgumentSignaturesLabel(type, typeArguments, flags);
            } else {
                String[] typeParameters = Signature.getTypeParameters(key.toSignature());
                appendTypeParameterSignaturesLabel(typeParameters, flags);
            }
        } else if (type.exists()) {
            try {
                appendTypeParametersLabels(type.getTypeParameters(), flags);
            } catch (JavaModelException e) {
                // ignore
            }
        }
    }

    // category
    if (getFlag(flags, JavaElementLabels.T_CATEGORY) && type.exists()) {
        try {
            appendCategoryLabel(type, flags);
        } catch (JavaModelException e) {
            // ignore
        }
    }

    // post qualification
    if (getFlag(flags, JavaElementLabels.T_POST_QUALIFIED)) {
        int offset = fBuffer.length();
        fBuffer.append(JavaElementLabels.CONCAT_STRING);
        IType declaringType = type.getDeclaringType();
        if (declaringType == null && type.isBinary() && isAnonymous) {
            // workaround for Bug 87165: [model] IType#getDeclaringType() does not work for anonymous binary type
            String tqn = type.getTypeQualifiedName();
            int lastDollar = tqn.lastIndexOf('$');
            if (lastDollar != 1) {
                String declaringTypeCF = tqn.substring(0, lastDollar) + ".class"; //$NON-NLS-1$
                declaringType = type.getPackageFragment().getClassFile(declaringTypeCF).getType();
                try {
                    ISourceRange typeSourceRange = type.getSourceRange();
                    if (declaringType.exists() && SourceRange.isAvailable(typeSourceRange)) {
                        IJavaElement realParent = declaringType.getTypeRoot()
                                .getElementAt(typeSourceRange.getOffset() - 1);
                        if (realParent != null) {
                            parent = realParent;
                        }
                    }
                } catch (JavaModelException e) {
                    // ignore
                }
            }
        }
        if (declaringType != null) {
            appendTypeLabel(declaringType, JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            int parentType = parent.getElementType();
            if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD
                    || parentType == IJavaElement.INITIALIZER) { // anonymous or local
                fBuffer.append('.');
                appendElementLabel(parent, 0);
            }
        } else {
            appendPackageFragmentLabel(type.getPackageFragment(), flags & QUALIFIER_FLAGS);
        }
        //         if (getFlag(flags, JavaElementLabels.COLORIZE)) {
        //            fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
        //         }
    }
}

From source file:bndtools.utils.JavaTypeContentProposal.java

License:Open Source License

public JavaTypeContentProposal(IType element) throws JavaModelException {
    super(element.getPackageFragment().getElementName(), element.getElementName(), element.isInterface());
    this.element = element;
}

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  w  w.  j  a v  a2 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;// ww w . ja  v  a 2 s .  com
    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 ww w  .  jav a2  s  . co  m*/
    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  w  w  .  ja  v a  2  s. co  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.javasketch.internal.JavaSearchUtils.java

License:Open Source License

public static String getFullyQualifiedName(IType type, boolean includeOccurrenceCount) {
    IType declaring = type;/*w  ww  . jav  a2 s .  c om*/
    String qualifiedName = "";
    try {
        while (declaring != null) {
            if (declaring.isAnonymous()) {
                qualifiedName = declaring.getOccurrenceCount() + "" + qualifiedName;
            } else {
                qualifiedName = declaring.getElementName() + qualifiedName;
                if (includeOccurrenceCount) {
                    IJavaElement parent = declaring.getParent();
                    if (parent instanceof IMember && !(parent instanceof IType)) {
                        qualifiedName = type.getOccurrenceCount() + qualifiedName;
                    }
                }
            }
            IJavaElement parent = declaring.getParent();
            while (parent != null) {
                if (parent instanceof IType) {
                    declaring = (IType) parent;
                    break;
                }
                parent = parent.getParent();
            }
            if (parent != null) {
                qualifiedName = "$" + qualifiedName;
            } else {
                declaring = null;
            }
        }
    } catch (JavaModelException e) {
        return type.getFullyQualifiedName();
    }
    String pack = type.getPackageFragment().getElementName();
    if (!"".equals(pack))
        pack = pack + ".";
    qualifiedName = pack + qualifiedName;
    return qualifiedName;
}