Search in sources :

Example 6 with JavaTypeElement

use of org.revapi.java.spi.JavaTypeElement in project revapi by revapi.

the class KindChanged method doEnd.

@Override
protected List<Difference> doEnd() {
    ActiveElements<JavaTypeElement> types = popIfActive();
    if (types != null) {
        TypeElement o = types.oldElement.getDeclaringElement();
        TypeElement n = types.newElement.getDeclaringElement();
        if (o.getKind() != n.getKind()) {
            Difference p = createDifference(Code.CLASS_KIND_CHANGED, Code.attachmentsFor(types.oldElement, types.newElement, "oldKind", kind(o), "newKind", kind(n)));
            return Collections.singletonList(p);
        }
    }
    return null;
}
Also used : JavaTypeElement(org.revapi.java.spi.JavaTypeElement) TypeElement(javax.lang.model.element.TypeElement) Difference(org.revapi.Difference) JavaTypeElement(org.revapi.java.spi.JavaTypeElement)

Example 7 with JavaTypeElement

use of org.revapi.java.spi.JavaTypeElement in project revapi by revapi.

the class JavaElementBase method createFullHumanReadableString.

protected String createFullHumanReadableString() {
    String decl = Util.toHumanReadableString(getDeclaringElement());
    if (isInherited()) {
        org.revapi.Element parent = getParent();
        while (parent != null && !(parent instanceof JavaTypeElement)) {
            parent = parent.getParent();
        }
        JavaTypeElement parentType = (JavaTypeElement) parent;
        if (parentType != null) {
            decl += " @ " + Util.toHumanReadableString(parentType.getDeclaringElement());
        }
    }
    return getHumanReadableElementType() + " " + decl;
}
Also used : JavaTypeElement(org.revapi.java.spi.JavaTypeElement)

Example 8 with JavaTypeElement

use of org.revapi.java.spi.JavaTypeElement in project revapi by revapi.

the class NowImplementsInterface method doEnd.

@Override
protected List<Difference> doEnd() {
    CheckBase.ActiveElements<JavaTypeElement> types = popIfActive();
    if (types == null) {
        return null;
    }
    List<Difference> result = new ArrayList<>();
    List<? extends TypeMirror> newInterfaces = types.newElement.getDeclaringElement().getInterfaces();
    List<? extends TypeMirror> oldInterfaces = types.oldElement.getDeclaringElement().getInterfaces();
    for (TypeMirror newIface : newInterfaces) {
        if (!Util.isSubtype(newIface, oldInterfaces, getNewTypeEnvironment().getTypeUtils())) {
            result.add(createDifference(Code.CLASS_NOW_IMPLEMENTS_INTERFACE, Code.attachmentsFor(types.oldElement, types.newElement, "interface", Util.toHumanReadableString(newIface))));
        }
    }
    return result;
}
Also used : CheckBase(org.revapi.java.spi.CheckBase) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) Difference(org.revapi.Difference) JavaTypeElement(org.revapi.java.spi.JavaTypeElement)

Example 9 with JavaTypeElement

use of org.revapi.java.spi.JavaTypeElement in project revapi by revapi.

the class InheritanceChainChanged method doEnd.

