Skip to content

Commit

Permalink
#167 method throwing in new version RuntimeException is no longer rep…
Browse files Browse the repository at this point in the history
…orted to be source incompatible
  • Loading branch information
siom79 committed Mar 24, 2017
1 parent 25d05a0 commit 17b9bdf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
7 changes: 4 additions & 3 deletions japicmp/src/main/java/japicmp/cmp/JarArchiveComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ private String setupClasspath(ClassPool classPool, List<String> classPathEntries
}

private String appendSystemClassPath(ClassPool classPool, String classPathAsString) {
String retVal = classPathAsString;
classPool.appendSystemPath();
if (classPathAsString.length() > 0 && !classPathAsString.endsWith(File.pathSeparator)) {
classPathAsString += File.pathSeparator;
if (retVal.length() > 0 && !retVal.endsWith(File.pathSeparator)) {
retVal += File.pathSeparator;
}
return classPathAsString;
return retVal;
}

private String appendUserDefinedClassPathEntries(ClassPool classPool, List<String> classPathEntries) {
Expand Down
19 changes: 14 additions & 5 deletions japicmp/src/main/java/japicmp/model/JApiException.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,27 @@ public JApiException(JarArchiveComparator jarArchiveComparator, String name, Opt
this.checkedException = isCheckedException(ctClassOptional, jarArchiveComparator);
}

private boolean isCheckedException(Optional<CtClass> ctClassOptional, JarArchiveComparator jarArchiveComparator) {
boolean checked = false;
private boolean isCheckedException(Optional<CtClass> ctClassOptional, JarArchiveComparator jarArchiveComparator) throws OutOfMemoryError {
boolean checkedException = false;
if (ctClassOptional.isPresent()) {
boolean subClassOfException = false;
CtClass ctClass = ctClassOptional.get();
Optional<CtClass> exceptionOptional = jarArchiveComparator.loadClass(JarArchiveComparator.ArchiveType.NEW, Exception.class.getName());
if (exceptionOptional.isPresent()) {
CtClass ctClass = ctClassOptional.get();
if (ctClass.subclassOf(exceptionOptional.get())) {
checked = true;
subClassOfException = true;
}
}
if (subClassOfException) {
Optional<CtClass> runtimeExceptionOptional = jarArchiveComparator.loadClass(JarArchiveComparator.ArchiveType.NEW, RuntimeException.class.getName());
if (runtimeExceptionOptional.isPresent()) {
if (!ctClass.subclassOf(runtimeExceptionOptional.get())) {
checkedException = true;
}
}
}
}
return checked;
return checkedException;
}

@XmlAttribute(name = "name")
Expand Down
28 changes: 28 additions & 0 deletions japicmp/src/test/java/japicmp/compat/CompatibilityChangesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,34 @@ public List<CtClass> createNewClasses(ClassPool classPool) throws Exception {
assertThat(method.getCompatibilityChanges(), hasItem(JApiCompatibilityChange.METHOD_NOW_THROWS_CHECKED_EXCEPTION));
}

@Test
public void testMethodThrowsNewRuntimeException() 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 ctClass = CtClassBuilder.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 = CtClassBuilder.create().name("japicmp.Test").addToClassPool(classPool);
CtMethodBuilder.create().publicAccess().name("method").exceptions(new CtClass[] {classPool.get("java.lang.RuntimeException")}).addToClass(ctClass);
return Collections.singletonList(ctClass);
}
});
JApiClass jApiClass = getJApiClass(jApiClasses, "japicmp.Test");
JApiMethod method = getJApiMethod(jApiClass.getMethods(), "method");
assertThat(method.getExceptions().size(), Is.is(1));
assertThat(method.getExceptions().get(0).getChangeStatus(), Is.is(JApiChangeStatus.NEW));
assertThat(method.getExceptions().get(0).isCheckedException(), Is.is(false));
assertThat(method.isBinaryCompatible(), is(true));
assertThat(method.isSourceCompatible(), is(true));
assertThat(method.getCompatibilityChanges().size(), is(0));
}

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

0 comments on commit 17b9bdf

Please sign in to comment.