Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -1,7 +1,6 @@
package org.alfasoftware.astra.core.refactoring.operations.annotations;

import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -32,6 +31,8 @@
* <pre>
* com.google.inject.Inject -&gt; javax.inject.Inject.
* <pre>
* By default the import for an annotation will be swapped over.
* If this is not required use {@link Builder#forceQualifiedName()}.
*/
public class AnnotationChangeRefactor implements ASTOperation {

Expand All @@ -47,8 +48,11 @@ public class AnnotationChangeRefactor implements ASTOperation {
private final Map<String, String> memberNameUpdates;
private final Map<String, String> memberNamesToUpdateWithNewValues;
private final Optional<Transform> transform;
private final boolean forceQualifiedName;

public AnnotationChangeRefactor(AnnotationMatcher fromType, String toType, Map<String, String> membersAndValuesToAdd, Map<String,String> memberAndTypesToAdd, Set<String> namesForMembersToRemove, Map<String, String> memberNameUpdates, Map<String, String> memberNamesToUpdateWithNewValues, Optional<Transform> transform) {
public AnnotationChangeRefactor(AnnotationMatcher fromType, String toType, Map<String, String> membersAndValuesToAdd,
Map<String,String> memberAndTypesToAdd, Set<String> namesForMembersToRemove, Map<String, String> memberNameUpdates,
Map<String, String> memberNamesToUpdateWithNewValues, Optional<Transform> transform, boolean forceQualifiedName) {
this.fromType = fromType;
this.toType = toType;
this.membersAndValuesToAdd = membersAndValuesToAdd;
Expand All @@ -57,6 +61,7 @@ public AnnotationChangeRefactor(AnnotationMatcher fromType, String toType, Map<S
this.memberNameUpdates = memberNameUpdates;
this.memberNamesToUpdateWithNewValues = memberNamesToUpdateWithNewValues;
this.transform = transform;
this.forceQualifiedName = forceQualifiedName;
}


Expand Down Expand Up @@ -86,6 +91,7 @@ public static class Builder {
private Map<String, String> memberNameUpdates = Map.of();
private Optional<Transform> transform = Optional.empty();
private Map<String, String> memberNamesToUpdateWithNewValues = Map.of();
private boolean forceQualifiedName;

private Builder() {
super();
Expand Down Expand Up @@ -132,6 +138,17 @@ public Builder withTransform(Transform transform) {
return this;
}

/**
* Forces use of the qualified name for the annotation.
* This is helpful if not all instances of an annotation are going to be replaced.
*
* @return this for method chaining
*/
public Builder forceQualifiedName() {
this.forceQualifiedName = true;
return this;
}

public AnnotationChangeRefactor build() {
return new AnnotationChangeRefactor(fromType,
toType,
Expand All @@ -140,8 +157,10 @@ public AnnotationChangeRefactor build() {
namesForMembersToRemove,
memberNameUpdates,
memberNamesToUpdateWithNewValues,
transform);
transform,
forceQualifiedName);
}

}


Expand All @@ -162,7 +181,7 @@ public void run(CompilationUnit compilationUnit, ASTNode node, ASTRewrite rewrit
+ "to [" + toType + "] "
+ "in [" + AstraUtils.getNameForCompilationUnit(compilationUnit) + "]");

if (! AstraUtils.getSimpleName(toType).equals(AstraUtils.getSimpleName(annotation.getTypeName().getFullyQualifiedName()))) {
if (!forceQualifiedName && !annotation.getTypeName().isQualifiedName()) {
AstraUtils.updateImport(compilationUnit, fromType.getFullyQualifiedName(), toType, rewriter);
}

Expand Down Expand Up @@ -193,7 +212,7 @@ private void rewriteAnnotation(CompilationUnit compilationUnit, Annotation annot

private Annotation changeAnnotationName(ASTRewrite rewriter, Annotation annotation) {
Name name;
if (annotation.getTypeName().isQualifiedName()) {
if (forceQualifiedName || annotation.getTypeName().isQualifiedName()) {
name = annotation.getAST().newName(toType);
} else {
name = annotation.getAST().newSimpleName(AstraUtils.getSimpleName(toType));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.alfasoftware.astra.core.refactoring.annotations;

import org.alfasoftware.astra.exampleTypes.AnnotationA;

@AnnotationA("")
public class AnnotationChangeSameSimpleNameExample {

@AnnotationA(value="A")
protected long someField;

@AnnotationA("BAR")
public char getBar(){
return 'a';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.alfasoftware.astra.core.refactoring.annotations;

import org.alfasoftware.astra.moreexampletypes.AnnotationA;

@AnnotationA("")
public class AnnotationChangeSameSimpleNameExampleAfter {

@AnnotationA(value="A")
protected long someField;

@AnnotationA("BAR")
public char getBar(){
return 'a';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.alfasoftware.astra.core.refactoring.annotations;

import org.alfasoftware.astra.exampleTypes.AnnotationA;

@AnnotationA("")
public class AnnotationChangeSameSimpleNameWithRemainderExample {

/**
* This annotation should not change
*/
@AnnotationA(value="A")
protected long someField;

@AnnotationA("BAR")
public char getBar(){
return 'a';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.alfasoftware.astra.core.refactoring.annotations;

import org.alfasoftware.astra.exampleTypes.AnnotationA;

@org.alfasoftware.astra.moreexampletypes.AnnotationA("")
public class AnnotationChangeSameSimpleNameWithRemainderExampleAfter {

/**
* This annotation should not change
*/
@AnnotationA(value="A")
protected long someField;

@org.alfasoftware.astra.moreexampletypes.AnnotationA("BAR")
public char getBar(){
return 'a';
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.alfasoftware.astra.core.refactoring.annotations;

import static org.alfasoftware.astra.core.utils.AstraUtils.addImport;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -22,8 +24,6 @@
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.junit.Test;

import static org.alfasoftware.astra.core.utils.AstraUtils.addImport;

public class TestAnnotationsRefactor extends AbstractRefactorTest {

@Test
Expand Down Expand Up @@ -91,6 +91,60 @@ public void testAnnotationChange() {
)));
}

@Test
public void testSameSimpleNameAnnotationChange() {
assertRefactor(
AnnotationChangeSameSimpleNameExample.class,
new HashSet<>(Arrays.asList(
AnnotationChangeRefactor.builder()
.from(AnnotationMatcher.builder()
.withFullyQualifiedName(AnnotationA.class.getName())
.withValue("")
.build())
.to(org.alfasoftware.astra.moreexampletypes.AnnotationA.class.getTypeName()).build(),
AnnotationChangeRefactor.builder()
.from(AnnotationMatcher.builder()
.withFullyQualifiedName(AnnotationA.class.getName())
.withValue("A")
.build())
.to(org.alfasoftware.astra.moreexampletypes.AnnotationA.class.getTypeName()).build(),
AnnotationChangeRefactor.builder()
.from(AnnotationMatcher.builder()
.withFullyQualifiedName(AnnotationA.class.getName())
.withValue("BAR")
.build())
.to(org.alfasoftware.astra.moreexampletypes.AnnotationA.class.getTypeName()).build()
)));
}

/**
* Example where we are not swapping over all instances of an annotation with the same simple name
* therefore we force the new annotation to be fully qualified.
*/
@Test
public void testSameSimpleNameAnnotationChangeWithRemainder() {
assertRefactor(
AnnotationChangeSameSimpleNameWithRemainderExample.class,
new HashSet<>(Arrays.asList(
AnnotationChangeRefactor.builder()
.from(AnnotationMatcher.builder()
.withFullyQualifiedName(AnnotationA.class.getName())
.withValue("")
.build())
.to(org.alfasoftware.astra.moreexampletypes.AnnotationA.class.getTypeName())
.forceQualifiedName()
.build(),
AnnotationChangeRefactor.builder()
.from(AnnotationMatcher.builder()
.withFullyQualifiedName(AnnotationA.class.getName())
.withValue("BAR")
.build())
.to(org.alfasoftware.astra.moreexampletypes.AnnotationA.class.getTypeName())
.forceQualifiedName()
.build()
)));
}

@Test
public void testRemoveAnnotation() {
assertRefactor(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.alfasoftware.astra.moreexampletypes;

public @interface AnnotationA {

String value() default "";

String othervalue() default "";
}