Example usage for org.eclipse.jdt.core ITypeHierarchy getSuperclass

List of usage examples for org.eclipse.jdt.core ITypeHierarchy getSuperclass

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ITypeHierarchy getSuperclass.

Prototype

IType getSuperclass(IType type);

Source Link

Document

Returns the resolved superclass of the given class, or null if the given class has no superclass, the superclass could not be resolved, or if the given type is an interface.

Usage

From source file:ar.com.fluxit.jqa.wizard.page.ThrowingDefinitionWizardPage.java

License:Open Source License

@Override
public void createControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NULL);
    GridLayout layout = new GridLayout();
    layout.numColumns = 1;//from  w  w w.j  ava2 s. c om
    container.setLayout(layout);

    layersTable = new TableViewer(container, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    layersTable.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
    layersTable.setContentProvider(ArrayContentProvider.getInstance());
    layersTable.getTable().setHeaderVisible(true);
    layersTable.getTable().setLinesVisible(true);

    TableViewerColumn layerColumn = new TableViewerColumn(layersTable, SWT.NONE);
    layerColumn.getColumn().setText("Layer");
    layerColumn.getColumn().setWidth(300);
    layerColumn.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            LayerDescriptor layer = (LayerDescriptor) element;
            return layer.getName();
        }
    });

    TableViewerColumn namePatternColumn = new TableViewerColumn(layersTable, SWT.NONE);
    namePatternColumn.getColumn().setWidth(300);
    namePatternColumn.getColumn().setText("Exception super type");
    namePatternColumn.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            LayerDescriptor layer = (LayerDescriptor) element;
            return layer.getExceptionSuperType();
        }

    });
    namePatternColumn.setEditingSupport(new EditingSupport(layersTable) {

        @Override
        protected boolean canEdit(Object element) {
            return true;
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            TypeSelectionExtension extension = new TypeSelectionExtension() {

                @Override
                public ISelectionStatusValidator getSelectionValidator() {
                    return new ISelectionStatusValidator() {
                        @Override
                        public IStatus validate(Object[] selection) {
                            if (selection.length == 1) {
                                try {
                                    IType type = (IType) selection[0];
                                    ITypeHierarchy hierarchy = type
                                            .newSupertypeHierarchy(new NullProgressMonitor());
                                    IType curr = type;
                                    while (curr != null) {
                                        if ("java.lang.Throwable".equals(curr.getFullyQualifiedName('.'))) { //$NON-NLS-1$
                                            return Status.OK_STATUS;
                                        }
                                        curr = hierarchy.getSuperclass(curr);
                                    }
                                } catch (JavaModelException e) {
                                    Status status = new Status(IStatus.ERROR, JQAEclipsePlugin.PLUGIN_ID,
                                            e.getLocalizedMessage(), e);
                                    JQAEclipsePlugin.getDefault().getLog().log(status);
                                    return Status.CANCEL_STATUS;
                                }
                            }
                            return new Status(IStatus.ERROR, JQAEclipsePlugin.PLUGIN_ID,
                                    "Selected item is not an exception");
                        }

                    };
                }

            };
            return new TypeCellEditor(layersTable.getTable(), ThrowingDefinitionWizardPage.this.getContainer(),
                    IJavaSearchConstants.CLASS, "*Exception", extension);
        }

        @Override
        protected Object getValue(Object element) {
            return ((LayerDescriptor) element).getExceptionSuperType();
        }

        @Override
        protected void setValue(Object element, Object value) {
            ((LayerDescriptor) element).setExceptionSuperType((String) value);
            layersTable.refresh(element, true);
        }
    });
    layersTable.setInput(getArchitectureDescriptor().getLayers());
    setControl(container);
    ((WizardDialog) getContainer()).addPageChangedListener(this);
}

From source file:ca.mt.wb.devtools.tx.ComparisonModel.java

License:unlicense.org

