1/*
2 * Copyright (c) 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package compiler.compilercontrol.share.method;
25
26import java.util.regex.Pattern;
27
28/**
29 * Class represents an element of the MethodDescriptor
30 * used as pattern for CompilerCommand method strings
31 */
32public abstract class MethodElementType {
33    private static final char[] INVALID_CHARS = { ';', '[', '(', ')', ']',
34            '<', '>'};
35    protected String element;
36    protected String regexp;
37    protected MethodDescriptor.Separator separator;
38
39    /**
40     * Constructor
41     */
42    protected MethodElementType(MethodDescriptor.Separator separator) {
43        this.separator = separator;
44    }
45
46    /**
47     * Gets element's separator
48     *
49     * @return separator instance
50     */
51    public MethodDescriptor.Separator getSeparator() {
52        return separator;
53    }
54
55    /**
56     * Sets separator for this element
57     *
58     * @param separator separator type
59     */
60    public void setSeparator(MethodDescriptor.Separator separator) {
61        this.separator = separator;
62    }
63
64    /**
65     * Gets String representation of the element
66     *
67     * @return element string
68     */
69    public String getElement() {
70        return element;
71    }
72
73    /**
74     * Sets String representation of the element
75     *
76     * @param element custom string to be used as an element
77     */
78    public void setElement(String element) {
79        this.element = element;
80        this.regexp = Pattern.quote(element);
81    }
82
83    /**
84     * Shows that the element is valid according to CompilerControl and JVMS specs
85     *
86     * @return true, if the element is a valid string
87     */
88    public boolean isValid() {
89        for (char ch : INVALID_CHARS) {
90            if (element.indexOf(ch) != -1) {
91                return false;
92            }
93        }
94        // Check for * usage
95        if (element.equals("**")) {
96            return false;
97        }
98        for (int i = 0; i < element.length(); i++) {
99            char c = element.charAt(i);
100            if (c == '*' && i > 0 && i < element.length() - 1) {
101                // Embedded * isn't allowed
102                return false;
103            }
104        }
105        return true;
106    }
107
108    /**
109     * Creates pattern of a given type
110     *
111     * @param patternType type of the pattern
112     */
113    public void setPattern(MethodDescriptor.PatternType patternType) {
114        switch (patternType) {
115            case EXACT:
116                break;
117            case PREFIX:
118                regexp = ".*" + regexp;
119                element = "*" + element;
120                break;
121            case ANY:
122                regexp = ".*";
123                element = "*";
124                break;
125            case SUFFIX:
126                regexp = regexp + ".*";
127                element = element + "*";
128                break;
129            case SUBSTRING:
130                setPattern(MethodDescriptor.PatternType.PREFIX);
131                setPattern(MethodDescriptor.PatternType.SUFFIX);
132                break;
133            default:
134                throw new IllegalArgumentException("ERROR: wrong pattern type"
135                        + patternType);
136        }
137    }
138
139    /**
140     * Gets regular expression of this element
141     *
142     * @return string representation of regexp
143     */
144    public String getRegexp() {
145        return regexp;
146    }
147}
148