Search in sources :

Example 1 with JavaTypeElement

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

the class JavaElementDifferenceAnalyzer method appendUses.

private void appendUses(ProbingEnvironment env, Element element, final StringBuilder bld) {
    LOG.trace("Reporting uses of {}", element);
    if (element == null) {
        bld.append("<null>");
        return;
    }
    while (element != null && !(element instanceof JavaTypeElement)) {
        element = element.getParent();
    }
    if (element == null) {
        return;
    }
    JavaTypeElement usedType = (JavaTypeElement) element;
    if (usedType.isInAPI() && !usedType.isInApiThroughUse()) {
        String message = MessageFormat.format(messages.getString("revapi.java.uses.partOfApi"), usedType.getFullHumanReadableString());
        bld.append(message);
        return;
    }
    usedType.visitUseSites(new UseSite.Visitor<Object, Void>() {

        @Nullable
        @Override
        public Object visit(@Nonnull DeclaredType type, @Nonnull UseSite use, @Nullable Void parameter) {
            if (appendUse(env, usedType, bld, type, use)) {
                // just a non-null values
                return Boolean.TRUE;
            }
            return null;
        }

        @Nullable
        @Override
        public Object end(DeclaredType type, @Nullable Void parameter) {
            return null;
        }
    }, null);
}
Also used : UseSite(org.revapi.java.spi.UseSite) JavaTypeElement(org.revapi.java.spi.JavaTypeElement) Nullable(javax.annotation.Nullable) DeclaredType(javax.lang.model.type.DeclaredType)

Example 2 with JavaTypeElement

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

the class JavaElementDifferenceAnalyzer method appendUseType.

private boolean appendUseType(ProbingEnvironment env, JavaTypeElement ut, List<TypeAndUseSite> path, JavaTypeElement usedType, DeclaredType type, UseSite currentUse, Set<javax.lang.model.element.TypeElement> visitedTypes) {
    javax.lang.model.element.TypeElement useType = ut.getDeclaringElement();
    if (visitedTypes.contains(useType)) {
        return false;
    }
    visitedTypes.add(useType);
    if (ut.isInAPI() && !ut.isInApiThroughUse() && !ut.equals(usedType)) {
        // the class is in the primary API
        path.add(0, new TypeAndUseSite(type, currentUse));
        return true;
    } else {
        Boolean ret = ut.visitUseSites(new UseSite.Visitor<Boolean, Void>() {

            @Nullable
            @Override
            public Boolean visit(@Nonnull DeclaredType visitedType, @Nonnull UseSite use, @Nullable Void parameter) {
                if (traverseToApi(env, usedType, visitedType, use, path, visitedTypes)) {
                    path.add(0, new TypeAndUseSite(type, currentUse));
                    return true;
                }
                return null;
            }

            @Nullable
            @Override
            public Boolean end(DeclaredType type, @Nullable Void parameter) {
                return null;
            }
        }, null);
        if (ret == null) {
            Set<javax.lang.model.element.TypeElement> derivedUseTypes = env.getDerivedTypes(useType);
            for (javax.lang.model.element.TypeElement dut : derivedUseTypes) {
                TypeElement model = env.getTypeMap().get(dut);
                if (model == null) {
                    continue;
                }
                JavaModelElement derivedUseElement = findSameDeclarationUnder(currentUse.getSite(), model);
                if (derivedUseElement == null) {
                    continue;
                }
                UseSite derivedUse = new UseSite(currentUse.getUseType(), derivedUseElement);
                if (appendUseType(env, model, path, usedType, type, derivedUse, visitedTypes)) {
                    ret = true;
                    break;
                }
            }
        }
        return ret == null ? false : ret;
    }
}
Also used : JavaModelElement(org.revapi.java.spi.JavaModelElement) JavaTypeElement(org.revapi.java.spi.JavaTypeElement) TypeElement(org.revapi.java.model.TypeElement) UseSite(org.revapi.java.spi.UseSite) Nullable(javax.annotation.Nullable) DeclaredType(javax.lang.model.type.DeclaredType)

Example 3 with JavaTypeElement

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

the class JavaElementDifferenceAnalyzer method isCheckedElsewhere.

private boolean isCheckedElsewhere(JavaModelElement element, ProbingEnvironment env) {
    if (element == null) {
        // the other element will not be null and therefore we will determine the fact with the other element...
        return true;
    }
    if (!element.isInherited()) {
        return false;
    }
    String elementSig = Util.toUniqueString(element.getModelRepresentation());
    String declSig = Util.toUniqueString(element.getDeclaringElement().asType());
    if (!Objects.equals(elementSig, declSig)) {
        return false;
    }
    javax.lang.model.element.TypeElement declaringType = findTypeOf(element.getDeclaringElement());
    JavaTypeElement declaringClass = env.getTypeMap().get(declaringType);
    return declaringClass != null && declaringClass.isInAPI();
}
Also used : JavaTypeElement(org.revapi.java.spi.JavaTypeElement)

Example 4 with JavaTypeElement

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

the class InheritanceChainChanged method doVisitClass.

@Override
protected void doVisitClass(JavaTypeElement oldEl, JavaTypeElement newEl) {
    if (!isBothAccessible(oldEl, newEl)) {
        return;
    }
    TypeElement oldType = oldEl.getDeclaringElement();
    TypeElement newType = newEl.getDeclaringElement();
    List<TypeMirror> oldSuperTypes = Util.getAllSuperClasses(getOldTypeEnvironment().getTypeUtils(), oldType.asType());
    List<TypeMirror> newSuperTypes = Util.getAllSuperClasses(getNewTypeEnvironment().getTypeUtils(), newType.asType());
    if (oldSuperTypes.size() != newSuperTypes.size()) {
        pushActive(oldEl, newEl, oldSuperTypes, newSuperTypes);
    } else {
        Types oldTypes = getOldTypeEnvironment().getTypeUtils();
        Types newTypes = getNewTypeEnvironment().getTypeUtils();
        for (int i = 0; i < oldSuperTypes.size(); ++i) {
            // need to erase them all so that we get only true type changes. formal type parameter changes
            // are captured by SuperTypeParametersChanged check
            TypeMirror oldSuperClass = oldTypes.erasure(oldSuperTypes.get(i));
            TypeMirror newSuperClass = newTypes.erasure(newSuperTypes.get(i));
            if (!Util.isSameType(oldSuperClass, newSuperClass)) {
                pushActive(oldEl, newEl, oldSuperTypes, newSuperTypes);
                break;
            }
        }
    }
}
Also used : Types(javax.lang.model.util.Types) TypeMirror(javax.lang.model.type.TypeMirror) JavaTypeElement(org.revapi.java.spi.JavaTypeElement) TypeElement(javax.lang.model.element.TypeElement)

Example 5 with JavaTypeElement

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

the class Added method doEnd.

@Override
protected List<Difference> doEnd() {
    ActiveElements<JavaTypeElement> types = popIfActive();
    if (types != null) {
        TypeElement typeInOld = getOldTypeEnvironment().getElementUtils().getTypeElement(types.newElement.getDeclaringElement().getQualifiedName());
        String[] attachments = Code.attachmentsFor(types.oldElement, types.newElement);
        Difference difference = typeInOld == null ? createDifference(Code.CLASS_ADDED, attachments) : createDifference(Code.CLASS_EXTERNAL_CLASS_EXPOSED_IN_API, attachments);
        return Collections.singletonList(difference);
    }
    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)

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