public IType getSuperclassFor(IType t) {
    for (ITypeHierarchy h : added.values()) {
        IType sup = h.getSuperclass(t);
        if (sup != null) {
            // don't include if explicitly deleted
            return isHidden(sup) ? null : sup;
        }/*from w w  w . j  a  v a  2  s.  c o m*/
    }
    return null;
}

From source file:com.android.ide.eclipse.adt.internal.editors.layout.descriptors.CustomViewDescriptorService.java

License:Open Source License

/**
 * Returns the {@link ViewElementDescriptor} for a particular project/class when the
 * fully qualified class name actually matches a class from the given project.
 * <p/>//www  . j a  v a 2 s  . c om
 * Custom descriptors are created as needed.
 * <p/>
 * If it is the first time the {@link ViewElementDescriptor} is requested, the method
 * will check that the specified class is in fact a custom View class. Once this is
 * established, a monitoring for that particular class is initiated. Any change will
 * trigger a notification to the {@link ICustomViewDescriptorListener}.
 *
 * @param project the project containing the class.
 * @param fqcn the fully qualified name of the class.
 * @return a {@link ViewElementDescriptor} or <code>null</code> if the class was not
 *         a custom View class.
 */
public ViewElementDescriptor getDescriptor(IProject project, String fqcn) {
    // look in the map first
    synchronized (mCustomDescriptorMap) {
        HashMap<String, ViewElementDescriptor> map = mCustomDescriptorMap.get(project);

        if (map != null) {
            ViewElementDescriptor descriptor = map.get(fqcn);
            if (descriptor != null) {
                return descriptor;
            }
        }

        // if we step here, it looks like we haven't created it yet.
        // First lets check this is in fact a valid type in the project

        try {
            // We expect the project to be both opened and of java type (since it's an android
            // project), so we can create a IJavaProject object from our IProject.
            IJavaProject javaProject = JavaCore.create(project);

            // replace $ by . in the class name
            String javaClassName = fqcn.replaceAll("\\$", "\\."); //$NON-NLS-1$ //$NON-NLS-2$

            // look for the IType object for this class
            IType type = javaProject.findType(javaClassName);
            if (type != null && type.exists()) {
                // the type exists. Let's get the parent class and its ViewClassInfo.

                // get the type hierarchy
                ITypeHierarchy hierarchy = type.newSupertypeHierarchy(new NullProgressMonitor());

                ViewElementDescriptor parentDescriptor = createViewDescriptor(hierarchy.getSuperclass(type),
                        project, hierarchy);

                if (parentDescriptor != null) {
                    // we have a valid parent, lets create a new ViewElementDescriptor.
                    List<AttributeDescriptor> attrList = new ArrayList<AttributeDescriptor>();
                    List<AttributeDescriptor> paramList = new ArrayList<AttributeDescriptor>();
                    Map<ResourceFile, Long> files = findCustomDescriptors(project, type, attrList, paramList);

                    AttributeDescriptor[] attributes = getAttributeDescriptor(type, parentDescriptor);
                    if (!attrList.isEmpty()) {
                        attributes = join(attrList, attributes);
                    }
                    AttributeDescriptor[] layoutAttributes = getLayoutAttributeDescriptors(type,
                            parentDescriptor);
                    if (!paramList.isEmpty()) {
                        layoutAttributes = join(paramList, layoutAttributes);
                    }
                    String name = DescriptorsUtils.getBasename(fqcn);
                    ViewElementDescriptor descriptor = new CustomViewDescriptor(name, fqcn, attributes,
                            layoutAttributes, parentDescriptor.getChildren(), project, files);
                    descriptor.setSuperClass(parentDescriptor);

                    synchronized (mCustomDescriptorMap) {
                        map = mCustomDescriptorMap.get(project);
                        if (map == null) {
                            map = new HashMap<String, ViewElementDescriptor>();
                            mCustomDescriptorMap.put(project, map);
                        }

                        map.put(fqcn, descriptor);
                    }

                    //TODO setup listener on this resource change.

                    return descriptor;
                }
            }
        } catch (JavaModelException e) {
            // there was an error accessing any of the IType, we'll just return null;
        }
    }

    return null;
}

