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.lang.reflect.Constructor;
27import java.lang.reflect.Executable;
28
29/**
30 * Represents a method in CompileControl method signature
31 */
32public class MethodType extends MethodElementType {
33    private static final char[] INVALID_CHARS = { '.', '/' };
34
35    public MethodType(Executable method) {
36        // Use pack/subpack/Class::method separators style
37        super(MethodDescriptor.Separator.DOT);
38        if (method instanceof Constructor) {
39            element = "<init>";
40        } else {
41            element = method.getName();
42        }
43        regexp = element;
44    }
45
46    @Override
47    public boolean isValid() {
48        for (char ch : INVALID_CHARS) {
49            if (element.indexOf(ch) != -1) {
50                return false;
51            }
52        }
53        if (element.isEmpty()) {
54            // Shouldn't be empty
55            return false;
56        }
57        if (element.contains("<") || element.contains(">")) {
58            return element.matches("(\\*)?<(cl)?init>(\\*)?");
59        }
60        return super.isValid();
61    }
62
63    @Override
64    public void setPattern(MethodDescriptor.PatternType patternType) {
65        switch (patternType) {
66            case EXACT:
67                break;
68            case PREFIX:
69                regexp = ".*" + regexp;
70                element = "*" + element;
71                break;
72            case ANY:
73                regexp = "[^(]*";
74                element = "*";
75                break;
76            case SUFFIX:
77                regexp = regexp + "[^(]*";
78                element = element + "*";
79                break;
80            case SUBSTRING:
81                setPattern(MethodDescriptor.PatternType.PREFIX);
82                setPattern(MethodDescriptor.PatternType.SUFFIX);
83                break;
84            default:
85                throw new IllegalArgumentException("ERROR: wrong pattern type "
86                        + patternType);
87        }
88    }
89}
90