@Override
protected List<Difference> doEnd() {
    ActiveElements<JavaTypeElement> types = popIfActive();
    if (types != null) {
        List<Difference> ret = new ArrayList<>();
        @SuppressWarnings("unchecked") List<TypeMirror> oldSuperClasses = (List<TypeMirror>) types.context[0];
        @SuppressWarnings("unchecked") List<TypeMirror> newSuperClasses = (List<TypeMirror>) types.context[1];
        Comparator<TypeMirror> typeNameComparator = Comparator.comparing(Util::toUniqueString);
        List<TypeMirror> removedSuperClasses = new ArrayList<>();
        List<TypeMirror> addedSuperClasses = new ArrayList<>();
        oldSuperClasses.sort(typeNameComparator);
        newSuperClasses.sort(typeNameComparator);
        CoIterator<TypeMirror> iterator = new CoIterator<>(oldSuperClasses.iterator(), newSuperClasses.iterator(), typeNameComparator);
        while (iterator.hasNext()) {
            iterator.next();
            TypeMirror oldType = iterator.getLeft();
            TypeMirror newType = iterator.getRight();
            if (oldType == null) {
                addedSuperClasses.add(newType);
            } else if (newType == null) {
                removedSuperClasses.add(oldType);
            }
        }
        // this will give us the equivalent of removed/added superclasses but ordered by the inheritance chain
        // not by name
        removedSuperClasses = retainInCopy(oldSuperClasses, removedSuperClasses);
        addedSuperClasses = retainInCopy(newSuperClasses, addedSuperClasses);
        Iterator<TypeMirror> removedIt = removedSuperClasses.iterator();
        Iterator<TypeMirror> addedIt = addedSuperClasses.iterator();
        // always report the most concrete classes
        if (removedIt.hasNext()) {
            removedIt.next();
        }
        if (addedIt.hasNext()) {
            addedIt.next();
        }
        // ok, now we only have super types left of the most concrete removed/added super class.
        // we are only going to report those that changed their inheritance hierarchy in the other version of the API.
        removeClassesWithEquivalentSuperClassChain(removedIt, getOldTypeEnvironment(), getNewTypeEnvironment());
        removeClassesWithEquivalentSuperClassChain(addedIt, getNewTypeEnvironment(), getOldTypeEnvironment());
        for (TypeMirror t : removedSuperClasses) {
            String str = Util.toHumanReadableString(t);
            ret.add(createDifference(Code.CLASS_NO_LONGER_INHERITS_FROM_CLASS, Code.attachmentsFor(types.oldElement, types.newElement, "superClass", str)));
        }
        for (TypeMirror t : addedSuperClasses) {
            String str = Util.toHumanReadableString(t);
            Code code = types.oldElement.getDeclaringElement().getModifiers().contains(Modifier.FINAL) ? Code.CLASS_FINAL_CLASS_INHERITS_FROM_NEW_CLASS : Code.CLASS_NON_FINAL_CLASS_INHERITS_FROM_NEW_CLASS;
            ret.add(createDifference(code, Code.attachmentsFor(types.oldElement, types.newElement, "superClass", str)));
            // additionally add a difference about checked exceptions
            if (changedToCheckedException(getNewTypeEnvironment().getTypeUtils(), t, oldSuperClasses)) {
                ret.add(createDifference(Code.CLASS_NOW_CHECKED_EXCEPTION, Code.attachmentsFor(types.oldElement, types.newElement)));
            }
        }
        return ret;
    }
    return null;
}
Also used : CoIterator(org.revapi.CoIterator) ArrayList(java.util.ArrayList) Util(org.revapi.java.spi.Util) Difference(org.revapi.Difference) Code(org.revapi.java.spi.Code) JavaTypeElement(org.revapi.java.spi.JavaTypeElement) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with JavaTypeElement

use of org.revapi.java.spi.JavaTypeElement in project revapi by revapi.

the class NoLongerImplementsInterface method doEnd.

@Override
protected List<Difference> doEnd() {
    ActiveElements<JavaTypeElement> types = popIfActive();
    if (types == null) {
        return null;
    }
    List<Difference> result = new ArrayList<>();
    @SuppressWarnings("unchecked") List<TypeMirror> oldInterfaces = (List<TypeMirror>) types.context[0];
    @SuppressWarnings("unchecked") List<TypeMirror> newInterfaces = (List<TypeMirror>) types.context[1];
    for (TypeMirror oldIface : oldInterfaces) {
        if (!Util.isSubtype(oldIface, newInterfaces, getOldTypeEnvironment().getTypeUtils())) {
            result.add(createDifference(Code.CLASS_NO_LONGER_IMPLEMENTS_INTERFACE, Code.attachmentsFor(types.oldElement, types.newElement, "interface", Util.toHumanReadableString(oldIface))));
        }
    }
    return result;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Difference(org.revapi.Difference) JavaTypeElement(org.revapi.java.spi.JavaTypeElement)

Aggregations

JavaTypeElement (org.revapi.java.spi.JavaTypeElement)11 Difference (org.revapi.Difference)6 TypeElement (javax.lang.model.element.TypeElement)4 TypeMirror (javax.lang.model.type.TypeMirror)4 ArrayList (java.util.ArrayList)3 List (java.util.List)2 Nullable (javax.annotation.Nullable)2 DeclaredType (javax.lang.model.type.DeclaredType)2 UseSite (org.revapi.java.spi.UseSite)2 Types (javax.lang.model.util.Types)1 CoIterator (org.revapi.CoIterator)1 TypeElement (org.revapi.java.model.TypeElement)1 CheckBase (org.revapi.java.spi.CheckBase)1 Code (org.revapi.java.spi.Code)1 JavaModelElement (org.revapi.java.spi.JavaModelElement)1 Util (org.revapi.java.spi.Util)1