Commits

Jan Lahoda committed 6e78ca2

Add HintDescriptionFactory used to create the HintDescription, also adding displayName to the HD.

Comments (0)

Files changed (6)

api/src/org/netbeans/modules/jackpot30/spi/HintDescription.java

  */
 public final class HintDescription {
 
+    private final String displayName;
     private final Kind triggerKind;
     private final PatternDescription triggerPattern;
     private final Worker worker;
 
-    private HintDescription(Kind triggerKind, PatternDescription triggerPattern, Worker worker) {
+    private HintDescription(String displayName, Kind triggerKind, PatternDescription triggerPattern, Worker worker) {
+        this.displayName = displayName;
         this.triggerKind = triggerKind;
         this.triggerPattern = triggerPattern;
         this.worker = worker;
         return worker;
     }
 
-    public static HintDescription create(PatternDescription triggerPattern, Worker worker) {
-        return new HintDescription(null, triggerPattern, worker);
+    //XXX: should not be public
+    public String getDisplayName() {
+        return displayName;
     }
 
-    public static HintDescription create(Kind triggerKind, Worker worker) {
-        return new HintDescription(triggerKind, null, worker);
+    static HintDescription create(String displayName, PatternDescription triggerPattern, Worker worker) {
+        return new HintDescription(displayName, null, triggerPattern, worker);
+    }
+
+    static HintDescription create(String displayName, Kind triggerKind, Worker worker) {
+        return new HintDescription(displayName, triggerKind, null, worker);
     }
     
     public static final class PatternDescription {

api/src/org/netbeans/modules/jackpot30/spi/HintDescriptionFactory.java

+package org.netbeans.modules.jackpot30.spi;
+
+import com.sun.source.tree.Tree.Kind;
+import org.netbeans.modules.jackpot30.spi.HintDescription.PatternDescription;
+import org.netbeans.modules.jackpot30.spi.HintDescription.Worker;
+
+/**
+ *
+ * @author lahvac
+ */
+public class HintDescriptionFactory {
+
+    private       String displayName;
+    private       Kind triggerKind;
+    private       PatternDescription triggerPattern;
+    private       Worker worker;
+    private       boolean finished;
+
+    private HintDescriptionFactory() {
+    }
+
+    public static HintDescriptionFactory create() {
+        return new HintDescriptionFactory();
+    }
+
+    public HintDescriptionFactory setDisplayName(String displayName) {
+        this.displayName = displayName;
+        return this;
+    }
+
+    public HintDescriptionFactory setTriggerKind(Kind triggerKind) {
+        if (this.triggerPattern != null) {
+            throw new IllegalStateException(this.triggerPattern.getPattern());
+        }
+
+        this.triggerKind = triggerKind;
+        return this;
+    }
+
+    public HintDescriptionFactory setTriggerPattern(PatternDescription triggerPattern) {
+        if (this.triggerKind != null) {
+            throw new IllegalStateException(this.triggerKind.name());
+        }
+        
+        this.triggerPattern = triggerPattern;
+        return this;
+    }
+
+    public HintDescriptionFactory setWorker(Worker worker) {
+        this.worker = worker;
+        return this;
+    }
+
+    public HintDescription produce() {
+        if (this.triggerKind == null) {
+            return HintDescription.create(displayName, triggerPattern, worker);
+        } else {
+            return HintDescription.create(displayName, triggerKind, worker);
+        }
+    }
+    
+}

api/test/unit/src/org/netbeans/modules/jackpot30/impl/hints/HintsInvokerTest.java

 import org.netbeans.modules.jackpot30.spi.HintDescription;
 import org.netbeans.modules.jackpot30.spi.HintDescription.PatternDescription;
 import org.netbeans.modules.jackpot30.spi.HintDescription.Worker;
+import org.netbeans.modules.jackpot30.spi.HintDescriptionFactory;
 import org.netbeans.modules.jackpot30.spi.JavaFix;
 import org.netbeans.modules.jackpot30.spi.support.ErrorDescriptionFactory;
 import org.netbeans.modules.java.hints.infrastructure.TreeRuleTestBase;
 
     static {
         test2Hint = new HashMap<String, HintDescription>();
-        test2Hint.put("testPattern1", HintDescription.create(HintDescription.PatternDescription.create("$1.toURL()", Collections.singletonMap("$1", "java.io.File")), new WorkerImpl()));
+        test2Hint.put("testPattern1", HintDescriptionFactory.create().setTriggerPattern(PatternDescription.create("$1.toURL()", Collections.singletonMap("$1", "java.io.File"))).setWorker(new WorkerImpl()).produce());
         test2Hint.put("testPattern2", test2Hint.get("testPattern1"));
-        test2Hint.put("testKind1", HintDescription.create(Kind.METHOD_INVOCATION, new WorkerImpl()));
-        test2Hint.put("testPatternVariable1", HintDescription.create(HintDescription.PatternDescription.create("{ $1 $2; $2 = $3; }", Collections.<String, String>emptyMap()), new WorkerImpl("{ $1 $2 = $3; }")));
+        test2Hint.put("testKind1", HintDescriptionFactory.create().setTriggerKind(Kind.METHOD_INVOCATION).setWorker(new WorkerImpl()).produce());
+        test2Hint.put("testPatternVariable1", HintDescriptionFactory.create().setTriggerPattern(PatternDescription.create("{ $1 $2; $2 = $3; }", Collections.<String, String>emptyMap())).setWorker(new WorkerImpl("{ $1 $2 = $3; }")).produce());
         Map<String, String> constraints = new HashMap<String, String>();
 
         constraints.put("$1", "boolean");
         constraints.put("$2", "java.lang.Object");
 
-        test2Hint.put("testPatternAssert1", HintDescription.create(HintDescription.PatternDescription.create("assert $1 : $2;", constraints), new WorkerImpl()));
-        test2Hint.put("testPatternStatementAndSingleStatementBlockAreSame", HintDescription.create(HintDescription.PatternDescription.create("if ($1) return $2;", Collections.<String, String>emptyMap()), new WorkerImpl()));
-        test2Hint.put("testPatternFalseOccurrence", HintDescription.create(HintDescription.PatternDescription.create("$1.toURL()", Collections.singletonMap("$1", "java.io.File")), new WorkerImpl()));
+        test2Hint.put("testPatternAssert1", HintDescriptionFactory.create().setTriggerPattern(PatternDescription.create("assert $1 : $2;", constraints)).setWorker(new WorkerImpl()).produce());
+        test2Hint.put("testPatternStatementAndSingleStatementBlockAreSame", HintDescriptionFactory.create().setTriggerPattern(PatternDescription.create("if ($1) return $2;", Collections.<String, String>emptyMap())).setWorker(new WorkerImpl()).produce());
+        test2Hint.put("testPatternFalseOccurrence", HintDescriptionFactory.create().setTriggerPattern(PatternDescription.create("$1.toURL()", Collections.singletonMap("$1", "java.io.File"))).setWorker(new WorkerImpl()).produce());
     }
 
     @Override

code/src/org/netbeans/modules/jackpot30/code/CodeHintProviderImpl.java

 import org.netbeans.modules.jackpot30.spi.HintDescription;
 import org.netbeans.modules.jackpot30.spi.HintDescription.PatternDescription;
 import org.netbeans.modules.jackpot30.spi.HintDescription.Worker;
+import org.netbeans.modules.jackpot30.spi.HintDescriptionFactory;
 import org.netbeans.modules.jackpot30.spi.HintProvider;
 import org.netbeans.spi.editor.hints.ErrorDescription;
 import org.openide.util.Exceptions;
                     Class clazz = l.loadClass(c);
 
                     for (Method m : clazz.getDeclaredMethods()) {
-                        if (m.getAnnotation(Hint.class) != null) {
-                            processMethod(result, m);
+                        Hint hint = m.getAnnotation(Hint.class);
+
+                        if (hint != null) {
+                            processMethod(result, hint, m);
                         }
                     }
                 } catch (ClassNotFoundException ex) {
         return l;
     }
 
-    static void processMethod(List<HintDescription> hints, Method m) {
+    static void processMethod(List<HintDescription> hints, Hint hint, Method m) {
         //XXX: combinations of TriggerTreeKind and TriggerPattern?
-        processTreeKindHint(hints, m);
-        processPatternHint(hints, m);
+        processTreeKindHint(hints, hint, m);
+        processPatternHint(hints, hint, m);
     }
     
-    private static void processTreeKindHint(List<HintDescription> hints, Method m) {
+    private static void processTreeKindHint(List<HintDescription> hints, Hint hint, Method m) {
         TriggerTreeKind kindTrigger = m.getAnnotation(TriggerTreeKind.class);
 
         if (kindTrigger == null) {
         Worker w = new WorkerImpl(m);
 
         for (Kind k : new HashSet<Kind>(Arrays.asList(kindTrigger.value()))) {
-            hints.add(HintDescription.create(k, w));
+            hints.add(HintDescriptionFactory.create()
+                                            .setTriggerKind(k)
+                                            .setWorker(w)
+                                            .produce());
         }
     }
     
-    private static void processPatternHint(List<HintDescription> hints, Method m) {
+    private static void processPatternHint(List<HintDescription> hints, Hint hint, Method m) {
         TriggerPattern patternTrigger = m.getAnnotation(TriggerPattern.class);
 
         if (patternTrigger == null) {
 
         PatternDescription pd = PatternDescription.create(pattern, constraints);
 
-        hints.add(HintDescription.create(pd, new WorkerImpl(m)));
+        hints.add(HintDescriptionFactory.create()
+                                        .setTriggerPattern(pd)
+                                        .setWorker(new WorkerImpl(m))
+                                        .produce());
     }
 
     //accessed by tests:

file/src/org/netbeans/modules/jackpot30/file/DeclarativeHintRegistry.java

 import org.netbeans.modules.jackpot30.spi.ClassPathBasedHintProvider;
 import org.netbeans.modules.jackpot30.spi.HintDescription;
 import org.netbeans.modules.jackpot30.spi.HintDescription.PatternDescription;
+import org.netbeans.modules.jackpot30.spi.HintDescriptionFactory;
 import org.netbeans.modules.jackpot30.spi.HintProvider;
 import org.openide.filesystems.FileObject;
 import org.openide.filesystems.FileUtil;
         Map<String, String> constraints = new HashMap<String, String>();
         String pattern = parseOutTypesFromPattern(s[1], constraints);
 
-        return HintDescription.create(PatternDescription.create(pattern, constraints), new DeclarativeHintsWorker(s[0], fixes));
+        return HintDescriptionFactory.create()
+                                     .setDisplayName(s[0])
+                                     .setTriggerPattern(PatternDescription.create(pattern, constraints))
+                                     .setWorker(new DeclarativeHintsWorker(s[0], fixes))
+                                     .produce();
     }
 
     static String[] splitNameAndPattern(String spec) {

transformer/src/org/netbeans/modules/jackpot30/transformers/TransformationHintProviderImpl.java

 import org.netbeans.modules.jackpot30.spi.ElementBasedHintProvider;
 import org.netbeans.modules.jackpot30.spi.HintContext;
 import org.netbeans.modules.jackpot30.spi.HintDescription;
+import org.netbeans.modules.jackpot30.spi.HintDescription.PatternDescription;
 import org.netbeans.modules.jackpot30.spi.HintDescription.Worker;
+import org.netbeans.modules.jackpot30.spi.HintDescriptionFactory;
 import org.netbeans.modules.jackpot30.spi.JavaFix;
 import org.netbeans.modules.jackpot30.spi.support.ErrorDescriptionFactory;
 import org.netbeans.spi.editor.hints.ErrorDescription;
             return null;//XXX
         }
 
-        return HintDescription.create(HintDescription.PatternDescription.create(patternString, variableTypes), new WorkerImpl(displayName, fixes));
+        return HintDescriptionFactory.create()
+                                     .setDisplayName(displayName)
+                                     .setTriggerPattern(PatternDescription.create(patternString, variableTypes))
+                                     .setWorker(new WorkerImpl(displayName, fixes))
+                                     .produce();
     }