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

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

Introduction

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

Prototype

IType[] getSupertypes(IType type);

Source Link

Document

Returns the resolved supertypes of the given type, in no particular order, limited to the types in this type hierarchy's graph.

Usage

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

License:unlicense.org

private boolean isShowing(IType t) {
    if (added.containsKey(t)) {
        return true;
    }/*  www .  java2 s  .  c om*/
    for (ITypeHierarchy h : added.values()) {
        if (h.getSupertypes(t) != null) {
            return true;
        }
    }
    return false;
}

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

License:unlicense.org

private void getAllTypes(Set<IType> results, ITypeHierarchy h, IType type) {
    if (isHidden(type) || results.contains(type)) {
        return;//from www  .ja  v  a 2  s  . c o  m
    }
    results.add(type);
    for (IType sup : h.getSupertypes(type)) {
        getAllTypes(results, h, sup);
    }
}

From source file:net.sf.commonclipse.CompareToGenerator.java

License:Apache License

/**
 * @see net.sf.commonclipse.Generator#createMethod(org.eclipse.jdt.core.IType)
 *//*from w w w .j av a  2 s  . c  om*/
protected String createMethod(IType type) throws JavaModelException {

    StringBuffer buffer = new StringBuffer();

    buffer.append(getJavadoc());

    String className = type.getElementName();

    buffer.append("public int compareTo("); //$NON-NLS-1$

    if (CCPluginPreferences.getPreferences().useFinalParameters()) {
        buffer.append("final "); //$NON-NLS-1$
    }

    buffer.append("Object object) {\n"); //$NON-NLS-1$

    buffer.append(className);
    buffer.append(" myClass = ("); //$NON-NLS-1$
    buffer.append(className);
    buffer.append(") object;\nreturn new CompareToBuilder()\n"); //$NON-NLS-1$

    if (CCPluginPreferences.getPreferences().appendSuperToCompareTo()) {
        // add only if superclass implements the Comparable interface
        if (doesSuperImplementsComparable(type)) {
            boolean addSuper = true;
            ITypeHierarchy hierarchy = type.newSupertypeHierarchy(null);
            IType[] types = hierarchy.getSupertypes(type);
            for (int i = 0; i < types.length; i++) {
                if (types[i] != null && "Ljava/lang/Object;".equals(types[i].getKey()))
                    addSuper = false;
            }
            if (addSuper) {
                buffer.append(".appendSuper(super.compareTo(object))\n"); //$NON-NLS-1$

            }
        }
    }

    buffer.append(buildAppenderList(type));

    buffer.append(".toComparison();\n}\n"); //$NON-NLS-1$
    return buffer.toString();
}

From source file:net.sf.commonclipse.EqualsGenerator.java

License:Apache License

/**
 * @see net.sf.commonclipse.Generator#createMethod(org.eclipse.jdt.core.IType)
 *///from   w  w  w  .  j av  a 2 s  . c  om
protected String createMethod(IType type) throws JavaModelException {

    StringBuffer buffer = new StringBuffer();

    buffer.append(getJavadoc());

    String className = type.getElementName();

    buffer.append("public boolean equals("); //$NON-NLS-1$

    if (CCPluginPreferences.getPreferences().useFinalParameters()) {
        buffer.append("final "); //$NON-NLS-1$
    }

    buffer.append("Object object) {\n"); //$NON-NLS-1$

    if (CCPluginPreferences.getPreferences().addInstanceCheckToEquals()) {
        buffer.append("if (object == this) {\nreturn true;\n}\n"); //$NON-NLS-1$
    }

    buffer.append("if ( !(object instanceof "); //$NON-NLS-1$
    buffer.append(className);
    buffer.append(") ) {\nreturn false;\n}\n"); //$NON-NLS-1$
    buffer.append(className);
    buffer.append(" rhs = ("); //$NON-NLS-1$
    buffer.append(className);
    buffer.append(") object;\nreturn new EqualsBuilder()\n"); //$NON-NLS-1$

    if (CCPluginPreferences.getPreferences().appendSuperToEquals()) {
        boolean addSuper = true;
        ITypeHierarchy hierarchy = type.newSupertypeHierarchy(null);
        IType[] types = hierarchy.getSupertypes(type);
        for (int i = 0; i < types.length; i++) {
            if (types[i] != null && "Ljava/lang/Object;".equals(types[i].getKey()))
                addSuper = false;
        }
        if (addSuper) {
            buffer.append(".appendSuper(super.equals(object))\n"); //$NON-NLS-1$

        }
    }

    buffer.append(buildAppenderList(type));

    buffer.append(".isEquals();\n}\n"); //$NON-NLS-1$
    return buffer.toString();
}

From source file:net.sf.commonclipse.Generator.java

License:Apache License

/**
 * Returns a Set containing all the names of fields visible by this type.
 * @param type IType/* w  ww  .j ava2 s .  com*/
 * @return Map containg field names - IField objects
 * @throws JavaModelException exception in analyzing type
 */
protected Map buildFieldMap(IType type) throws JavaModelException {
    Map fieldNames = new HashMap();

    IField[] fields = type.getFields();

    for (int j = 0; j < fields.length; j++) {
        IField field = fields[j];
        int flags = field.getFlags();

        if (!Flags.isStatic(flags)) {
            fieldNames.put(field.getElementName(), field);
        }
    }

    // get all the supertypes to look for public and protected fields
    ITypeHierarchy hierarchy = type.newSupertypeHierarchy(null);
    IType[] types = hierarchy.getSupertypes(type);

    for (int j = 0; j < types.length; j++) {
        IField[] superFields = types[j].getFields();

        for (int x = 0; x < superFields.length; x++) {
            IField field = superFields[x];
            int flags = field.getFlags();

            // no static and private
            if (!Flags.isStatic(flags) && !Flags.isPrivate(flags)) {
                fieldNames.put(field.getElementName(), field);
            }
        }
    }
    return fieldNames;
}

