Skip to content

Commit

Permalink
fix(test): much stronger contracts for setter spec and tests
Browse files Browse the repository at this point in the history
Close PR #975
  • Loading branch information
monperrus authored and tdurieux committed Nov 17, 2016
1 parent 9272284 commit cac5ade
Show file tree
Hide file tree
Showing 28 changed files with 423 additions and 336 deletions.
6 changes: 6 additions & 0 deletions src/main/java/spoon/reflect/code/CtTypeAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
*/
package spoon.reflect.code;

import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.reference.CtTypeReference;
import spoon.support.DerivedProperty;
import spoon.support.UnsettableProperty;

/**
* This code element represents a type reference usable as an expression.
Expand Down Expand Up @@ -72,6 +74,10 @@ public interface CtTypeAccess<A> extends CtExpression<Void> {
@DerivedProperty
CtTypeReference<Void> getType();

@Override
@UnsettableProperty
<C extends CtTypedElement> C setType(CtTypeReference<Void> type);

@Override
CtTypeAccess<A> clone();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public interface CtAnonymousExecutable extends CtExecutable<Void>, CtTypeMember
@Override
@UnsettableProperty
<T extends CtExecutable<Void>> T setParameters(List<CtParameter<?>> parameters);

@Override
@UnsettableProperty
<C extends CtTypedElement> C setType(CtTypeReference<Void> type);
}
7 changes: 7 additions & 0 deletions src/main/java/spoon/reflect/declaration/CtConstructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package spoon.reflect.declaration;

import spoon.reflect.reference.CtTypeReference;
import spoon.support.UnsettableProperty;

/**
* This element defines a constructor declaration.
*/
Expand All @@ -28,4 +31,8 @@ public interface CtConstructor<T> extends CtExecutable<T>, CtTypeMember, CtForma

@Override
CtConstructor<T> clone();

