file/src/org/netbeans/modules/jackpot30/file/conditionapi/DefaultRuleUtilities.java
author Jan Lahoda <jlahoda@netbeans.org>
Thu, 21 Jul 2011 22:51:52 +0200
branchrelease701
changeset 648 cb630b7113bf
parent 513 4e4e3643aa73
permissions -rw-r--r--
bitbucket-20: adding conditions for enclosing class/package (transplanting from trunk).
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
     5  *
     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]"
    23  *
    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.
    34  *
    35  * Contributor(s):
    36  *
    37  * Portions Copyrighted 2009 Sun Microsystems, Inc.
    38  */
    39 
    40 package org.netbeans.modules.jackpot30.file.conditionapi;
    41 
    42 import java.util.Arrays;
    43 import java.util.EnumSet;
    44 import java.util.Set;
    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;
    50 
    51 /**
    52  *
    53  * @author lahvac
    54  */
    55 public final class DefaultRuleUtilities {
    56 
    57     private final Context context;
    58     private final Matcher matcher;
    59 
    60     DefaultRuleUtilities(Context context, Matcher matcher) {
    61         this.context = context;
    62         this.matcher = matcher;
    63     }
    64     
    65     public boolean referencedIn(Variable variable, Variable in) {
    66         return matcher.referencedIn(variable, in);
    67     }
    68 
    69     public boolean sourceVersionGE(SourceVersion source) {
    70         return context.sourceVersion().compareTo(source) >= 0;
    71     }
    72 
    73     public boolean hasModifier(Variable variable, Modifier modifier) {
    74         return context.modifiers(variable).contains(modifier);
    75     }
    76 
    77     public boolean parentMatches(String pattern) {
    78         Variable parent = context.parent(context.variableForName("$_"));
    79         
    80         if (parent == null) {
    81             return false;
    82         }
    83         
    84         return matcher.matchesAny(parent, pattern); //XXX: $_ currently not part of variables map, so this won't work!!!
    85     }
    86 
    87     public boolean elementKindMatches(Variable variable, ElementKind... kind) {
    88         Set<ElementKind> kinds = EnumSet.noneOf(ElementKind.class);
    89         
    90         kinds.addAll(Arrays.asList(kind));
    91 
    92         return kinds.contains(context.elementKind(variable));
    93     }
    94 
    95     public boolean isNullLiteral(@NonNull Variable var) {
    96         return context.isNullLiteral(var);
    97     }
    98     
    99     public boolean matches(String pattern) {
   100         Variable current = context.variableForName("$_");
   101 
   102         assert current != null;
   103 
   104         return matchesAny(current, pattern); //XXX: $_ currently not part of variables map, so this won't work!!!
   105     }
   106 
   107     public boolean matchesWithBind(Variable var, String pattern) {
   108         return matcher.matchesWithBind(var, pattern);
   109     }
   110 
   111     public boolean matchesAny(Variable var, String... patterns) {
   112         return matcher.matchesAny(var, patterns);
   113     }
   114 
   115     public boolean containsAny(Variable var, String... patterns) {
   116         return matcher.containsAny(var, patterns);
   117     }
   118 
   119     /**Tests whether the current occurrences is enclosed (directly or indirectly)
   120      * by any of the specified classes.
   121      *
   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.
   125      */
   126     public boolean inClass(String... className) {
   127         Pattern p = constructRegexp(className);
   128         Variable current = context.variableForName("$_");
   129 
   130         assert current != null;
   131 
   132         for (String canonicalName : context.enclosingClasses(current)) {
   133             if (p.matcher(canonicalName).matches()) return true;
   134         }
   135 
   136         return false;
   137     }
   138 
   139     /**Tests whether the current occurrences is in any of the given packages.
   140      *
   141      * @param packageName names of possibly enclosing packages
   142      * @return true if and only if the current occurrence is inside any of the
   143      *              given packages
   144      */
   145     public boolean inPackage(String... packageName) {
   146         Pattern p = constructRegexp(packageName);
   147 
   148         return p.matcher(context.enclosingPackage()).matches();
   149     }
   150 
   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("|");
   156 
   157             regexp.append("(");
   158             regexp.append(Pattern.quote(p));
   159             regexp.append(")");
   160             first = false;
   161         }
   162         Pattern p = Pattern.compile(regexp.toString());
   163         return p;
   164     }
   165 }