From source file:com.android.ide.eclipse.adt.internal.editors.layout.descriptors.CustomViewDescriptorService.java

License:Open Source License

/**
 * Computes (if needed) and returns the {@link ViewElementDescriptor} for the specified type.
 *
 * @return A {@link ViewElementDescriptor} or null if type or typeHierarchy is null.
 *///from  w  w w .ja  v a2  s.c  o m
private ViewElementDescriptor createViewDescriptor(IType type, IProject project, ITypeHierarchy typeHierarchy) {
    // check if the type is a built-in View class.
    List<ViewElementDescriptor> builtInList = null;

    // give up if there's no type
    if (type == null) {
        return null;
    }

    String fqcn = type.getFullyQualifiedName();

    Sdk currentSdk = Sdk.getCurrent();
    if (currentSdk != null) {
        IAndroidTarget target = currentSdk.getTarget(project);
        if (target != null) {
            AndroidTargetData data = currentSdk.getTargetData(target);
            if (data != null) {
                LayoutDescriptors descriptors = data.getLayoutDescriptors();
                ViewElementDescriptor d = descriptors.findDescriptorByClass(fqcn);
                if (d != null) {
                    return d;
                }
                builtInList = descriptors.getViewDescriptors();
            }
        }
    }

    // it's not a built-in class? Lets look if the superclass is built-in
    // give up if there's no type
    if (typeHierarchy == null) {
        return null;
    }

    IType parentType = typeHierarchy.getSuperclass(type);
    if (parentType != null) {
        ViewElementDescriptor parentDescriptor = createViewDescriptor(parentType, project, typeHierarchy);

        if (parentDescriptor != null) {
            // parent class is a valid View class with a descriptor, so we create one
            // for this class.
            String name = DescriptorsUtils.getBasename(fqcn);
            // A custom view accepts children if its parent descriptor also does.
            // The only exception to this is ViewGroup, which accepts children even though
            // its parent does not.
            boolean isViewGroup = fqcn.equals(CLASS_VIEWGROUP);
            boolean hasChildren = isViewGroup || parentDescriptor.hasChildren();
            ViewElementDescriptor[] children = null;
            if (hasChildren && builtInList != null) {
                // We can't figure out what the allowable children are by just
                // looking at the class, so assume any View is valid
                children = builtInList.toArray(new ViewElementDescriptor[builtInList.size()]);
            }
            ViewElementDescriptor descriptor = new CustomViewDescriptor(name, fqcn,
                    getAttributeDescriptor(type, parentDescriptor),
                    getLayoutAttributeDescriptors(type, parentDescriptor), children, project, null);
            descriptor.setSuperClass(parentDescriptor);

            // add it to the map
            synchronized (mCustomDescriptorMap) {
                HashMap<String, ViewElementDescriptor> map = mCustomDescriptorMap.get(project);

                if (map == null) {
                    map = new HashMap<String, ViewElementDescriptor>();
                    mCustomDescriptorMap.put(project, map);
                }

                map.put(fqcn, descriptor);

            }

            //TODO setup listener on this resource change.

            return descriptor;
        }
    }

    // class is neither a built-in view class, nor extend one. return null.
    return null;
}

From source file:com.android.ide.eclipse.adt.internal.lint.EclipseLintClient.java

License:Open Source License

