package annotator.find;

import annotator.Main;

import com.sun.source.tree.*;
import com.sun.source.util.TreePath;

/**
 * Represents the criterion that a program element is in a package with a
 * certain name.
 */
final class InPackageCriterion implements Criterion {

  private final String name;

  InPackageCriterion(String name) {
    this.name = name;
  }

  /**
   * {@inheritDoc}
   */
  public Kind getKind() {
    return Kind.IN_PACKAGE;
  }

  /** {@inheritDoc} */
  @Override
  public boolean isSatisfiedBy(TreePath path, Tree leaf) {
    assert path == null || path.getLeaf() == leaf;
    return isSatisfiedBy(path);
  }

  /** {@inheritDoc} */
  @Override
  public boolean isSatisfiedBy(TreePath path) {

    if (path == null)
      return false;

    if (Criteria.debug) {
      debug("InPackageCriterion.isSatisfiedBy(" + Main.pathToString(path) + "); this=" + this);
    }

    do {
      Tree tree = path.getLeaf();
      if (tree.getKind() == Tree.Kind.COMPILATION_UNIT) {
        CompilationUnitTree cu = (CompilationUnitTree)tree;
        String packageName = cu.getPackageName().toString();
        if (name.equals(packageName))
          return true;
      }
      path = path.getParentPath();
    } while (path != null && path.getLeaf() != null);
    debug("InPackageCriterion.isSatisfiedBy => false");
    return false;
  }

  /**
   * {@inheritDoc}
   */
  public String toString() {
    return "in package '" + name + "'";
  }

  private static void debug(String s) {
    if (Criteria.debug) {
      System.out.println(s);
    }
  }

}

@@ -1058,36 +1058,160 @@
     
-String packageName = cu.getP
+ExpressionTree pn = cu.getPackageName();%0A        if (pn == null) %7B%0A          return name == null %7C%7C name.equals(%22%22);%0A        %7D else %7B%0A          String p
 acka
@@ -1216,18 +1216,21 @@
 kageName
-()
+ = pn
 .toStrin
@@ -1242,18 +1242,40 @@
         
-if
+  return name != null &&
  (name.e
@@ -1293,16 +1293,17 @@
 geName))
+;
 %0A       
@@ -1307,22 +1307,9 @@
     
-  return true;
+%7D
 %0A   

Fix bug related to default (unnamed) package.