Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(CtLineElementComparator): one can add two values to an annotation #3640

Merged
merged 5 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
public class CtLineElementComparator implements Comparator<CtElement>, Serializable {

/**
* Returns 0 if o1 has the same position as o2, or both positions are invalid.
* Returns -1 if o1 is before o2 in the file, or o1 has no valid position.
* Returns 0 if o1 has the same position as o2, or both positions are invalid and o1.equals(o2).
* Returns -1 if o1 is before o2 in the file, or o1 has no valid position or both positions are invalid !o1.equals(o2).
* Returns 1 if o2 is after o1 in the file, or o2 has no valid position.
*/
@Override
public int compare(CtElement o1, CtElement o2) {
if (!o1.getPosition().isValidPosition() && !o2.getPosition().isValidPosition()) {
return 0;
return o1.equals(o2) ? 0 : -1;
Copy link
Collaborator Author

@nharrand nharrand Oct 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to respect the antisymetric property, this line could be replaced with

return o1.equals(o2) ? 0 : ((o1.hashCode() < o2.hashCode()) ? -1 : 1);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix. At first glance I see no problem with transitive and antisymmetric property. LGTM.

}
if (!o1.getPosition().isValidPosition()) {
return -1;
Expand Down
28 changes: 26 additions & 2 deletions src/test/java/spoon/test/annotation/AnnotationValuesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.junit.Test;
import spoon.Launcher;
import spoon.SpoonAPI;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtFieldAccess;
Expand All @@ -30,12 +31,15 @@
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.annotation.testclasses.AnnotationValues;
import spoon.test.annotation.testclasses.BoundNumber;
import spoon.test.annotation.testclasses.Summary;

import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -142,8 +146,10 @@ public void testAnnotationPrintAnnotation() {
launcher.getEnvironment().setCommentEnabled(false); // avoid getting the comment for the equals
launcher.buildModel();

assertEquals(strCtClassOracle,
launcher.getFactory().Class().getAll().get(0).getElements(new TypeFilter<>(CtClass.class)).get(2).toString());
assertEquals(
strCtClassOracle,
launcher.getFactory().Class().getAll().get(0).getElements(new TypeFilter<>(CtClass.class)).get(2).toString()
);
}

private static final String nl = System.lineSeparator();
Expand Down Expand Up @@ -202,4 +208,22 @@ public Request isNotAnnotated() {
return myself;
}
}

@Test
public void testIssue3639() {
SpoonAPI spoon = new Launcher();

CtTypeReference<Summary> typeRef = spoon.getFactory().createCtTypeReference(Summary.class);
CtAnnotation<Summary> annotation = spoon.getFactory().createAnnotation(typeRef);

annotation.addValue("title", "First summary");
annotation.addValue("date", "2020-10-05");

//contract: All added values are added to the annotations
assertEquals(2, annotation.getAllValues().size());
assertTrue(annotation.getAllValues().containsKey("title"));
assertTrue(annotation.getAllValues().containsKey("date"));
assertEquals("\"First summary\"", annotation.getAllValues().get("title").toString());
assertEquals("\"2020-10-05\"", annotation.getAllValues().get("date").toString());
}
}
6 changes: 6 additions & 0 deletions src/test/java/spoon/test/annotation/testclasses/Summary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package spoon.test.annotation.testclasses;

public @interface Summary {
String title();
String date();
}