@Override
@Nullable/*from  w  ww.j  a  v  a2s. co  m*/
public String getSuperClass(@NonNull Project project, @NonNull String name) {
    if (!mSearchForSuperClasses) {
        // Super type search using the Eclipse index is potentially slow, so
        // only do this when necessary
        return null;
    }

    IProject eclipseProject = getProject(project);
    if (eclipseProject == null) {
        return null;
    }

    try {
        IJavaProject javaProject = BaseProjectHelper.getJavaProject(eclipseProject);
        if (javaProject == null) {
            return null;
        }

        String typeFqcn = ClassContext.getFqcn(name);
        IType type = javaProject.findType(typeFqcn);
        if (type != null) {
            ITypeHierarchy hierarchy = type.newSupertypeHierarchy(new NullProgressMonitor());
            IType superType = hierarchy.getSuperclass(type);
            if (superType != null) {
                String key = superType.getKey();
                if (!key.isEmpty() && key.charAt(0) == 'L' && key.charAt(key.length() - 1) == ';') {
                    return key.substring(1, key.length() - 1);
                } else {
                    String fqcn = superType.getFullyQualifiedName();
                    return ClassContext.getInternalName(fqcn);
                }
            }
        }
    } catch (JavaModelException e) {
        log(Severity.INFORMATIONAL, e, null);
    } catch (CoreException e) {
        log(Severity.INFORMATIONAL, e, null);
    }

    return null;
}

From source file:com.android.ide.eclipse.adt.internal.project.BaseProjectHelper.java

License:Open Source License

/**
 * Tests that a class name is valid for usage in the manifest.
 * <p/>// w  w w . j  a  va 2 s . c o m
 * This tests the class existence, that it can be instantiated (ie it must not be abstract,
 * nor non static if enclosed), and that it extends the proper super class (not necessarily
 * directly)
 * @param javaProject the {@link IJavaProject} containing the class.
 * @param className the fully qualified name of the class to test.
 * @param superClassName the fully qualified name of the expected super class.
 * @param testVisibility if <code>true</code>, the method will check the visibility of the class
 * or of its constructors.
 * @return {@link #TEST_CLASS_OK} or an error message.
 */
public final static String testClassForManifest(IJavaProject javaProject, String className,
        String superClassName, boolean testVisibility) {
    try {
        // replace $ by .
        String javaClassName = className.replaceAll("\\$", "\\."); //$NON-NLS-1$ //$NON-NLS-2$

        // look for the IType object for this class
        IType type = javaProject.findType(javaClassName);
        if (type != null && type.exists()) {
            // test that the class is not abstract
            int flags = type.getFlags();
            if (Flags.isAbstract(flags)) {
                return String.format("%1$s is abstract", className);
            }

            // test whether the class is public or not.
            if (testVisibility && Flags.isPublic(flags) == false) {
                // if its not public, it may have a public default constructor,
                // which would then be fine.
                IMethod basicConstructor = type.getMethod(type.getElementName(), new String[0]);
                if (basicConstructor != null && basicConstructor.exists()) {
                    int constructFlags = basicConstructor.getFlags();
                    if (Flags.isPublic(constructFlags) == false) {
                        return String.format(
                                "%1$s or its default constructor must be public for the system to be able to instantiate it",
                                className);
                    }
                } else {
                    return String.format(
                            "%1$s must be public, or the system will not be able to instantiate it.",
                            className);
                }
            }

            // If it's enclosed, test that it's static. If its declaring class is enclosed
            // as well, test that it is also static, and public.
            IType declaringType = type;
            do {
                IType tmpType = declaringType.getDeclaringType();
                if (tmpType != null) {
                    if (tmpType.exists()) {
                        flags = declaringType.getFlags();
                        if (Flags.isStatic(flags) == false) {
                            return String.format("%1$s is enclosed, but not static",
                                    declaringType.getFullyQualifiedName());
                        }

                        flags = tmpType.getFlags();
                        if (testVisibility && Flags.isPublic(flags) == false) {
                            return String.format("%1$s is not public", tmpType.getFullyQualifiedName());
                        }
                    } else {
                        // if it doesn't exist, we need to exit so we may as well mark it null.
                        tmpType = null;
                    }
                }
                declaringType = tmpType;
            } while (declaringType != null);

            // test the class inherit from the specified super class.
            // get the type hierarchy
            ITypeHierarchy hierarchy = type.newSupertypeHierarchy(new NullProgressMonitor());

            // if the super class is not the reference class, it may inherit from
            // it so we get its supertype. At some point it will be null and we
            // will stop
            IType superType = type;
            boolean foundProperSuperClass = false;
            while ((superType = hierarchy.getSuperclass(superType)) != null && superType.exists()) {
                if (superClassName.equals(superType.getFullyQualifiedName())) {
                    foundProperSuperClass = true;
                }
            }

            // didn't find the proper superclass? return false.
            if (foundProperSuperClass == false) {
                return String.format("%1$s does not extend %2$s", className, superClassName);
            }

            return TEST_CLASS_OK;
        } else {
            return String.format("Class %1$s does not exist", className);
        }
    } catch (JavaModelException e) {
        return String.format("%1$s: %2$s", className, e.getMessage());
    }
}

