file/src/org/netbeans/modules/jackpot30/file/conditionapi/DefaultRuleUtilities.java
1.1 --- a/file/src/org/netbeans/modules/jackpot30/file/conditionapi/DefaultRuleUtilities.java Sun Jan 16 13:56:54 2011 +0100
1.2 +++ b/file/src/org/netbeans/modules/jackpot30/file/conditionapi/DefaultRuleUtilities.java Thu Jul 21 22:51:52 2011 +0200
1.3 @@ -42,6 +42,7 @@
1.4 import java.util.Arrays;
1.5 import java.util.EnumSet;
1.6 import java.util.Set;
1.7 +import java.util.regex.Pattern;
1.8 import javax.lang.model.SourceVersion;
1.9 import javax.lang.model.element.ElementKind;
1.10 import javax.lang.model.element.Modifier;
1.11 @@ -114,4 +115,51 @@
1.12 public boolean containsAny(Variable var, String... patterns) {
1.13 return matcher.containsAny(var, patterns);
1.14 }
1.15 +
1.16 + /**Tests whether the current occurrences is enclosed (directly or indirectly)
1.17 + * by any of the specified classes.
1.18 + *
1.19 + * @param className canonical class names of possibly enclosing classes
1.20 + * @return true if and only if the current occurrence is directly or indirectly
1.21 + * enclosed by any of the given classes.
1.22 + */
1.23 + public boolean inClass(String... className) {
1.24 + Pattern p = constructRegexp(className);
1.25 + Variable current = context.variableForName("$_");
1.26 +
1.27 + assert current != null;
1.28 +
1.29 + for (String canonicalName : context.enclosingClasses(current)) {
1.30 + if (p.matcher(canonicalName).matches()) return true;
1.31 + }
1.32 +
1.33 + return false;
1.34 + }
1.35 +
1.36 + /**Tests whether the current occurrences is in any of the given packages.
1.37 + *
1.38 + * @param packageName names of possibly enclosing packages
1.39 + * @return true if and only if the current occurrence is inside any of the
1.40 + * given packages
1.41 + */
1.42 + public boolean inPackage(String... packageName) {
1.43 + Pattern p = constructRegexp(packageName);
1.44 +
1.45 + return p.matcher(context.enclosingPackage()).matches();
1.46 + }
1.47 +
1.48 + private static Pattern constructRegexp(String[] pattern) {
1.49 + StringBuilder regexp = new StringBuilder();
1.50 + boolean first = true;
1.51 + for (String p : pattern) {
1.52 + if (first) regexp.append("|");
1.53 +
1.54 + regexp.append("(");
1.55 + regexp.append(Pattern.quote(p));
1.56 + regexp.append(")");
1.57 + first = false;
1.58 + }
1.59 + Pattern p = Pattern.compile(regexp.toString());
1.60 + return p;
1.61 + }
1.62 }