Search in sources :

Example 16 with SemanticVersioningClassVisitor

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

the class BinaryCompatibilityTest method test_jdk_chap13_4_17_1.

/**
     * If a method is not private was not declared static and is changed to be declared static, this breaks compatibility.
     */
@Test
public void test_jdk_chap13_4_17_1() {
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_5, ACC_PUBLIC, "pkg/Test", null, "versioning/java/files/TestA", null);
    cw.visitMethod(ACC_PUBLIC, "getCooLen", "(Ljava/lang/String;)I", null, null).visitEnd();
    cw.visitEnd();
    byte[] oldBytes = cw.toByteArray();
    cw = new ClassWriter(0);
    cw.visit(V1_5, ACC_PUBLIC, "pkg/Test", null, "versioning/java/files/TestA", null);
    cw.visitField(ACC_PUBLIC + ACC_TRANSIENT, "aa", "Ljava/lang/String;", null, new String("newBar")).visitEnd();
    cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "getCooLen", "(Ljava/lang/String;)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);
    BinaryCompatibilityStatus bcs = newCV.getClassDeclaration().getBinaryCompatibleStatus((oldCV.getClassDeclaration()));
    assertEquals("The method int getCooLen(java.lang.String) has changed from static to non-static or vice versa.", bcs.get(0));
    assertTrue("If a method is not private was not declared static and is changed to be decalared static, this should break compatibility.", bcs.size() == 1);
}
Also used : BinaryCompatibilityStatus(org.apache.aries.versioning.utils.BinaryCompatibilityStatus) ClassReader(org.objectweb.asm.ClassReader) SemanticVersioningClassVisitor(org.apache.aries.versioning.utils.SemanticVersioningClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) Test(org.junit.Test)

Example 17 with SemanticVersioningClassVisitor

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

the class BinaryCompatibilityTest method test_jdk_chap13_4_14_1.

/**
     * Changing a method to be abstract is a binary compatibility change.
     */
@Test
public void test_jdk_chap13_4_14_1() {
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_5, ACC_PUBLIC, "pkg/Test", null, "versioning/java/files/TestA", null);
    cw.visitMethod(ACC_PUBLIC, "getCooLen", "(Ljava/lang/String;)I", null, null).visitEnd();
    cw.visitEnd();
    byte[] oldBytes = cw.toByteArray();
    cw = new ClassWriter(0);
    cw.visit(V1_5, ACC_PUBLIC, "pkg/Test", null, "versioning/java/files/TestA", null);
    cw.visitField(ACC_PUBLIC + ACC_TRANSIENT, "aa", "Ljava/lang/String;", null, new String("newBar")).visitEnd();
    cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "getCooLen", "(Ljava/lang/String;)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);
    BinaryCompatibilityStatus bcs = newCV.getClassDeclaration().getBinaryCompatibleStatus((oldCV.getClassDeclaration()));
    assertEquals("The method int getCooLen(java.lang.String) has changed from non abstract to abstract.", bcs.get(0));
    assertTrue("Changing a method to be abstract will break binary compatibility.", bcs.size() == 1);
}
Also used : BinaryCompatibilityStatus(org.apache.aries.versioning.utils.BinaryCompatibilityStatus) ClassReader(org.objectweb.asm.ClassReader) SemanticVersioningClassVisitor(org.apache.aries.versioning.utils.SemanticVersioningClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) Test(org.junit.Test)

Example 18 with SemanticVersioningClassVisitor

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

the class BundleCompatibility method checkAdditionalClassOrXsds.

/**
   * Check whether the package has gained additional class or xsd files. If yes, log a minor change.
   *
   * @param pkgName
   * @param curClazz
   * @param curXsds
   * @param minorChange
   */
