Search in sources :

Example 1 with MethodDeclaration

use of org.apache.aries.versioning.utils.MethodDeclaration in project aries by apache.

the class BundleCompatibility method visitPackage.

/**
 * Visit the whole package to scan each class to see whether we need to log minor or major changes.
 *
 * @param pkgName
 * @param baseClazz
 * @param curClazz
 * @param majorChange
 * @param minorChange
 */
private void visitPackage(String pkgName, Map<String, IFile> baseClazz, Map<String, IFile> curClazz, VersionChangeReason majorChange, VersionChangeReason minorChange) {
    StringBuilder major_reason = new StringBuilder();
    StringBuilder minor_reason = new StringBuilder();
    boolean is_major_change = false;
    boolean is_minor_change = false;
    String fatal_class = null;
    boolean foundNewAbstract = false;
    for (Map.Entry<String, IFile> file : baseClazz.entrySet()) {
        // scan the latest version of the class
        IFile curFile = curClazz.get(file.getKey());
        String changeClass = file.getValue().getName();
        // Scan the base version
        SemanticVersioningClassVisitor oldcv = getVisitor(file.getValue(), oldJarsLoader);
        // skip the property files as they are compiled as class file as well
        ClassDeclaration cd = oldcv.getClassDeclaration();
        if ((cd != null) && (!SemanticVersioningUtils.isPropertyFile(cd))) {
            if (curFile == null) {
                // the class we are scanning has been deleted from the current version of the bundle
                // This should be a major increase
                major_reason.append(twoLineBreaks + "The class/interface " + getClassName(changeClass) + " has been deleted from the package.");
                // majorChange.update(reason, changeClass);
                is_major_change = true;
                // only replace the fatal class if not set as the class won't be found in cmvc due to the fact it has been deleted.
                if (fatal_class == null) {
                    fatal_class = changeClass;
                }
            } else {
                // check for binary compatibility
                // load the class from the current version of the bundle
                // remove it from the curClazz collection as we would like to know whether there are more classes added
                curClazz.remove(file.getKey());
                SemanticVersioningClassVisitor newcv = getVisitor(curFile, newJarsLoader);
                // check for binary compatibility
                ClassDeclaration newcd = newcv.getClassDeclaration();
                BinaryCompatibilityStatus bcs = newcd.getBinaryCompatibleStatus(oldcv.getClassDeclaration());
                if (!bcs.isCompatible()) {
                    major_reason.append(twoLineBreaks + "In the " + getClassName(changeClass) + " class or its supers, the following changes have been made since the last release.");
                    // break binary compatibility
                    for (String reason : bcs) {
                        major_reason.append(oneLineBreak).append(reason);
                    }
                    is_major_change = true;
                    fatal_class = changeClass;
                } else {
                    // check to see whether more methods are added
                    ClassDeclaration oldcd = oldcv.getClassDeclaration();
                    Collection<MethodDeclaration> extraMethods = newcd.getExtraMethods(oldcd);
                    boolean containsConcrete = false;
                    boolean containsAbstract = false;
                    boolean abstractClass = newcd.isAbstract();
                    StringBuilder subRemarks = new StringBuilder();
                    String concreteSubRemarks = null;
                    for (MethodDeclaration extraMethod : extraMethods) {
                        // only interested in the visible methods not the system generated ones
                        if (!extraMethod.getName().contains("$")) {
                            if (abstractClass) {
                                if (extraMethod.isAbstract()) {
                                    foundNewAbstract = true;
                                    containsAbstract = true;
                                    subRemarks.append(oneLineBreak + SemanticVersioningUtils.getReadableMethodSignature(extraMethod.getName(), extraMethod.getDesc()));
                                } else {
                                    // only list one abstract method, no need to list all
                                    containsConcrete = true;
                                    concreteSubRemarks = oneLineBreak + SemanticVersioningUtils.getReadableMethodSignature(extraMethod.getName(), extraMethod.getDesc());
                                }
                            } else {
                                containsConcrete = true;
                                concreteSubRemarks = oneLineBreak + SemanticVersioningUtils.getReadableMethodSignature(extraMethod.getName(), extraMethod.getDesc());
                                break;
                            }
                        }
                    }
                    if (containsConcrete || containsAbstract) {
                        is_minor_change = true;
                        if (!is_major_change) {
                            fatal_class = changeClass;
                        }
                        if (containsAbstract) {
                            minor_reason.append(twoLineBreaks + "In the " + getClassName(changeClass) + " class or its supers, the following abstract methods have been added since the last release of this bundle.");
                            minor_reason.append(subRemarks);
                        } else {
                            minor_reason.append(twoLineBreaks + "In the " + getClassName(changeClass) + " class or its supers, the following method has been added since the last release of this bundle.");
                            minor_reason.append(concreteSubRemarks);
                        }
                    }
                    if (!is_minor_change) {
                        for (FieldDeclaration field : newcd.getExtraFields(oldcd)) {
                            if (field.isPublic() || field.isProtected()) {
                                is_minor_change = true;
                                String extraFieldRemarks = oneLineBreak + " " + SemanticVersioningUtils.transform(field.getDesc()) + " " + field.getName();
                                if (!is_major_change) {
                                    fatal_class = changeClass;
                                }
                                minor_reason.append(twoLineBreaks + "In the " + getClassName(changeClass) + " class or its supers, the following fields have been added since the last release of this bundle.");
                                minor_reason.append(extraFieldRemarks);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    if (is_major_change) {
        majorChange.update(major_reason.toString(), fatal_class, false);
    }
    if (is_minor_change) {
        minorChange.update(minor_reason.toString(), fatal_class, (foundNewAbstract ? true : false));
    }
}
Also used : IFile(org.apache.aries.util.filesystem.IFile) ClassDeclaration(org.apache.aries.versioning.utils.ClassDeclaration) BinaryCompatibilityStatus(org.apache.aries.versioning.utils.BinaryCompatibilityStatus) MethodDeclaration(org.apache.aries.versioning.utils.MethodDeclaration) SemanticVersioningClassVisitor(org.apache.aries.versioning.utils.SemanticVersioningClassVisitor) HashMap(java.util.HashMap) Map(java.util.Map) FieldDeclaration(org.apache.aries.versioning.utils.FieldDeclaration)

Example 2 with MethodDeclaration

use of org.apache.aries.versioning.utils.MethodDeclaration in project aries by apache.

the class BinaryCompatibilityTest method test_containing_more_abstract_methods.

/**
 * Check containing more abstract methods
 */
@Test
public void test_containing_more_abstract_methods() {
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_5, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE, "pkg/Test", null, "java/lang/Object", new String[] { "versioning/java/files/TestB" });
    cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "getFoo", "(I)I", null, null).visitEnd();
    cw.visitEnd();
    byte[] oldBytes = cw.toByteArray();
    cw = new ClassWriter(0);
    cw.visit(V1_5, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE, "pkg/Test", null, "java/lang/Object", new String[] { "versioning/java/files/TestB" });
    cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "getFoo", "(I)I", null, null).visitEnd();
    cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "getMoo", "()I", null, null).visitEnd();
    cw.visitEnd();
    byte[] newBytes = cw.toByteArray();
    SemanticVersioningClassVisitor oldCV = new SemanticVersioningClassVisitor(loader);
    SemanticVersioningClassVisitor newCV = new SemanticVersioningClassVisitor(loader);
    ClassReader newCR = new ClassReader(newBytes);
    ClassReader oldCR = new ClassReader(oldBytes);
    newCR.accept(newCV, 0);
    oldCR.accept(oldCV, 0);
    assertTrue("Adding an abstract methods should not break compatibility.", newCV.getClassDeclaration().getBinaryCompatibleStatus(oldCV.getClassDeclaration()).isCompatible());
    Collection<MethodDeclaration> extraMethods = newCV.getClassDeclaration().getExtraMethods(oldCV.getClassDeclaration());
    assertEquals(1, extraMethods.size());
    for (MethodDeclaration md : extraMethods) {
        assertEquals("getMoo", md.getName());
    }
}
Also used : MethodDeclaration(org.apache.aries.versioning.utils.MethodDeclaration) ClassReader(org.objectweb.asm.ClassReader) SemanticVersioningClassVisitor(org.apache.aries.versioning.utils.SemanticVersioningClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) Test(org.junit.Test)

Aggregations

MethodDeclaration (org.apache.aries.versioning.utils.MethodDeclaration)2 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 IFile (org.apache.aries.util.filesystem.IFile)1 BinaryCompatibilityStatus (org.apache.aries.versioning.utils.BinaryCompatibilityStatus)1 ClassDeclaration (org.apache.aries.versioning.utils.ClassDeclaration)1 FieldDeclaration (org.apache.aries.versioning.utils.FieldDeclaration)1 Test (org.junit.Test)1 ClassReader (org.objectweb.asm.ClassReader)1 ClassWriter (org.objectweb.asm.ClassWriter)1