From source file:com.android.ide.eclipse.editors.layout.descriptors.CustomViewDescriptorService.java

License:Open Source License

/**
 * Returns the {@link ElementDescriptor} for a particular project/class.
 * <p/>//from  w  ww  .  j  a  v  a 2s . com
 * If it is the first time the <code>ElementDescriptor</code> is requested, the method
 * will check that the specified class is in fact a custom View class. Once this is
 * established, a monitoring for that particular class is initiated. Any change will
 * trigger a notification to the {@link ICustomViewDescriptorListener}.
 * @param project the project containing the class.
 * @param fqClassName the fully qualified name of the class.
 * @return a <code>ElementDescriptor</code> or <code>null</code> if the class was not
 * a custom View class.
 */
public ElementDescriptor getDescriptor(IProject project, String fqClassName) {
    // look in the map first
    synchronized (mCustomDescriptorMap) {
        HashMap<String, ElementDescriptor> map = mCustomDescriptorMap.get(project);

        if (map != null) {
            ElementDescriptor descriptor = map.get(fqClassName);
            if (descriptor != null) {
                return descriptor;
            }
        }

        // if we step here, it looks like we haven't created it yet.
        // First lets check this is in fact a valid type in the project

        try {
            // We expect the project to be both opened and of java type (since it's an android
            // project), so we can create a IJavaProject object from our IProject.
            IJavaProject javaProject = JavaCore.create(project);

            // replace $ by . in the class name
            String javaClassName = fqClassName.replaceAll("\\$", "\\."); //$NON-NLS-1$ //$NON-NLS-2$

            // look for the IType object for this class
            IType type = javaProject.findType(javaClassName);
            if (type != null && type.exists()) {
                // the type exists. Let's get the parent class and its ViewClassInfo.

                // get the type hierarchy
                ITypeHierarchy hierarchy = type.newSupertypeHierarchy(new NullProgressMonitor());

                ElementDescriptor parentDescriptor = getDescriptor(hierarchy.getSuperclass(type), project,
                        hierarchy);

                if (parentDescriptor != null) {
                    // we have a valid parent, lets create a new ElementDescriptor.

                    ViewElementDescriptor descriptor = new ViewElementDescriptor(fqClassName, fqClassName, // ui_name
                            fqClassName, // canonical class name
                            null, // tooltip
                            null, // sdk_url
                            getAttributeDescriptor(type, parentDescriptor), null, // layout attributes
                            null, // children
                            false /* mandatory */);

                    synchronized (mCustomDescriptorMap) {
                        map = mCustomDescriptorMap.get(project);
                        if (map == null) {
                            map = new HashMap<String, ElementDescriptor>();
                            mCustomDescriptorMap.put(project, map);
                        }

                        map.put(fqClassName, descriptor);
                    }

                    //TODO setup listener on this resource change.

                    return descriptor;
                }
            }
        } catch (JavaModelException e) {
            // there was an error accessing any of the IType, we'll just return null;
        }
    }

    return null;
}

From source file:com.android.ide.eclipse.editors.layout.descriptors.CustomViewDescriptorService.java

License:Open Source License

/**
 * Computes (if needed) and returns the {@link ElementDescriptor} for the specified type.
 * //w  w w .j a  v a 2s.  co m
 * @param type 
 * @param project 
 * @param typeHierarchy
 * @return A ViewElementDescriptor or null if type or typeHierarchy is null.
 */