private void checkAdditionalClassOrXsds(String pkgName, Map<String, IFile> curClazz, Map<String, IFile> curXsds, VersionChangeReason minorChange) {
    String reason;
    Collection<IFile> ifiles = curClazz.values();
    Iterator<IFile> iterator = ifiles.iterator();
    while (iterator.hasNext()) {
        IFile ifile = iterator.next();
        String changeClass = ifile.getName();
        SemanticVersioningClassVisitor cv = getVisitor(ifile, newJarsLoader);
        if (cv.getClassDeclaration() != null) {
            // If this is a public/protected class, it will need to increase the minor version of the package.
            minorChange.setChange(true);
            if (minorChange.isChange()) {
                reason = "The package " + pkgName + " has gained at least one class : " + getClassName(changeClass) + ".";
                minorChange.update(reason, changeClass, false);
                break;
            }
        }
    }
    if (!!!(minorChange.isChange() || curXsds.isEmpty())) {
        /// a new xsd file was added, it is a minor change
        IFile firstXsd = null;
        Iterator<IFile> xsdIterator = curXsds.values().iterator();
        firstXsd = xsdIterator.next();
        reason = "In the package " + pkgName + ", The schema file(s) are added: " + curXsds.keySet() + ".";
        minorChange.update(reason, firstXsd.getName(), false);
    }
}
Also used : IFile(org.apache.aries.util.filesystem.IFile) SemanticVersioningClassVisitor(org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)

Example 19 with SemanticVersioningClassVisitor

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

the class BinaryCompatibilityTest method test_jdk_chap13_5_3_2.

/**
     * Adding a method in an interface should not break compatibility.
     */
@Test
public void test_jdk_chap13_5_3_2() {
    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, "getFoo", "()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, "getFoo", "()I", null, null).visitEnd();
    cw.visitMethod(ACC_PUBLIC, "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 a method in an interface should not break compatibility.", newCV.getClassDeclaration().getBinaryCompatibleStatus(oldCV.getClassDeclaration()).isCompatible());
}
Also used : ClassReader(org.objectweb.asm.ClassReader) SemanticVersioningClassVisitor(org.apache.aries.versioning.utils.SemanticVersioningClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) Test(org.junit.Test)

Example 20 with SemanticVersioningClassVisitor

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

the class BinaryCompatibilityTest method test_jdk_chap13_4_12_1.

/**
     * Adding a parameter is a binary compatibility change.
     */
@Test
public void test_jdk_chap13_4_12_1() {
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_5, ACC_PUBLIC, "pkg/Test", null, "versioning/java/files/TestA", null);
    cw.visitMethod(ACC_PUBLIC, "getCooLen", "(Ljava/lang/String;)I", null, null).visitEnd();
    cw.visitEnd();
    byte[] oldBytes = cw.toByteArray();
    cw = new ClassWriter(0);
    cw.visit(V1_5, ACC_PUBLIC, "pkg/Test", null, "versioning/java/files/TestA", null);
    cw.visitField(ACC_PUBLIC + ACC_TRANSIENT, "aa", "Ljava/lang/String;", null, new String("newBar")).visitEnd();
    cw.visitMethod(ACC_PUBLIC, "getCooLen", "(Ljava/lang/String;I)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);
    BinaryCompatibilityStatus bcs = newCV.getClassDeclaration().getBinaryCompatibleStatus((oldCV.getClassDeclaration()));
    assertEquals("The method int getCooLen(java.lang.String) has been deleted or its return type or parameter list has changed.", bcs.get(0));
    assertTrue("Changing a parameter list will break binary compatibility.", bcs.size() == 1);
}
Also used : BinaryCompatibilityStatus(org.apache.aries.versioning.utils.BinaryCompatibilityStatus) ClassReader(org.objectweb.asm.ClassReader) SemanticVersioningClassVisitor(org.apache.aries.versioning.utils.SemanticVersioningClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) Test(org.junit.Test)

Aggregations

SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)55 ClassReader (org.objectweb.asm.ClassReader)53 Test (org.junit.Test)52 ClassWriter (org.objectweb.asm.ClassWriter)52 BinaryCompatibilityStatus (org.apache.aries.versioning.utils.BinaryCompatibilityStatus)33 IFile (org.apache.aries.util.filesystem.IFile)2 MethodDeclaration (org.apache.aries.versioning.utils.MethodDeclaration)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ClassDeclaration (org.apache.aries.versioning.utils.ClassDeclaration)1 FieldDeclaration (org.apache.aries.versioning.utils.FieldDeclaration)1 SerialVersionClassVisitor (org.apache.aries.versioning.utils.SerialVersionClassVisitor)1