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).
jlahoda@217
     1
/*
jlahoda@217
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jlahoda@217
     3
 *
jlahoda@217
     4
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
jlahoda@217
     5
 *
jlahoda@217
     6
 * The contents of this file are subject to the terms of either the GNU
jlahoda@217
     7
 * General Public License Version 2 only ("GPL") or the Common
jlahoda@217
     8
 * Development and Distribution License("CDDL") (collectively, the
jlahoda@217
     9
 * "License"). You may not use this file except in compliance with the
jlahoda@217
    10
 * License. You can obtain a copy of the License at
jlahoda@217
    11
 * http://www.netbeans.org/cddl-gplv2.html
jlahoda@217
    12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jlahoda@217
    13
 * specific language governing permissions and limitations under the
jlahoda@217
    14
 * License.  When distributing the software, include this License Header
jlahoda@217
    15
 * Notice in each file and include the License file at
jlahoda@217
    16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jlahoda@217
    17
 * particular file as subject to the "Classpath" exception as provided
jlahoda@217
    18
 * by Sun in the GPL Version 2 section of the License file that
jlahoda@217
    19
 * accompanied this code. If applicable, add the following below the
jlahoda@217
    20
 * License Header, with the fields enclosed by brackets [] replaced by
jlahoda@217
    21
 * your own identifying information:
jlahoda@217
    22
 * "Portions Copyrighted [year] [name of copyright owner]"
jlahoda@217
    23
 *
jlahoda@217
    24
 * If you wish your version of this file to be governed by only the CDDL
jlahoda@217
    25
 * or only the GPL Version 2, indicate your decision by adding
jlahoda@217
    26
 * "[Contributor] elects to include this software in this distribution
jlahoda@217
    27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jlahoda@217
    28
 * single choice of license, a recipient has the option to distribute
jlahoda@217
    29
 * your version of this file under either the CDDL, the GPL Version 2 or
jlahoda@217
    30
 * to extend the choice of license to its licensees as provided above.
jlahoda@217
    31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jlahoda@217
    32
 * Version 2 license, then the option applies only if the new code is
jlahoda@217
    33
 * made subject to such option by the copyright holder.
jlahoda@217
    34
 *
jlahoda@217
    35
 * Contributor(s):
jlahoda@217
    36
 *
jlahoda@217
    37
 * Portions Copyrighted 2009 Sun Microsystems, Inc.
jlahoda@217
    38
 */
jlahoda@217
    39
jlahoda@189
    40
package org.netbeans.modules.jackpot30.file.conditionapi;
jlahoda@189
    41
jlahoda@510
    42
import java.util.Arrays;
jlahoda@510
    43
import java.util.EnumSet;
jlahoda@510
    44
import java.util.Set;
jlahoda@644
    45
import java.util.regex.Pattern;
jlahoda@189
    46
import javax.lang.model.SourceVersion;
jlahoda@200
    47
import javax.lang.model.element.ElementKind;
jlahoda@189
    48
import javax.lang.model.element.Modifier;
jlahoda@201
    49
import org.netbeans.api.annotations.common.NonNull;
jlahoda@189
    50
jlahoda@189
    51
/**
jlahoda@189
    52
 *
jlahoda@189
    53
 * @author lahvac
jlahoda@189
    54
 */
jlahoda@189
    55
