Skip to content

Commit

Permalink
#363 added METHOD_DEFAULT_NOW_STATIC
Browse files Browse the repository at this point in the history
  • Loading branch information
siom79 committed Sep 27, 2023
1 parent 0a600c2 commit 9118a7c
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 33 deletions.
6 changes: 6 additions & 0 deletions japicmp-testbase/japicmp-test-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,12 @@
<sourceCompatible>true</sourceCompatible>
<semanticVersionLevel>PATCH</semanticVersionLevel>
</overrideCompatibilityChangeParameter>
<overrideCompatibilityChangeParameter>
<compatibilityChange>METHOD_DEFAULT_NOW_STATIC</compatibilityChange>
<binaryCompatible>true</binaryCompatible>
<sourceCompatible>true</sourceCompatible>
<semanticVersionLevel>PATCH</semanticVersionLevel>
</overrideCompatibilityChangeParameter>
</overrideCompatibilityChangeParameters>
<breakBuildOnBinaryIncompatibleModifications>true</breakBuildOnBinaryIncompatibleModifications>
<breakBuildOnSourceIncompatibleModifications>true</breakBuildOnSourceIncompatibleModifications>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,26 @@ public interface InterfaceWithStaticMethod {
public interface InterfaceWithDefaultMethod {

}

public interface InterfaceAbstractMethodToStatic {
void doSomething();
}

public static class ClassAbstractMethodToStatic implements InterfaceAbstractMethodToStatic {

@Override
public void doSomething() {
System.out.println("test");
}
}

public interface InterfaceDefaultMethodToStatic {
default void doSomething() {
System.out.println("Test");
}
}

public static class ClassDefaultMethodToStatic implements InterfaceDefaultMethodToStatic {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,27 @@ default void test() {

}
}

public interface InterfaceAbstractMethodToStatic {
static void doSomething() {

}
}

public static class ClassAbstractMethodToStatic implements InterfaceAbstractMethodToStatic {

public void doSomething() {
System.out.println("test");
}
}

public interface InterfaceDefaultMethodToStatic {
static void doSomething() {
System.out.println("Test");
}
}

public static class ClassDefaultMethodToStatic implements InterfaceDefaultMethodToStatic {

}
}
2 changes: 1 addition & 1 deletion japicmp-testbase/japicmp-test-vx-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<groupId>com.github.siom79.japicmp</groupId>
<artifactId>japicmp-test-v2</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
<scope>provided</scope> <!-- for compilation order -->
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package japicmp.test.client;

import japicmp.test.Interfaces;

