MatchingUtils.java revision 2846:072008f47620
1/*
2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.javac.util;
27
28import java.util.regex.Pattern;
29import javax.lang.model.SourceVersion;
30
31/**Utilities to convert an import-like string to a regexp.
32 *
33 *  <p><b>This is NOT part of any supported API.
34 *  If you write code that depends on this, you do so at your own risk.
35 *  This code and its internal interfaces are subject to change or
36 *  deletion without notice.</b>
37 */
38public class MatchingUtils {
39    private static final Pattern allMatches = Pattern.compile(".*");
40
41    /**
42     * Return true if the argument string is a valid import-style
43     * string specifying claimed annotations; return false otherwise.
44     */
45    public static boolean isValidImportString(String s) {
46        if (s.equals("*"))
47            return true;
48
49        boolean valid = true;
50        String t = s;
51        int index = t.indexOf('*');
52
53        if (index != -1) {
54            // '*' must be last character...
55            if (index == t.length() -1) {
56                // ... any and preceding character must be '.'
57                if ( index-1 >= 0 ) {
58                    valid = t.charAt(index-1) == '.';
59                    // Strip off ".*$" for identifier checks
60                    t = t.substring(0, t.length()-2);
61                }
62            } else
63                return false;
64        }
65
66        // Verify string is off the form (javaId \.)+ or javaId
67        if (valid) {
68            String[] javaIds = t.split("\\.", t.length()+2);
69            for(String javaId: javaIds)
70                valid &= SourceVersion.isIdentifier(javaId);
71        }
72        return valid;
73    }
74
75    public static Pattern validImportStringToPattern(String s) {
76        if (s.equals("*")) {
77            return allMatches;
78        } else {
79            String s_prime = s.replace(".", "\\.");
80
81            if (s_prime.endsWith("*")) {
82                s_prime =  s_prime.substring(0, s_prime.length() - 1) + ".+";
83            }
84
85            return Pattern.compile(s_prime);
86        }
87    }
88
89}
90