public final class DefaultRuleUtilities {
jlahoda@189
    56
jlahoda@189
    57
    private final Context context;
jlahoda@189
    58
    private final Matcher matcher;
jlahoda@189
    59
jlahoda@189
    60
    DefaultRuleUtilities(Context context, Matcher matcher) {
jlahoda@189
    61
        this.context = context;
jlahoda@189
    62
        this.matcher = matcher;
jlahoda@189
    63
    }
jlahoda@189
    64
    
jlahoda@189
    65
    public boolean referencedIn(Variable variable, Variable in) {
jlahoda@189
    66
        return matcher.referencedIn(variable, in);
jlahoda@189
    67
    }
jlahoda@189
    68
jlahoda@189
    69
    public boolean sourceVersionGE(SourceVersion source) {
jlahoda@189
    70
        return context.sourceVersion().compareTo(source) >= 0;
jlahoda@189
    71
    }
jlahoda@189
    72
jlahoda@189
    73
    public boolean hasModifier(Variable variable, Modifier modifier) {
jlahoda@189
    74
        return context.modifiers(variable).contains(modifier);
jlahoda@189
    75
    }
jlahoda@189
    76
jlahoda@189
    77
    public boolean parentMatches(String pattern) {
jlahoda@189
    78
        Variable parent = context.parent(context.variableForName("$_"));
jlahoda@189
    79
        
jlahoda@189
    80
        if (parent == null) {
jlahoda@189
    81
            return false;
jlahoda@189
    82
        }
jlahoda@189
    83
        
jlahoda@261
    84
        return matcher.matchesAny(parent, pattern); //XXX: $_ currently not part of variables map, so this won't work!!!
jlahoda@189
    85
    }
jlahoda@200
    86
jlahoda@510
    87
    public boolean elementKindMatches(Variable variable, ElementKind... kind) {
jlahoda@510
    88
        Set<ElementKind> kinds = EnumSet.noneOf(ElementKind.class);
jlahoda@510
    89
        
jlahoda@510
    90
        kinds.addAll(Arrays.asList(kind));
jlahoda@510
    91
jlahoda@510
    92
        return kinds.contains(context.elementKind(variable));
jlahoda@200
    93
    }
jlahoda@201
    94
jlahoda@201
    95
    public boolean isNullLiteral(@NonNull Variable var) {
jlahoda@201
    96
        return context.isNullLiteral(var);
jlahoda@201
    97
    }
jlahoda@201
    98
    
jlahoda@246
    99
    public boolean matches(String pattern) {
jlahoda@246
   100
        Variable current = context.variableForName("$_");
jlahoda@246
   101
jlahoda@246
   102
        assert current != null;
jlahoda@246
   103
jlahoda@261
   104
        return matchesAny(current, pattern); //XXX: $_ currently not part of variables map, so this won't work!!!
jlahoda@261
   105
    }
jlahoda@261
   106
jlahoda@513
   107
    public boolean matchesWithBind(Variable var, String pattern) {
jlahoda@513
   108
        return matcher.matchesWithBind(var, pattern);
jlahoda@513
   109
    }
jlahoda@513
   110
jlahoda@261
   111
    public boolean matchesAny(Variable var, String... patterns) {
jlahoda@261
   112
        return matcher.matchesAny(var, patterns);
jlahoda@261
   113
    }
jlahoda@261
   114
jlahoda@261
   115
    public boolean containsAny(Variable var, String... patterns) {
jlahoda@261
   116
        return matcher.containsAny(var, patterns);
jlahoda@246
   117
    }
jlahoda@644
   118
jlahoda@644
   119
    /**Tests whether the current occurrences is enclosed (directly or indirectly)
jlahoda@644
   120
     * by any of the specified classes.
jlahoda@644
   121
     *
jlahoda@644
   122
     * @param className canonical class names of possibly enclosing classes
jlahoda@644
   123
     * @return true if and only if the current occurrence is directly or indirectly
jlahoda@644
   124
     *              enclosed by any of the given classes.
jlahoda@644
   125
     */
jlahoda@644
   126
    public boolean inClass(String... className) {
jlahoda@644
   127
        Pattern p = constructRegexp(className);
jlahoda@644
   128
        Variable current = context.variableForName("$_");
jlahoda@644
   129
jlahoda@644
   130
        assert current != null;
jlahoda@644
   131
jlahoda@644
   132
        for (String canonicalName : context.enclosingClasses(current)) {
jlahoda@644
   133
            if (p.matcher(canonicalName).matches()) return true;
jlahoda@644
   134
        }
jlahoda@644
   135
jlahoda@644
   136
        return false;
jlahoda@644
   137
    }
jlahoda@644
   138
jlahoda@644
   139
    /**Tests whether the current occurrences is in any of the given packages.
jlahoda@644
   140
     *
jlahoda@644
   141
     * @param packageName names of possibly enclosing packages
jlahoda@644
   142
     * @return true if and only if the current occurrence is inside any of the
jlahoda@644
   143
     *              given packages
jlahoda@644
   144
     */
jlahoda@644
   145
    public boolean inPackage(String... packageName) {
jlahoda@644
   146
        Pattern p = constructRegexp(packageName);
jlahoda@644
   147
jlahoda@644
   148
        return p.matcher(context.enclosingPackage()).matches();
jlahoda@644
   149
    }
jlahoda@644
   150
jlahoda@644
   151
    private static Pattern constructRegexp(String[] pattern) {
jlahoda@644
   152
        StringBuilder regexp = new StringBuilder();
jlahoda@644
   153
        boolean first = true;
jlahoda@644
   154
        for (String p : pattern) {
jlahoda@644
   155
            if (first) regexp.append("|");
jlahoda@644
   156
jlahoda@644
   157
            regexp.append("(");
jlahoda@644
   158
            regexp.append(Pattern.quote(p));
jlahoda@644
   159
            regexp.append(")");
jlahoda@644
   160
            first = false;
jlahoda@644
   161
        }
jlahoda@644
   162
        Pattern p = Pattern.compile(regexp.toString());
jlahoda@644
   163
        return p;
jlahoda@644
   164
    }
jlahoda@189
   165
}