Skip to content

Commit

Permalink
Fix all tests and implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
surli committed Oct 27, 2017
1 parent 17f1bf0 commit 09b6fce
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/main/java/spoon/reflect/factory/CodeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public <T> CtLocalVariableReference<T> createLocalVariableReference(CtTypeRefere
* @return a new catch variable declaration
*/
public <T> CtCatchVariable<T> createCatchVariable(CtTypeReference<T> type, String name, ModifierKind...modifierKinds) {
return factory.Core().<T>createCatchVariable().<CtCatchVariable<T>>setSimpleName(name).<CtCatchVariable<T>>setType(type).setModifiers(new HashSet<>(Arrays.asList(modifierKinds)));
return factory.Core().<T>createCatchVariable().<CtCatchVariable<T>>setSimpleName(name).<CtCatchVariable<T>>setType(type).setModifiers(new HashSet<>(Arrays.asList(modifierKinds))).setSourceModifiers(new HashSet<>(Arrays.asList(modifierKinds)));
}

/**
Expand Down Expand Up @@ -491,7 +491,7 @@ public <T> CtStatementList createVariableAssignments(List<? extends CtVariable<T
* @return a field
*/
public <T> CtField<T> createCtField(String name, CtTypeReference<T> type, String exp, ModifierKind... visibilities) {
return factory.Core().createField().<CtField<T>>setModifiers(modifiers(visibilities)).<CtField<T>>setSimpleName(name).<CtField<T>>setType(type)
return factory.Core().createField().<CtField<T>>setModifiers(modifiers(visibilities)).<CtField<T>>setSourceModifiers(modifiers(visibilities)).<CtField<T>>setSimpleName(name).<CtField<T>>setType(type)
.setDefaultExpression(this.<T>createCodeSnippetExpression(exp));
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/spoon/reflect/factory/ConstructorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public <T> CtConstructor<T> create(CtClass<T> target, CtMethod<?> source) {
newConstructor.setDocComment(method.getDocComment());
newConstructor.setFormalCtTypeParameters(method.getFormalCtTypeParameters());
newConstructor.setModifiers(method.getModifiers());
newConstructor.setSourceModifiers(method.getSourceModifiers());
newConstructor.setParameters(method.getParameters());
target.addConstructor(newConstructor);
return newConstructor;
Expand All @@ -101,6 +102,7 @@ public <T> CtConstructor<T> create(CtClass<T> target, Set<ModifierKind> modifier
Set<CtTypeReference<? extends Throwable>> thrownTypes) {
CtConstructor<T> constructor = factory.Core().createConstructor();
constructor.setModifiers(modifiers);
constructor.setSourceModifiers(modifiers);
constructor.setParameters(parameters);
constructor.setThrownTypes(thrownTypes);
target.addConstructor(constructor);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/spoon/reflect/factory/FieldFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public FieldFactory(Factory factory) {
public <T> CtField<T> create(CtType<?> target, Set<ModifierKind> modifiers, CtTypeReference<T> type, String name) {
CtField<T> field = factory.Core().createField();
field.setModifiers(modifiers);
field.setSourceModifiers(modifiers);
field.setType(type);
field.setSimpleName(name);
if (target != null) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/spoon/reflect/factory/MethodFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public <T> CtMethod<T> create(CtType<?> target, Set<ModifierKind> modifiers, CtT
CtMethod<T> method = factory.Core().createMethod();
if (modifiers != null) {
method.setModifiers(modifiers);
method.setSourceModifiers(modifiers);
}
method.setType(returnType);
method.setSimpleName(name);
Expand Down
25 changes: 24 additions & 1 deletion src/main/java/spoon/reflect/visitor/ElementPrinterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,31 @@ public void writeAnnotations(CtElement element) {
}

public void writeModifiers(CtModifiable modifiable) {
List<String> firstPosition = new ArrayList<>(); // visibility: public, private, protected
List<String> secondPosition = new ArrayList<>(); // keywords: static, abstract
List<String> thirdPosition = new ArrayList<>(); // all other things

for (ModifierKind modifierKind : modifiable.getSourceModifiers()) {
printer.writeKeyword(modifierKind.toString()).writeSpace();
if (modifierKind == ModifierKind.PUBLIC || modifierKind == ModifierKind.PRIVATE || modifierKind == ModifierKind.PROTECTED) {
firstPosition.add(modifierKind.toString());
} else if (modifierKind == ModifierKind.ABSTRACT || modifierKind == ModifierKind.STATIC) {
secondPosition.add(modifierKind.toString());
} else {
thirdPosition.add(modifierKind.toString());
}

}

for (String s : firstPosition) {
printer.writeKeyword(s).writeSpace();
}

for (String s : secondPosition) {
printer.writeKeyword(s).writeSpace();
}

for (String s : thirdPosition) {
printer.writeKeyword(s).writeSpace();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,7 @@ public boolean visit(Initializer initializer, MethodScope scope) {
CtAnonymousExecutable b = factory.Core().createAnonymousExecutable();
if (initializer.isStatic()) {
b.addModifier(ModifierKind.STATIC);
b.setSourceModifiers(b.getModifiers());
}
context.enter(b, initializer);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,10 @@ <T> CtParameter<T> createParameter(Argument argument) {
CtParameter<T> p = jdtTreeBuilder.getFactory().Core().createParameter();
p.setSimpleName(CharOperation.charToString(argument.name));
p.setVarArgs(argument.isVarArgs());
p.setModifiers(getModifiers(argument.modifiers));

int modifiers = (argument.binding != null) ? argument.binding.modifiers : argument.modifiers;
p.setModifiers(getModifiers(modifiers));
p.setSourceModifiers(getModifiers(argument.modifiers));
if (argument.binding != null && argument.binding.type != null && argument.type == null) {
p.setType(jdtTreeBuilder.getReferencesBuilder().<T>getTypeReference(argument.binding.type));
p.getType().setImplicit(argument.type == null);
Expand Down Expand Up @@ -646,7 +649,9 @@ CtType<?> createType(TypeDeclaration typeDeclaration) {
}

// Setting modifiers
type.setModifiers(getModifiers(typeDeclaration.modifiers));
int modifiers = (typeDeclaration.binding != null) ? typeDeclaration.binding.modifiers : typeDeclaration.modifiers;
type.setModifiers(getModifiers(modifiers));
type.setSourceModifiers(getModifiers(typeDeclaration.modifiers));

return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public <C extends CtModifiable> C setModifiers(Set<ModifierKind> modifiers) {

@Override
public <T extends CtModifiable> T setSourceModifiers(Set<ModifierKind> sourceModifiers) {
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, MODIFIER, this.sourceModifiers, sourceModifiers);
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.sourceModifiers = EnumSet.noneOf(ModifierKind.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public <C extends CtModifiable> C setModifiers(Set<ModifierKind> modifiers) {

@Override
public <T extends CtModifiable> T setSourceModifiers(Set<ModifierKind> sourceModifiers) {
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, MODIFIER, this.sourceModifiers, sourceModifiers);
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.sourceModifiers = EnumSet.noneOf(ModifierKind.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ public void accept(CtVisitor visitor) {
@Override
public <T extends CtModifiable> T addModifier(ModifierKind modifier) {
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
modifiers = EnumSet.noneOf(ModifierKind.class);
this.modifiers = EnumSet.noneOf(ModifierKind.class);
}
if (sourceModifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.sourceModifiers = EnumSet.noneOf(ModifierKind.class);
}
getFactory().getEnvironment().getModelChangeListener().onSetAdd(this, MODIFIER, this.modifiers, modifier);
modifiers.add(modifier);
sourceModifiers.add(modifier);
return (T) this;
}

Expand All @@ -66,6 +70,7 @@ public boolean removeModifier(ModifierKind modifier) {
return false;
}
getFactory().getEnvironment().getModelChangeListener().onSetDelete(this, MODIFIER, modifiers, modifier);
sourceModifiers.remove(modifier);
return modifiers.remove(modifier);
}

Expand Down Expand Up @@ -99,23 +104,26 @@ public boolean hasModifier(ModifierKind modifier) {
}

@Override
public <T extends CtModifiable> T setModifiers(Set<ModifierKind> modifiers) {
if (modifiers.size() > 0) {
getFactory().getEnvironment().getModelChangeListener().onSetDelete(this, MODIFIER, this.modifiers, new HashSet<>(this.modifiers));
this.modifiers.clear();
for (ModifierKind modifier : modifiers) {
addModifier(modifier);
public <C extends CtModifiable> C setModifiers(Set<ModifierKind> modifiers) {
if (modifiers != null) {
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, MODIFIER, this.modifiers, modifiers);
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.modifiers = EnumSet.noneOf(ModifierKind.class);
} else {
this.modifiers = new HashSet<>(modifiers);
}
}
return (T) this;
return (C) this;
}

@Override
public <T extends CtModifiable> T setSourceModifiers(Set<ModifierKind> sourceModifiers) {
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, MODIFIER, this.sourceModifiers, sourceModifiers);
if (sourceModifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.sourceModifiers = EnumSet.noneOf(ModifierKind.class);
} else {
this.sourceModifiers = new HashSet<>(sourceModifiers);
}
this.sourceModifiers = sourceModifiers;
return (T) this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,25 @@ public boolean hasModifier(ModifierKind modifier) {

@Override
public <C extends CtModifiable> C setModifiers(Set<ModifierKind> modifiers) {
if (modifiers.size() > 0) {
getFactory().getEnvironment().getModelChangeListener().onSetDeleteAll(this, MODIFIER, this.modifiers, new HashSet<>(this.modifiers));
this.modifiers.clear();
for (ModifierKind modifier : modifiers) {
addModifier(modifier);
if (modifiers != null) {
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, MODIFIER, this.modifiers, modifiers);
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.modifiers = EnumSet.noneOf(ModifierKind.class);
} else {
this.modifiers = new HashSet<>(modifiers);
}
}
return (C) this;
}

@Override
public <T extends CtModifiable> T setSourceModifiers(Set<ModifierKind> sourceModifiers) {
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, MODIFIER, this.sourceModifiers, sourceModifiers);
if (sourceModifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.sourceModifiers = EnumSet.noneOf(ModifierKind.class);
} else {
this.sourceModifiers = new HashSet<>(sourceModifiers);
}
this.sourceModifiers = sourceModifiers;
return (T) this;
}

Expand All @@ -180,8 +183,12 @@ public <C extends CtModifiable> C addModifier(ModifierKind modifier) {
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.modifiers = EnumSet.noneOf(ModifierKind.class);
}
if (sourceModifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.sourceModifiers = EnumSet.noneOf(ModifierKind.class);
}
getFactory().getEnvironment().getModelChangeListener().onSetAdd(this, MODIFIER, this.modifiers, modifier);
modifiers.add(modifier);
sourceModifiers.add(modifier);
return (C) this;
}

Expand All @@ -191,6 +198,7 @@ public boolean removeModifier(ModifierKind modifier) {
return false;
}
getFactory().getEnvironment().getModelChangeListener().onSetDelete(this, MODIFIER, modifiers, modifier);
sourceModifiers.remove(modifier);
return modifiers.remove(modifier);
}

Expand Down
17 changes: 10 additions & 7 deletions src/main/java/spoon/support/reflect/declaration/CtFieldImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,25 @@ public boolean hasModifier(ModifierKind modifier) {

@Override
public <C extends CtModifiable> C setModifiers(Set<ModifierKind> modifiers) {
if (modifiers.size() > 0) {
getFactory().getEnvironment().getModelChangeListener().onSetDeleteAll(this, MODIFIER, this.modifiers, new HashSet<>(this.modifiers));
this.modifiers.clear();
for (ModifierKind modifier : modifiers) {
addModifier(modifier);
if (modifiers != null) {
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, MODIFIER, this.modifiers, modifiers);
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.modifiers = EnumSet.noneOf(ModifierKind.class);
} else {
this.modifiers = new HashSet<>(modifiers);
}
}
return (C) this;
}

@Override
public <T extends CtModifiable> T setSourceModifiers(Set<ModifierKind> sourceModifiers) {
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, MODIFIER, this.sourceModifiers, sourceModifiers);
if (sourceModifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.sourceModifiers = EnumSet.noneOf(ModifierKind.class);
} else {
this.sourceModifiers = new HashSet<>(sourceModifiers);
}
this.sourceModifiers = sourceModifiers;
return (T) this;
}

Expand Down
22 changes: 15 additions & 7 deletions src/main/java/spoon/support/reflect/declaration/CtMethodImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,25 @@ public boolean hasModifier(ModifierKind modifier) {

@Override
public <C extends CtModifiable> C setModifiers(Set<ModifierKind> modifiers) {
if (modifiers.size() > 0) {
getFactory().getEnvironment().getModelChangeListener().onSetDeleteAll(this, MODIFIER, this.modifiers, new HashSet<>(this.modifiers));
this.modifiers.clear();
for (ModifierKind modifier : modifiers) {
addModifier(modifier);
if (modifiers != null) {
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, MODIFIER, this.modifiers, modifiers);
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.modifiers = EnumSet.noneOf(ModifierKind.class);
} else {
this.modifiers = new HashSet<>(modifiers);
}
}
return (C) this;
}

@Override
public <T extends CtModifiable> T setSourceModifiers(Set<ModifierKind> sourceModifiers) {
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, MODIFIER, this.sourceModifiers, sourceModifiers);
if (sourceModifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.sourceModifiers = EnumSet.noneOf(ModifierKind.class);
} else {
this.sourceModifiers = new HashSet<>(sourceModifiers);
}
this.sourceModifiers = sourceModifiers;
return (T) this;
}

Expand All @@ -188,8 +191,12 @@ public <C extends CtModifiable> C addModifier(ModifierKind modifier) {
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.modifiers = EnumSet.noneOf(ModifierKind.class);
}
if (sourceModifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.sourceModifiers = EnumSet.noneOf(ModifierKind.class);
}
getFactory().getEnvironment().getModelChangeListener().onSetAdd(this, MODIFIER, this.modifiers, modifier);
modifiers.add(modifier);
sourceModifiers.add(modifier);
return (C) this;
}

Expand All @@ -199,6 +206,7 @@ public boolean removeModifier(ModifierKind modifier) {
return false;
}
getFactory().getEnvironment().getModelChangeListener().onSetDelete(this, MODIFIER, modifiers, modifier);
sourceModifiers.remove(modifier);
return modifiers.remove(modifier);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,25 @@ public boolean hasModifier(ModifierKind modifier) {

@Override
public <C extends CtModifiable> C setModifiers(Set<ModifierKind> modifiers) {
if (modifiers.size() > 0) {
getFactory().getEnvironment().getModelChangeListener().onSetDeleteAll(this, MODIFIER, this.modifiers, new HashSet<>(this.modifiers));
this.modifiers.clear();
for (ModifierKind modifier : modifiers) {
addModifier(modifier);
if (modifiers != null) {
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, MODIFIER, this.modifiers, modifiers);
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.modifiers = EnumSet.noneOf(ModifierKind.class);
} else {
this.modifiers = new HashSet<>(modifiers);
}
}
return (C) this;
}

@Override
public <T extends CtModifiable> T setSourceModifiers(Set<ModifierKind> sourceModifiers) {
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, MODIFIER, this.sourceModifiers, sourceModifiers);
if (sourceModifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.sourceModifiers = EnumSet.noneOf(ModifierKind.class);
} else {
this.sourceModifiers = new HashSet<>(sourceModifiers);
}
this.sourceModifiers = sourceModifiers;
return (T) this;
}

Expand All @@ -154,8 +157,12 @@ public <C extends CtModifiable> C addModifier(ModifierKind modifier) {
if (modifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.modifiers = EnumSet.noneOf(ModifierKind.class);
}
if (sourceModifiers == CtElementImpl.<ModifierKind>emptySet()) {
this.sourceModifiers = EnumSet.noneOf(ModifierKind.class);
}
getFactory().getEnvironment().getModelChangeListener().onSetAdd(this, MODIFIER, this.modifiers, modifier);
modifiers.add(modifier);
sourceModifiers.add(modifier);
return (C) this;
}

Expand All @@ -165,6 +172,7 @@ public boolean removeModifier(ModifierKind modifier) {
return false;
}
getFactory().getEnvironment().getModelChangeListener().onSetDelete(this, MODIFIER, modifiers, modifier);
sourceModifiers.remove(modifier);
return modifiers.remove(modifier);
}

Expand Down
Loading

0 comments on commit 09b6fce

Please sign in to comment.