1 /* 2 * Copyright (c) 2016, 2017, 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 24 package jdk.tools.jaotc; 25 26 import java.util.HashSet; 27 import java.util.Iterator; 28 import java.util.regex.Pattern; 29 30 import jdk.vm.ci.meta.ResolvedJavaMethod; 31 32 /** 33 * A class encapsulating any user-specified compilation restrictions. 34 */ 35 final class CompilationSpec { 36 37 /** 38 * Set of method names to restrict compilation to. 39 */ 40 private HashSet<String> compileOnlyStrings = new HashSet<>(); 41 private HashSet<Pattern> compileOnlyPatterns = new HashSet<>(); 42 43 /** 44 * Set of method names that should be excluded from compilation. 45 */ 46 private HashSet<String> excludeStrings = new HashSet<>(); 47 private HashSet<Pattern> excludePatterns = new HashSet<>(); 48 49 /** 50 * Add a {@code compileOnly} directive to the compile-only list. 51 * 52 * @param pattern regex or non-regex pattern string 53 */ 54 void addCompileOnlyPattern(String pattern) { 55 if (pattern.contains("*")) { 56 compileOnlyPatterns.add(Pattern.compile(pattern)); 57 } else { 58 compileOnlyStrings.add(pattern); 59 } 60 } 61 62 /** 63 * Add an {@code exclude} directive to the exclude list. 64 * 65 * @param pattern regex or non-regex pattern string 66 */ 67 void addExcludePattern(String pattern) { 68 if (pattern.contains("*")) { 69 excludePatterns.add(Pattern.compile(pattern)); 70 } else { 71 excludeStrings.add(pattern); 72 } 73 } 74 75 /** 76 * Check if a given method is part of a restrictive compilation. 77 * 78 * @param method method to be checked 79 * @return true or false 80 */ 81 boolean shouldCompileMethod(ResolvedJavaMethod method) { 82 if (compileWithRestrictions()) { 83 // If there are user-specified compileOnly patterns, default action 84 // is not to compile the method. 85 boolean compileMethod = compileOnlyStrings.isEmpty() && compileOnlyPatterns.isEmpty(); 86 87 // Check if the method matches with any of the specified compileOnly patterns. 88 String methodName = JavaMethodInfo.uniqueMethodName(method); 89 90 // compileOnly 91 if (!compileMethod) { 92 compileMethod = compileOnlyStrings.contains(methodName); 93 } 94 if (!compileMethod) { 95 Iterator<Pattern> it = compileOnlyPatterns.iterator(); 96 while (!compileMethod && it.hasNext()) { 97 Pattern pattern = it.next(); 98 compileMethod = pattern.matcher(methodName).matches(); 99 } 100 } 101 102 // exclude 103 if (compileMethod) { 104 compileMethod = !excludeStrings.contains(methodName); 105 } 106 if (compileMethod) { 107 Iterator<Pattern> it = excludePatterns.iterator(); 108 while (compileMethod && it.hasNext()) { 109 Pattern pattern = it.next(); 110 compileMethod = !(pattern.matcher(methodName).matches()); 111 } 112 } 113 return compileMethod; 114 } 115 return true; 116 } 117 118 /** 119 * Return true if compilation restrictions are specified. 120 */ 121 private boolean compileWithRestrictions() { 122 return !(compileOnlyStrings.isEmpty() && compileOnlyPatterns.isEmpty() && excludeStrings.isEmpty() && excludePatterns.isEmpty()); 123 } 124 125 }