diff --git a/bndtools.core/src/org/bndtools/refactor/types/ComponentRefactorer.java b/bndtools.core/src/org/bndtools/refactor/types/ComponentRefactorer.java index 05b437e2d8..8684b16fcb 100644 --- a/bndtools.core/src/org/bndtools/refactor/types/ComponentRefactorer.java +++ b/bndtools.core/src/org/bndtools/refactor/types/ComponentRefactorer.java @@ -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 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 NOT_REFERENCE = Set.of(BUNDLECONTEXT_T, SERVICEREFERENCE_T, MAP_T); + public static final int BASE_LEVEL = 2000; class MethodState extends DomainBase { final String annotation; @@ -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() @@ -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))); } }); } diff --git a/bndtools.core/src/org/bndtools/refactor/types/GogoRefactorer.java b/bndtools.core/src/org/bndtools/refactor/types/GogoRefactorer.java index d4c77b1be7..5da4d66283 100644 --- a/bndtools.core/src/org/bndtools/refactor/types/GogoRefactorer.java +++ b/bndtools.core/src/org/bndtools/refactor/types/GogoRefactorer.java @@ -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) diff --git a/bndtools.core/src/org/bndtools/refactor/util/RefactorAssistant.java b/bndtools.core/src/org/bndtools/refactor/util/RefactorAssistant.java index d8d7b43da8..99202baa3a 100644 --- a/bndtools.core/src/org/bndtools/refactor/util/RefactorAssistant.java +++ b/bndtools.core/src/org/bndtools/refactor/util/RefactorAssistant.java @@ -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(); } /** @@ -1039,6 +1040,7 @@ public RefactorAssistant(ICompilationUnit iunit) throws JavaModelException { }); this.iunit = iunit; this.source = iunit.getSource(); + init(); } /** diff --git a/bndtools.core/test/org/bndtools/refactor/types/ComponentRefactorerTest.java b/bndtools.core/test/org/bndtools/refactor/types/ComponentRefactorerTest.java index 0e3af24b37..e575215f2c 100644 --- a/bndtools.core/test/org/bndtools/refactor/types/ComponentRefactorerTest.java +++ b/bndtools.core/test/org/bndtools/refactor/types/ComponentRefactorerTest.java @@ -191,8 +191,8 @@ 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+"), @@ -200,7 +200,7 @@ void unsetService( Bar service){ 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 ); diff --git a/bndtools.core/test/org/bndtools/refactor/types/DiverseRefactorerTest.java b/bndtools.core/test/org/bndtools/refactor/types/DiverseRefactorerTest.java index bb4570c69a..5cc2dd758a 100644 --- a/bndtools.core/test/org/bndtools/refactor/types/DiverseRefactorerTest.java +++ b/bndtools.core/test/org/bndtools/refactor/types/DiverseRefactorerTest.java @@ -78,7 +78,7 @@ class Foo { public static String this_before = """ class Foo { Foo( int x){ - super(x); + this(x); } } """; @@ -86,7 +86,7 @@ class Foo { class Foo { final int x; Foo( int x){ - super(x); + this(x); this.x=x; } } diff --git a/bndtools.core/test/org/bndtools/refactor/types/GogoRefactorerTest.java b/bndtools.core/test/org/bndtools/refactor/types/GogoRefactorerTest.java index e4f28e2c44..4cc1138e1e 100644 --- a/bndtools.core/test/org/bndtools/refactor/types/GogoRefactorerTest.java +++ b/bndtools.core/test/org/bndtools/refactor/types/GogoRefactorerTest.java @@ -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 ""; } @@ -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 ""; } @@ -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 ""; } @@ -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 ""; } @@ -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 ""; } @@ -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 ""; } @@ -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 ""; } @@ -144,9 +151,9 @@ static List 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+"),