From source file:net.sf.commonclipse.HashcodeGenerator.java

License:Apache License

/**
 * @see net.sf.commonclipse.Generator#createMethod(org.eclipse.jdt.core.IType)
 *///from  www . j a v  a2s. c o m
protected String createMethod(IType type) throws JavaModelException {
    int initial = this.random.nextInt();
    int multiplier = this.random.nextInt();

    // be shure they are odd numbers
    if (initial % 2 == 0) {
        initial++;
    }
    if (multiplier % 2 == 0) {
        multiplier++;
    }

    StringBuffer buffer = new StringBuffer();

    buffer.append(getJavadoc());

    buffer.append("public int hashCode()\n{\nreturn new HashCodeBuilder("); //$NON-NLS-1$
    buffer.append(initial);
    buffer.append(", "); //$NON-NLS-1$
    buffer.append(multiplier);
    buffer.append(")\n"); //$NON-NLS-1$

    if (CCPluginPreferences.getPreferences().appendSuperToHashcode()) {
        boolean addSuper = true;
        ITypeHierarchy hierarchy = type.newSupertypeHierarchy(null);
        IType[] types = hierarchy.getSupertypes(type);
        for (int i = 0; i < types.length; i++) {
            if (types[i] != null && "Ljava/lang/Object;".equals(types[i].getKey()))
                addSuper = false;
        }
        if (addSuper) {
            buffer.append(".appendSuper(super.hashCode())\n"); //$NON-NLS-1$
        }
    }

    buffer.append(buildAppenderList(type));

    buffer.append(".toHashCode();\n}\n"); //$NON-NLS-1$
    return buffer.toString();
}

From source file:net.sf.commonclipse.ToStringGenerator.java

License:Apache License

/**
 * @see net.sf.commonclipse.Generator#createMethod(org.eclipse.jdt.core.IType)
 *///from   ww  w . jav a2s .  c o m
protected String createMethod(IType type) throws JavaModelException {

    StringBuffer buffer = new StringBuffer();

    buffer.append(getJavadoc());

    buffer.append("    public String toString()\n    {\n        return new ToStringBuilder(this"); //$NON-NLS-1$

    if (CCPluginPreferences.getPreferences().useCustomToStringStyle()) {
        buffer.append(", "); //$NON-NLS-1$
        buffer.append(CCPluginPreferences.getPreferences().getToStringStyleClassAndConstant());
    }

    buffer.append(")\n"); //$NON-NLS-1$

    if (CCPluginPreferences.getPreferences().appendSuperToToString()) {
        boolean addSuper = true;
        ITypeHierarchy hierarchy = type.newSupertypeHierarchy(null);
        IType[] types = hierarchy.getSupertypes(type);
        for (int i = 0; i < types.length; i++) {
            if (types[i] != null && "Ljava/lang/Object;".equals(types[i].getKey()))
                addSuper = false;
        }
        if (addSuper) {
            buffer.append(".appendSuper(super.toString())\n"); //$NON-NLS-1$

        }
    }

    if (CCPluginPreferences.getPreferences().useJavabeanToString()) {
        buffer.append(buildAppenderListFromBean(type));
    } else {
        buffer.append(buildAppenderList(type));
    }

    buffer.append(".toString();\n}"); //$NON-NLS-1$

    return buffer.toString();
}

From source file:org.hawkinssoftware.rns.analysis.compile.publication.PublicationTypeConstraintCollector.java

License:Open Source License

/**
 * Collect all the constraints that may affect <code>type</code>, aggregating them according to fixed rules of:
 * /*from   w  ww .j a  va 2 s  .  c  o  m*/
 * <pre>
 * 1. inheritance along hierarchy edges 
 * 2. from a type to its contained methods
 * </pre>
 * 
 * Nodes not declaring `inherited are skipped over as if they were not annotated. A node having `voidInheritance or
 * having no declared elements truncates the path (allow all and deny all, respectively).
 * 
 * @throws JavaModelException
 */
AggregatePublicationConstraint collectTypeConstraints(ITypeHierarchy hierarchy) throws JavaModelException {
    AggregatePublicationConstraint directConstraints = parseTypeConstraints(hierarchy.getType());
    if ((directConstraints != null) && !directConstraints.traverse()) {
        if (directConstraints.voidInheritance && directConstraints.isEmpty()) {
            return null;
        }
        return directConstraints;
    }

    TypeScanPass pass = new TypeScanPass(hierarchy);
    for (IType supertype : hierarchy.getSupertypes(hierarchy.getType())) {
        collectTypeConstraints(supertype, pass);
    }

    if (directConstraints == null) {
        if (pass.applicableConstraints.isEmpty()) {
            return null;
        }
        directConstraints = pass.applicableConstraints.remove(pass.applicableConstraints.size() - 1);
    }
    for (AggregatePublicationConstraint constraints : pass.applicableConstraints) {
        directConstraints = directConstraints.inheritFrom(constraints);
    }

    // TODO: the error will not know which @PublicationConstraint is causing a violation (if any)
    return directConstraints;
}