Skip to content

Commit

Permalink
Merge branch 'testTypeMembersOrder2' into testTypeMembersOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus committed Feb 18, 2018
2 parents 5557733 + 6dd6f07 commit fbf0df6
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
public class CtLineElementComparator implements Comparator<CtElement>, Serializable {

/**
* Compares two program elements.
* Reurns -1 if o1 is before o2 in the file
*/
public int compare(CtElement o1, CtElement o2) {
if (o1.getPosition() == null) {
return -1;
}

if (o1.getPosition() == null || o2.getPosition() == null) {
if (o2.getPosition() == null) {
// ensures that compare(x,y) = - compare(y,x)
return o1.hashCode() > o2.hashCode() ? 1 : -1;
return 1;
}

int pos1 = o1.getPosition().getSourceStart();
Expand Down
60 changes: 30 additions & 30 deletions src/main/java/spoon/support/compiler/jdt/ParentExiter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,65 +37,65 @@
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.ast.UnionTypeReference;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtLoop;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtTargetedExpression;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.code.CtArrayWrite;
import spoon.reflect.code.CtArrayRead;
import spoon.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.CtArrayAccess;
import spoon.reflect.code.CtArrayRead;
import spoon.reflect.code.CtArrayWrite;
import spoon.reflect.code.CtAssert;
import spoon.reflect.code.CtAssignment;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtCase;
import spoon.reflect.code.CtCatch;
import spoon.reflect.code.CtCatchVariable;
import spoon.reflect.code.CtConditional;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtDo;
import spoon.reflect.code.CtExecutableReferenceExpression;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtFor;
import spoon.reflect.code.CtForEach;
import spoon.reflect.code.CtWhile;
import spoon.reflect.code.CtConditional;
import spoon.reflect.code.CtIf;
import spoon.reflect.code.CtSuperAccess;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtLoop;
import spoon.reflect.code.CtNewArray;
import spoon.reflect.code.CtNewClass;
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtExecutableReferenceExpression;
import spoon.reflect.code.CtReturn;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtSuperAccess;
import spoon.reflect.code.CtSwitch;
import spoon.reflect.code.CtSynchronized;
import spoon.reflect.code.CtTargetedExpression;
import spoon.reflect.code.CtThisAccess;
import spoon.reflect.code.CtThrow;
import spoon.reflect.code.CtTry;
import spoon.reflect.code.CtTryWithResource;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.code.CtUnaryOperator;
import spoon.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.CtThisAccess;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtWhile;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtAnnotatedElementType;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtAnnotationMethod;
import spoon.reflect.declaration.CtAnonymousExecutable;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtEnum;
import spoon.reflect.declaration.CtEnumValue;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtFormalTypeDeclarer;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtVariable;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtEnum;
import spoon.reflect.declaration.CtAnnotationMethod;
import spoon.reflect.declaration.CtAnonymousExecutable;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtEnumValue;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.declaration.CtAnnotatedElementType;
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.declaration.CtVariable;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtIntersectionTypeReference;
import spoon.reflect.reference.CtTypeParameterReference;
Expand Down Expand Up @@ -470,10 +470,10 @@ public <T> void visitCtCatchVariable(CtCatchVariable<T> e) {
public <T> void visitCtClass(CtClass<T> ctClass) {
if (child instanceof CtConstructor) {
CtConstructor<T> c = (CtConstructor<T>) child;
ctClass.addConstructor(c);
if (c.getPosition() != null && c.getPosition().getSourceStart() == -1) {
c.setImplicit(true);
}
ctClass.addConstructor(c);
}
if (child instanceof CtAnonymousExecutable) {
ctClass.addAnonymousExecutable((CtAnonymousExecutable) child);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@
import spoon.support.reflect.CtModifierHandler;
import spoon.support.util.QualifiedNameBasedSortedSet;
import spoon.support.util.SignatureBasedSortedSet;
import spoon.support.util.SortedList;
import spoon.support.visitor.ClassTypingContext;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -143,7 +143,7 @@ public <C extends CtType<T>> C addTypeMemberAt(int position, CtTypeMember member
return (C) this;
}
if (this.typeMembers == CtElementImpl.<CtTypeMember>emptyList()) {
this.typeMembers = new SortedList<>(new CtLineElementComparator());
this.typeMembers = new ArrayList<>();
}
if (!this.typeMembers.contains(member)) {
member.setParent(this);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/spoon/support/util/SortedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public boolean add(E o) {
return super.add(o);
}

@Override
public void add(int index, E element) {
throw new IllegalArgumentException("cannot force a position with a sorted list that has its own ordering");
}

@Override
public boolean addAll(Collection<? extends E> c) {
boolean ret = true;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/compilation/CompilationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void testNewInstance() throws Exception {
"class X implements spoon.test.compilation.Ifoo { public int foo() {int i=0; return i;} }").compile();
c.addModifier(ModifierKind.PUBLIC); // required otherwise java.lang.IllegalAccessException at runtime when instantiating

CtBlock body = c.getElements(new TypeFilter<>(CtBlock.class)).get(1);
CtBlock body = c.getElements(new TypeFilter<>(CtMethod.class)).get(0).getBody();
Ifoo o = c.newInstance();
assertEquals(0, o.foo());
for (int i = 1; i <= 10; i++) {
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/spoon/test/generics/GenericsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,9 @@ public boolean matches(CtTypeReference element) {

// spoon.test.generics.testclasses.Tacos<K, java.lang.String>.Burritos<K, V>
CtTypeReference<?> burritosRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "burritos")).first(CtVariable.class).getType();
assertEquals(true, burritosRef.isGenerics());
// now that the order of type members is correct
// this burritos is indeed "IBurritos<?, ?> burritos = new Burritos<>()" with no generics
assertEquals(false, burritosRef.isGenerics());

// int
CtTypeReference<?> nbTacosRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "nbTacos")).first(CtVariable.class).getType();
Expand Down
1 change: 1 addition & 0 deletions src/test/java/spoon/test/secondaryclasses/ClassesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public void testAnonymousClassInStaticField() throws Exception {
final CtType<Pozole> type = buildClass(Pozole.class);

final CtNewClass<?> anonymousClass = type.getField("CONFLICT_HOOK").getElements(new TypeFilter<>(CtNewClass.class)).get(1);
assertEquals("ColorModel", anonymousClass.getAnonymousClass().getSuperclass().getSimpleName());
final CtVariableRead<?> ctVariableRead = anonymousClass.getElements(new TypeFilter<>(CtVariableRead.class)).get(2);
final CtVariable<?> declaration = ctVariableRead.getVariable().getDeclaration();

Expand Down

0 comments on commit fbf0df6

Please sign in to comment.