public class ManualTest {

public static void main(String[] args) {
new Interfaces.ClassAbstractMethodToStatic().doSomething();
new Interfaces.ClassDefaultMethodToStatic().doSomething();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,12 @@ private void checkAbstractMethod(JApiClass jApiClass, Map<String, JApiClass> cla
}
JApiModifier<StaticModifier> staticModifier = method.getStaticModifier();
if (staticModifier.hasChangedFromTo(StaticModifier.NON_STATIC, StaticModifier.STATIC)) {
addCompatibilityChange(method, JApiCompatibilityChange.METHOD_NON_STATIC_IN_INTERFACE_NOW_STATIC);
// changed from default to static
if (abstractModifier.getChangeStatus() == JApiChangeStatus.UNCHANGED && abstractModifier.getNewModifier().isPresent() && abstractModifier.getNewModifier().get() == AbstractModifier.NON_ABSTRACT) {
addCompatibilityChange(method, JApiCompatibilityChange.METHOD_DEFAULT_NOW_STATIC);
} else {
addCompatibilityChange(method, JApiCompatibilityChange.METHOD_NON_STATIC_IN_INTERFACE_NOW_STATIC);
}
} else if (staticModifier.hasChangedFromTo(StaticModifier.STATIC, StaticModifier.NON_STATIC)) {
addCompatibilityChange(method, JApiCompatibilityChange.METHOD_STATIC_IN_INTERFACE_NO_LONGER_STATIC);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum JApiCompatibilityChange {
METHOD_ABSTRACT_ADDED_IN_IMPLEMENTED_INTERFACE(true, false, JApiSemanticVersionLevel.MINOR),
METHOD_DEFAULT_ADDED_IN_IMPLEMENTED_INTERFACE(true, true, JApiSemanticVersionLevel.MINOR),
METHOD_NEW_DEFAULT(true, true, JApiSemanticVersionLevel.MINOR),
METHOD_DEFAULT_NOW_STATIC(false, false, JApiSemanticVersionLevel.MAJOR),
METHOD_NEW_STATIC_ADDED_TO_INTERFACE(true, true, JApiSemanticVersionLevel.MINOR),
METHOD_MOVED_TO_SUPERCLASS(true, true, JApiSemanticVersionLevel.PATCH),
METHOD_ABSTRACT_NOW_DEFAULT(false, false, JApiSemanticVersionLevel.MAJOR),
Expand Down
63 changes: 32 additions & 31 deletions japicmp/src/test/java/japicmp/compat/CompatibilityChangesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1154,37 +1154,6 @@ public List<CtClass> createNewClasses(ClassPool classPool) throws Exception {
assertThat(jApiMethod.isSourceCompatible(), is(true));
}

@Test
public void testMethodNotStaticChangesToStaticInInterface() throws Exception {
JarArchiveComparatorOptions options = new JarArchiveComparatorOptions();
options.setIncludeSynthetic(true);
options.setAccessModifier(AccessModifier.PRIVATE);
List<JApiClass> jApiClasses = ClassesHelper.compareClasses(options, new ClassesHelper.ClassesGenerator() {
@Override
public List<CtClass> createOldClasses(ClassPool classPool) throws Exception {
CtClass ctClass = CtInterfaceBuilder.create().name("japicmp.Test").addToClassPool(classPool);
CtMethodBuilder.create().publicAccess().name("method").addToClass(ctClass);
return Collections.singletonList(ctClass);
}

@Override
public List<CtClass> createNewClasses(ClassPool classPool) throws Exception {
CtClass ctClass = CtInterfaceBuilder.create().name("japicmp.Test").addToClassPool(classPool);
CtMethodBuilder.create().publicAccess().staticAccess().name("method").addToClass(ctClass);
return Collections.singletonList(ctClass);
}
});
JApiClass jApiClass = getJApiClass(jApiClasses, "japicmp.Test");
assertThat(jApiClass.getChangeStatus(), is(JApiChangeStatus.MODIFIED));
assertThat(jApiClass.isBinaryCompatible(), is(true));
assertThat(jApiClass.isSourceCompatible(), is(true));
JApiMethod jApiMethod = getJApiMethod(jApiClass.getMethods(), "method");
assertThat(jApiMethod.getChangeStatus(), is(JApiChangeStatus.MODIFIED));
assertThat(jApiMethod.getCompatibilityChanges(), hasItem(JApiCompatibilityChange.METHOD_NON_STATIC_IN_INTERFACE_NOW_STATIC));
assertThat(jApiMethod.isBinaryCompatible(), is(true));
assertThat(jApiMethod.isSourceCompatible(), is(true));
}

@Test
public void testMethodStaticChangesToNotStaticInInterface() throws Exception {
JarArchiveComparatorOptions options = new JarArchiveComparatorOptions();
Expand Down Expand Up @@ -1654,6 +1623,38 @@ public List<CtClass> createNewClasses(ClassPool classPool) throws Exception {
assertThat(jApiMethod.getCompatibilityChanges(), not(hasItem(JApiCompatibilityChange.METHOD_ADDED_TO_INTERFACE)));
}

@Test
public void testInterfaceMethodDefaultToStatic() throws Exception {
JarArchiveComparatorOptions options = new JarArchiveComparatorOptions();
List<JApiClass> jApiClasses = ClassesHelper.compareClasses(options, new ClassesHelper.ClassesGenerator() {
@Override
public List<CtClass> createOldClasses(ClassPool classPool) throws Exception {
CtClass ctInterface = CtInterfaceBuilder.create().name("japicmp.Interface").addToClassPool(classPool);
CtMethodBuilder.create().publicAccess().name("defaultMethodToStatic").addToClass(ctInterface);
CtClass ctClass = CtClassBuilder.create().name("japicmp.Test").implementsInterface(ctInterface).addToClassPool(classPool);
return Arrays.asList(ctClass, ctInterface);
}

@Override
public List<CtClass> createNewClasses(ClassPool classPool) throws Exception {
CtClass ctInterface = CtInterfaceBuilder.create().name("japicmp.Interface").addToClassPool(classPool);
CtMethodBuilder.create().publicAccess().staticAccess().name("defaultMethodToStatic").addToClass(ctInterface);
CtClass ctClass = CtClassBuilder.create().name("japicmp.Test").implementsInterface(ctInterface).addToClassPool(classPool);
return Arrays.asList(ctClass, ctInterface);
}
});
JApiClass jApiClass = getJApiClass(jApiClasses, "japicmp.Test");
assertThat(jApiClass.isBinaryCompatible(), is(true));
assertThat(jApiClass.isSourceCompatible(), is(true));
jApiClass = getJApiClass(jApiClasses, "japicmp.Interface");
assertThat(jApiClass.isBinaryCompatible(), is(false));
assertThat(jApiClass.isSourceCompatible(), is(false));
JApiMethod jApiMethod = getJApiMethod(jApiClass.getMethods(), "defaultMethodToStatic");
assertThat(jApiMethod.isBinaryCompatible(), is(false));
assertThat(jApiMethod.isSourceCompatible(), is(false));
assertThat(jApiMethod.getCompatibilityChanges(), hasItem(JApiCompatibilityChange.METHOD_DEFAULT_NOW_STATIC));
}

@Test
public void testInterfaceDefaultMethodOverriddenInSubInterface() throws Exception {
JarArchiveComparatorOptions options = new JarArchiveComparatorOptions();
Expand Down

0 comments on commit 9118a7c

Please sign in to comment.