private ViewElementDescriptor getDescriptor(IType type, IProject project, ITypeHierarchy typeHierarchy) {
    // check if the type is a built-in View class.
    List<ElementDescriptor> builtInList = null;

    Sdk currentSdk = Sdk.getCurrent();
    IAndroidTarget target = currentSdk == null ? null : currentSdk.getTarget(project);
    if (target != null) {
        AndroidTargetData data = currentSdk.getTargetData(target);
        builtInList = data.getLayoutDescriptors().getViewDescriptors();
    }

    // give up if there's no type
    if (type == null) {
        return null;
    }

    String canonicalName = type.getFullyQualifiedName();

    if (builtInList != null) {
        for (ElementDescriptor desc : builtInList) {
            if (desc instanceof ViewElementDescriptor) {
                ViewElementDescriptor viewDescriptor = (ViewElementDescriptor) desc;
                if (canonicalName.equals(viewDescriptor.getCanonicalClassName())) {
                    return viewDescriptor;
                }
            }
        }
    }

    // it's not a built-in class? Lets look if the superclass is built-in
    // give up if there's no type
    if (typeHierarchy == null) {
        return null;
    }

    IType parentType = typeHierarchy.getSuperclass(type);
    if (parentType != null) {
        ViewElementDescriptor parentDescriptor = getDescriptor(parentType, project, typeHierarchy);

        if (parentDescriptor != null) {
            // parent class is a valid View class with a descriptor, so we create one
            // for this class.
            ViewElementDescriptor descriptor = new ViewElementDescriptor(canonicalName, canonicalName, // ui_name
                    canonicalName, // canonical name
                    null, // tooltip
                    null, // sdk_url
                    getAttributeDescriptor(type, parentDescriptor), null, // layout attributes
                    null, // children
                    false /* mandatory */);

            // add it to the map
            synchronized (mCustomDescriptorMap) {
                HashMap<String, ElementDescriptor> map = mCustomDescriptorMap.get(project);

                if (map == null) {
                    map = new HashMap<String, ElementDescriptor>();
                    mCustomDescriptorMap.put(project, map);
                }

                map.put(canonicalName, descriptor);

            }

            //TODO setup listener on this resource change.

            return descriptor;
        }
    }

    // class is neither a built-in view class, nor extend one. return null.
    return null;
}

From source file:com.google.gdt.eclipse.core.java.JavaModelSearch.java

License:Open Source License

public static IField findFieldInHierarchy(ITypeHierarchy hierarchy, IType type, String fieldName) {

    IField field = findField(type, fieldName);

    // Check super class
    if (field == null) {
        IType superClass = hierarchy.getSuperclass(type);
        if (superClass != null) {
            field = findFieldInHierarchy(hierarchy, superClass, fieldName);
        }//  ww w  .j  a va2 s. com
    }

    return field;
}

From source file:com.google.gdt.eclipse.core.java.JavaModelSearch.java

License:Open Source License

private static IMethod findMethodOrCtorInHierarchy(ITypeHierarchy hierarchy, IType type, String methodName,
        boolean isConstructor) {
    try {/*w  w  w  .j a va 2  s  .c om*/
        IMethod[] methods = type.getMethods();
        for (IMethod method : methods) {
            if (method.getElementName().equals(methodName)) {
                return method;
            }
        }
    } catch (JavaModelException e) {
        CorePluginLog.logError(e);
        return null;
    }

    // Check super class
    IType superClass = hierarchy.getSuperclass(type);
    if (superClass != null) {
        IMethod method = findMethodOrCtorInHierarchy(hierarchy, superClass, methodName, isConstructor);
        if (method != null) {
            return method;
        }
    }

    if (!isConstructor) {
        // Check interfaces
        IType[] superInterfaces = hierarchy.getSuperInterfaces(type);
        for (IType superInterface : superInterfaces) {
            IMethod method = findMethodOrCtorInHierarchy(hierarchy, superInterface, methodName, false);
            if (method != null) {
                return method;
            }
        }
    }

    return null;
}