@Override
@UnsettableProperty
<C extends CtTypedElement> C setType(CtTypeReference<T> type);
}
11 changes: 11 additions & 0 deletions src/main/java/spoon/reflect/declaration/CtTypeParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,15 @@ public interface CtTypeParameter extends CtType<Object> {
@UnsettableProperty
<C extends CtType<Object>> C setTypeMembers(List<CtTypeMember> members);

@Override
@UnsettableProperty
<C extends CtType<Object>> C setFields(List<CtField<?>> fields);

@Override
@UnsettableProperty
<C extends CtType<Object>> C setMethods(Set<CtMethod<?>> methods);

@Override
@UnsettableProperty
<C extends CtType<Object>> C setNestedTypes(Set<CtType<?>> nestedTypes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ public void writeHeader(List<CtType<?>> types, Collection<CtTypeReference<?>> im
writeComment(ctType, CommentOffset.TOP_FILE);
printer.writeln().writeln().writeTabs();
}
prettyPrinter.scan(types.get(0).getPackage());
// writing the header package
if (!types.get(0).getPackage().isUnnamedPackage()) {
printer.write("package " + types.get(0).getPackage().getQualifiedName() + ";");
}
printer.writeln().writeln().writeTabs();
if (env.isAutoImports()) {
for (CtTypeReference<?> ref : imports) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/support/DefaultCoreFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ public CtElement create(Class<? extends CtElement> klass) {
if (klass.equals(spoon.reflect.reference.CtWildcardReference.class)) {
return createWildcardReference();
}
throw new IllegalArgumentException("not instantiable by CoreFactory()");
throw new IllegalArgumentException("not instantiable by CoreFactory(): " + klass);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import spoon.reflect.reference.CtCatchVariableReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;
import spoon.support.UnsettableProperty;
import spoon.support.reflect.declaration.CtElementImpl;

import java.util.ArrayList;
Expand Down Expand Up @@ -73,8 +74,10 @@ public CtTypeReference<T> getType() {
}

@Override
@UnsettableProperty
public <C extends CtVariable<T>> C setDefaultExpression(CtExpression<T> defaultExpression) {
throw new UnsupportedOperationException();
// unsettable property
return (C) this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public List<CtExpression<?>> getArguments() {

@Override
public CtExecutableReference<T> getExecutable() {
if (executable == null) {
// default reference
executable = getFactory().Core().createExecutableReference();
}
return executable;
}

Expand Down Expand Up @@ -173,6 +177,9 @@ public CtTypeReference<T> getType() {

@Override
public <C extends CtTypedElement> C setType(CtTypeReference<T> type) {
if (type != null) {
type.setParent(this);
}
if (getExecutable() != null) {
getExecutable().setType(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public void removeArgument(CtExpression<?> argument) {

@Override
public CtExecutableReference<T> getExecutable() {
if (executable == null) {
// default reference
executable = getFactory().Core().createExecutableReference();
}
return executable;
}

Expand Down Expand Up @@ -150,6 +154,9 @@ public CtTypeReference<T> getType() {

@Override
public <C extends CtTypedElement> C setType(CtTypeReference<T> type) {
if (type != null) {
type.setParent(this);
}
if (getExecutable() != null) {
getExecutable().setType(type);
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/spoon/support/reflect/code/CtLambdaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import spoon.reflect.visitor.CtVisitor;
import spoon.support.reflect.declaration.CtElementImpl;
import spoon.support.util.QualifiedNameBasedSortedSet;
import spoon.support.visitor.SignaturePrinter;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -160,7 +161,9 @@ public boolean removeThrownType(CtTypeReference<? extends Throwable> throwType)

@Override
public String getSignature() {
throw new UnsupportedOperationException();
final SignaturePrinter pr = new SignaturePrinter();
pr.scan(this);
return pr.getSignature();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;
import spoon.support.UnsettableProperty;

public class CtTypeAccessImpl<A> extends CtExpressionImpl<Void> implements CtTypeAccess<A> {
private CtTypeReference<Void> voidType;
Expand Down Expand Up @@ -54,6 +55,7 @@ public CtTypeReference<Void> getType() {
}

@Override
@UnsettableProperty
public <C extends CtTypedElement> C setType(CtTypeReference<Void> type) {
// type is used in setAccessedType now.
return (C) this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import spoon.reflect.declaration.CtType;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;
import spoon.support.UnsettableProperty;

import java.lang.annotation.Annotation;
import java.util.Collections;
Expand Down Expand Up @@ -52,8 +53,15 @@ public Set<CtTypeReference<?>> getSuperInterfaces() {
}

@Override
@UnsettableProperty
public <C extends CtType<T>> C setSuperclass(CtTypeReference<?> superClass) {
return (C) this;
}

@Override
@UnsettableProperty
public <C extends CtType<T>> C setSuperInterfaces(Set<CtTypeReference<?>> interfaces) {
throw new UnsupportedOperationException("You can't have super interfaces in an annotation.");
return (C) this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;
import spoon.support.UnsettableProperty;

import java.util.EnumSet;
import java.util.List;
Expand Down Expand Up @@ -110,18 +111,23 @@ public List<CtParameter<?>> getParameters() {
}

@Override
@UnsettableProperty
public CtExecutable setParameters(List list) {
throw new UnsupportedOperationException("You can't have parameters in a anonymous executable");
// unsettable property
return this;
}

@Override
@UnsettableProperty
public CtExecutable addParameter(CtParameter parameter) {
throw new UnsupportedOperationException("You can't have parameters in a anonymous executable");
// unsettable property
return this;
}

@Override
@UnsettableProperty
public boolean removeParameter(CtParameter parameter) {
throw new UnsupportedOperationException("You can't have parameters in a anonymous executable");
return false;
}

@Override
Expand All @@ -130,18 +136,24 @@ public Set<CtTypeReference<? extends Throwable>> getThrownTypes() {
}

@Override
@UnsettableProperty
public CtExecutable setThrownTypes(Set thrownTypes) {
throw new UnsupportedOperationException("You can't have throw types in a anonymous executable");
// unsettable property
return this;
}

@Override
@UnsettableProperty
public CtExecutable addThrownType(CtTypeReference throwType) {
throw new UnsupportedOperationException("You can't have throw types in a anonymous executable");
// unsettable property
return this;
}

@Override
@UnsettableProperty
public boolean removeThrownType(CtTypeReference throwType) {
throw new UnsupportedOperationException("You can't have throw types in a anonymous executable");
// unsettable property
return false;
}

@Override
Expand All @@ -150,8 +162,10 @@ public String getSimpleName() {
}

@Override
@UnsettableProperty
public <T extends CtNamedElement> T setSimpleName(String simpleName) {
throw new UnsupportedOperationException("You can't have a name in a anonymous executable");
// unsettable property
return (T) this;
}

@Override
Expand All @@ -160,8 +174,10 @@ public CtTypeReference<Void> getType() {
}

@Override
@UnsettableProperty
public <C extends CtTypedElement> C setType(CtTypeReference<Void> type) {
throw new UnsupportedOperationException("An anonymous executable isn't typed");
// unsettable property
return (C) this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;
import spoon.support.UnsettableProperty;
import spoon.support.compiler.jdt.JDTBasedSpoonCompiler;
import spoon.support.reflect.code.CtStatementImpl;
import spoon.support.reflect.eval.VisitorPartialEvaluator;
Expand Down Expand Up @@ -216,8 +217,9 @@ public String getLabel() {
}

@Override
@UnsettableProperty
public <C extends CtStatement> C setLabel(String label) {
throw new UnsupportedOperationException("cannot set a label on a class declaration");
return (C) this;
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import spoon.reflect.reference.CtTypeParameterReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;
import spoon.support.DerivedProperty;
import spoon.support.UnsettableProperty;

import java.util.ArrayList;
import java.util.EnumSet;
Expand All @@ -51,8 +53,9 @@ public void accept(CtVisitor visitor) {
}

@Override
@UnsettableProperty
public <C extends CtNamedElement> C setSimpleName(String simpleName) {
throw new UnsupportedOperationException();
return (C) this;
}

@Override
Expand All @@ -67,6 +70,7 @@ public CtType<T> getDeclaringType() {
}

@Override
@DerivedProperty
public CtTypeReference<T> getType() {
if (getDeclaringType() == null) {
return null;
Expand All @@ -75,8 +79,10 @@ public CtTypeReference<T> getType() {
}

@Override
@UnsettableProperty
public <C extends CtTypedElement> C setType(CtTypeReference<T> type) {
throw new UnsupportedOperationException();
// unsettable property
return (C) this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package spoon.support.reflect.declaration;

import spoon.reflect.declaration.CtInterface;
import spoon.reflect.declaration.CtType;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;
import spoon.support.UnsettableProperty;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -67,4 +69,11 @@ public Collection<CtExecutableReference<?>> getDeclaredExecutables() {
public CtInterface<T> clone() {
return (CtInterface<T>) super.clone();
}

@Override
@UnsettableProperty
public <C extends CtType<T>> C setSuperclass(CtTypeReference<?> superClass) {
// unsettable property
return (C) this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import spoon.reflect.reference.CtParameterReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;
import spoon.support.UnsettableProperty;

import java.util.EnumSet;
import java.util.Set;
Expand Down Expand Up @@ -70,8 +71,10 @@ public CtTypeReference<T> getType() {
}

@Override
@UnsettableProperty
public <C extends CtVariable<T>> C setDefaultExpression(CtExpression<T> defaultExpression) {
throw new UnsupportedOperationException();
// unsettable property
return (C) this;
}

@Override
Expand Down
Loading

0 comments on commit cac5ade

Please sign in to comment.