Skip to content

Commit

Permalink
Merge pull request #5976 from pkriens/issue/refactoring
Browse files Browse the repository at this point in the history
Fixes #5975. Changes in refactoring UI
  • Loading branch information
pkriens authored Jan 18, 2024
2 parents de54cac + 3c9be0e commit 58d9db0
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@
@Component
public class ComponentRefactorer extends BaseRefactorer implements IQuickFixProcessor {

public final RE BIND_METHOD_P = g(g("prefix", or("add", "set")), g("service", setAll));
public final RE UNBIND_METHOD_P = g(g("prefix", or("remove", "unset")), g("service", setAll));
public final RE ACTIVATE_METHOD_P = g(or("activate", "start", "init", "initialize", "onActivate",
public static final RE BIND_METHOD_P = g(g("prefix", or("add", "set")), g("service", setAll));
public static final RE UNBIND_METHOD_P = g(g("prefix", or("remove", "unset")), g("service", setAll));
public static final RE ACTIVATE_METHOD_P = g(or("activate", "start", "init", "initialize", "onActivate",
"onStart", "begin", "doActivate", "create", "setup", "ready", "load"), setAll);
public final RE DEACTIVATE_METHOD_P = g(or("deactivate", "stop", "close", "finish", "dispose",
public static final RE DEACTIVATE_METHOD_P = g(or("deactivate", "stop", "close", "finish", "dispose",
"shutdown", "onDeactivate", "onStop", "end", "doDeactivate", "release", "teardown", "cleanup"), setAll);
public final String COMPONENT_A = "org.osgi.service.component.annotations.Component";
public final String REFERENCE_A = "org.osgi.service.component.annotations.Reference";
public final String ACTIVATE_A = "org.osgi.service.component.annotations.Activate";
public final String DEACTIVATE_A = "org.osgi.service.component.annotations.Deactivate";
public final String BUNDLECONTEXT_T = "org.osgi.framework.BundleContext";
public final String SERVICEREFERENCE_T = "org.osgi.framework.ServiceReference";
public final String MAP_T = "java.util.Map";
public final Set<String> NOT_REFERENCE = Set.of(BUNDLECONTEXT_T, SERVICEREFERENCE_T, MAP_T);
public final static int BASE_LEVEL = 2000;
public static final String COMPONENT_A = "org.osgi.service.component.annotations.Component";
public static final String REFERENCE_A = "org.osgi.service.component.annotations.Reference";
public static final String ACTIVATE_A = "org.osgi.service.component.annotations.Activate";
public static final String DEACTIVATE_A = "org.osgi.service.component.annotations.Deactivate";
public static final String BUNDLECONTEXT_T = "org.osgi.framework.BundleContext";
public static final String SERVICEREFERENCE_T = "org.osgi.framework.ServiceReference";
public static final String MAP_T = "java.util.Map";
public static final Set<String> NOT_REFERENCE = Set.of(BUNDLECONTEXT_T, SERVICEREFERENCE_T, MAP_T);
public static final int BASE_LEVEL = 2000;

class MethodState extends DomainBase<MethodDeclaration> {
final String annotation;
Expand Down Expand Up @@ -227,13 +227,15 @@ public void addCompletions(ProposalBuilder builder, RefactorAssistant assistant,
root.isJavaSourceType(JavaSourceType.CLASS)
.upTo(TypeDeclaration.class)
.forEach((ass, typeDeclaration) -> {

int relevance = root.getNode()
.map(selected -> 2 - ass.getDistance(selected, typeDeclaration, 1000))
.orElse(-1);

if (ass.hasAnnotation(typeDeclaration, COMPONENT_A)) {
ComponentState cs = new ComponentState(ass.cursor(typeDeclaration), builder);

builder.build("comp-", "Remove @Component", "component", 3, cs::remove);
if (relevance >= 1)
builder.build("comp-", "Remove @Component", "component", 3, cs::remove);

root.upTo(VariableDeclarationFragment.class)
.isNotPrimitive()
Expand Down Expand Up @@ -263,8 +265,9 @@ public void addCompletions(ProposalBuilder builder, RefactorAssistant assistant,
.forEach((x, md) -> cs.proposeDeactivate(md));

} else {
builder.build("comp+", "Add @Component", "component", relevance,
() -> ass.ensureAnnotation(typeDeclaration, ass.newAnnotation(COMPONENT_A)));
if (relevance >= 1)
builder.build("comp+", "Add @Component", "component", relevance,
() -> ass.ensureAnnotation(typeDeclaration, ass.newAnnotation(COMPONENT_A)));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,21 @@ public void add(Command command) {
public void addCompletions(ProposalBuilder builder, RefactorAssistant assistant, Cursor<?> root,
IInvocationContext context) {


root = root.isJavaSourceType(JavaSourceType.CLASS);

GogoState state = root.upTo(TypeDeclaration.class)
.anyOfTheseAnnotations(ComponentRefactorer.COMPONENT_A)
.processSingletons(GogoState::new)
.stream()
.findAny()
.orElse(null);

if (state != null) {

doScope(builder, state);
root.upTo(TypeDeclaration.class, 1)
.forEach((ass, n) -> {
doScope(builder, state);
});
if (state.isPresent()) {
root.upTo(SingleVariableDeclaration.class, 4)
.parentType(MethodDeclaration.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,7 @@ public RefactorAssistant(CompilationUnit unit, @Nullable
engine = Memoize.supplier(() -> new ASTEngine(unit));
source = iunit == null ? null : iunit.getSource();
this.iunit = iunit;
init();
}

/**
Expand All @@ -1039,6 +1040,7 @@ public RefactorAssistant(ICompilationUnit iunit) throws JavaModelException {
});
this.iunit = iunit;
this.source = iunit.getSource();
init();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,16 @@ void unsetService( Bar service){
//@formatter:off

new Scenario(CAc, Cpublic, "F\\(" , "comp.act-"),
new Scenario(empty, C, "ref" , "comp+"),
new Scenario(C, empty, "ref" , "comp-"),
new Scenario(empty, C, "class F" , "comp+"),
new Scenario(C, empty, "class F" , "comp-"),
new Scenario(C, CAc, "String service" , "comp.act+"),
new Scenario(CAc, CAm, "activate" , "comp.act+"),
new Scenario(CAc, CAcRp, "String service" , "comp.ref+"),
new Scenario(CAcRp, CAc, "String service" , "comp.ref-"),
new Scenario(CAm, CAmDm, "deactivate" , "comp.deact+"),
new Scenario(CAmDm, CAm, "deactivate" , "comp.deact-"),
new Scenario(CAmDm, CAmDmRm, "setService" , "comp.ref+"),
new Scenario(CAmDmRm, emptyPublic, "setService" , "comp-")
new Scenario(CAmDmRm, emptyPublic, "F \\{" , "comp-")

//@formatter:on
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ class Foo {
public static String this_before = """
class Foo {
Foo( int x){
super(x);
this(x);
}
}
""";
public static String this_after = """
class Foo {
final int x;
Foo( int x){
super(x);
this(x);
this.x=x;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class GogoRefactorerTest {
* secondary. Lower case means NO.
*/
final static String gd_dpf_d = """
public class Foo {
import org.osgi.service.component.annotations.Component;
@Component public class Foo {
public String command( String s, boolean flag, int some){
return "";
}
Expand All @@ -32,8 +33,9 @@ public String secondary(){
}
""";
final static String Gd_dpf_d = """
import org.osgi.service.component.annotations.Component;
import org.apache.felix.service.command.annotations.GogoCommand;
@GogoCommand(scope="foo",function={}) public class Foo {
@GogoCommand(scope="foo",function={}) @Component public class Foo {
public String command( String s, boolean flag, int some){
return "";
}
Expand All @@ -43,9 +45,10 @@ public String secondary(){
}
""";
final static String GD_dpf_d = """
import org.osgi.service.component.annotations.Component;
import org.apache.felix.service.command.Descriptor;
import org.apache.felix.service.command.annotations.GogoCommand;
@GogoCommand(scope="foo",function={}) @Descriptor("foo - Foo") public class Foo {
@GogoCommand(scope="foo",function={}) @Descriptor("foo - Foo") @Component public class Foo {
public String command( String s, boolean flag, int some){
return "";
}
Expand All @@ -55,9 +58,10 @@ public String secondary(){
}
""";
final static String Gd_Dpf_d = """
import org.osgi.service.component.annotations.Component;
import org.apache.felix.service.command.annotations.GogoCommand;
import org.apache.felix.service.command.Descriptor;
@GogoCommand(scope="foo",function="command") public class Foo {
@GogoCommand(scope="foo",function="command") @Component public class Foo {
@Descriptor("command") public String command( String s, boolean flag, int some){
return "";
}
Expand All @@ -67,10 +71,11 @@ public String secondary(){
}
""";
final static String Gd_DpF_d = """
import org.osgi.service.component.annotations.Component;
import org.apache.felix.service.command.annotations.GogoCommand;
import org.apache.felix.service.command.Descriptor;
import org.apache.felix.service.command.Parameter;
@GogoCommand(scope="foo",function="command") public class Foo {
@GogoCommand(scope="foo",function="command") @Component public class Foo {
@Descriptor("command") public String command( String s, @Descriptor("flag") @Parameter(absentValue="false",presentValue="true",names={"-f","--flag"}) boolean flag, int some){
return "";
}
Expand All @@ -81,10 +86,11 @@ public String secondary(){
""";

final static String Gd_DPF_d = """
import org.osgi.service.component.annotations.Component;
import org.apache.felix.service.command.annotations.GogoCommand;
import org.apache.felix.service.command.Descriptor;
import org.apache.felix.service.command.Parameter;
@GogoCommand(scope="foo",function="command") public class Foo {
@GogoCommand(scope="foo",function="command") @Component public class Foo {
@Descriptor("command") public String command( String s, @Descriptor("flag") @Parameter(absentValue="false",presentValue="true",names={"-f","--flag"}) boolean flag, @Descriptor("some") @Parameter(absentValue="",names={"-s","--some"}) int some){
return "";
}
Expand All @@ -95,9 +101,10 @@ public String secondary(){
""";

final static String Gd_Dpf_D = """
import org.osgi.service.component.annotations.Component;
import org.apache.felix.service.command.annotations.GogoCommand;
import org.apache.felix.service.command.Descriptor;
@GogoCommand(scope="foo",function={"command","secondary"}) public class Foo {
@GogoCommand(scope="foo",function={"command","secondary"}) @Component public class Foo {
@Descriptor("command") public String command( String s, boolean flag, int some){
return "";
}
Expand Down Expand Up @@ -144,9 +151,9 @@ static List<Scenario> scenarios() {
new Scenario(Gd_DPF_d, Gd_DpF_d, "int ()some" , "gogo.parm-"),
new Scenario(Gd_Dpf_d, Gd_DpF_d, "boolean ()flag" , "gogo.parm+"),
new Scenario(Gd_DpF_d, Gd_Dpf_d, "boolean ()flag" , "gogo.parm-"),
new Scenario(gd_dpf_d, GD_dpf_d, "String co(mma)nd\\(" , "gogo.scope+"),
new Scenario(Gd_dpf_d, gd_dpf_d, "String co(mma)nd\\(" , "gogo.scope-"),
new Scenario(Gd_Dpf_D, gd_dpf_d, "String co(mma)nd\\(" , "gogo.scope-"),
new Scenario(gd_dpf_d, GD_dpf_d, "class Foo" , "gogo.scope+"),
new Scenario(Gd_dpf_d, gd_dpf_d, "class Foo" , "gogo.scope-"),
new Scenario(Gd_Dpf_D, gd_dpf_d, "class Foo" , "gogo.scope-"),
new Scenario(Gd_dpf_d, Gd_Dpf_d, "String co(mma)nd\\(" , "gogo.cmd.desc+"),
new Scenario(Gd_dpf_d, Gd_Dpf_d, "String com()mand\\(" , "gogo.cmd.desc+"),
new Scenario(Gd_dpf_d, Gd_Dpf_d, "String command\\(" , "gogo.cmd.desc+"),
Expand Down

0 comments on commit 58d9db0

Please sign in to comment.