file/src/org/netbeans/modules/jackpot30/file/conditionapi/DefaultRuleUtilities.java
bitbucket-20: adding conditions for enclosing class/package (transplanting from trunk).
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
24 * If you wish your version of this file to be governed by only the CDDL
25 * or only the GPL Version 2, indicate your decision by adding
26 * "[Contributor] elects to include this software in this distribution
27 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28 * single choice of license, a recipient has the option to distribute
29 * your version of this file under either the CDDL, the GPL Version 2 or
30 * to extend the choice of license to its licensees as provided above.
31 * However, if you add GPL Version 2 code and therefore, elected the GPL
32 * Version 2 license, then the option applies only if the new code is
33 * made subject to such option by the copyright holder.
37 * Portions Copyrighted 2009 Sun Microsystems, Inc.
40 package org.netbeans.modules.jackpot30.file.conditionapi;
42 import java.util.Arrays;
43 import java.util.EnumSet;
45 import java.util.regex.Pattern;
46 import javax.lang.model.SourceVersion;
47 import javax.lang.model.element.ElementKind;
48 import javax.lang.model.element.Modifier;
49 import org.netbeans.api.annotations.common.NonNull;
55 public final class DefaultRuleUtilities {
57 private final Context context;
58 private final Matcher matcher;
60 DefaultRuleUtilities(Context context, Matcher matcher) {
61 this.context = context;
62 this.matcher = matcher;
65 public boolean referencedIn(Variable variable, Variable in) {
66 return matcher.referencedIn(variable, in);
69 public boolean sourceVersionGE(SourceVersion source) {
70 return context.sourceVersion().compareTo(source) >= 0;
73 public boolean hasModifier(Variable variable, Modifier modifier) {
74 return context.modifiers(variable).contains(modifier);
77 public boolean parentMatches(String pattern) {
78 Variable parent = context.parent(context.variableForName("$_"));
84 return matcher.matchesAny(parent, pattern); //XXX: $_ currently not part of variables map, so this won't work!!!
87 public boolean elementKindMatches(Variable variable, ElementKind... kind) {
88 Set<ElementKind> kinds = EnumSet.noneOf(ElementKind.class);
90 kinds.addAll(Arrays.asList(kind));
92 return kinds.contains(context.elementKind(variable));
95 public boolean isNullLiteral(@NonNull Variable var) {
96 return context.isNullLiteral(var);
99 public boolean matches(String pattern) {
100 Variable current = context.variableForName("$_");
102 assert current != null;
104 return matchesAny(current, pattern); //XXX: $_ currently not part of variables map, so this won't work!!!
107 public boolean matchesWithBind(Variable var, String pattern) {
108 return matcher.matchesWithBind(var, pattern);
111 public boolean matchesAny(Variable var, String... patterns) {
112 return matcher.matchesAny(var, patterns);
115 public boolean containsAny(Variable var, String... patterns) {
116 return matcher.containsAny(var, patterns);
119 /**Tests whether the current occurrences is enclosed (directly or indirectly)
120 * by any of the specified classes.
122 * @param className canonical class names of possibly enclosing classes
123 * @return true if and only if the current occurrence is directly or indirectly
124 * enclosed by any of the given classes.
126 public boolean inClass(String... className) {
127 Pattern p = constructRegexp(className);
128 Variable current = context.variableForName("$_");
130 assert current != null;
132 for (String canonicalName : context.enclosingClasses(current)) {
133 if (p.matcher(canonicalName).matches()) return true;
139 /**Tests whether the current occurrences is in any of the given packages.
141 * @param packageName names of possibly enclosing packages
142 * @return true if and only if the current occurrence is inside any of the
145 public boolean inPackage(String... packageName) {
146 Pattern p = constructRegexp(packageName);
148 return p.matcher(context.enclosingPackage()).matches();
151 private static Pattern constructRegexp(String[] pattern) {
152 StringBuilder regexp = new StringBuilder();
153 boolean first = true;
154 for (String p : pattern) {
155 if (first) regexp.append("|");
158 regexp.append(Pattern.quote(p));
162 Pattern p = Pattern.compile(regexp.toString());