Path: blob/master/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/AbstractCommandBuilder.java
41161 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.compilercontrol.share.scenario;2425import compiler.compilercontrol.share.method.MethodDescriptor;26import compiler.compilercontrol.share.pool.PoolHelper;27import jdk.test.lib.util.Pair;2829import java.lang.reflect.Executable;30import java.util.ArrayList;31import java.util.Collections;32import java.util.HashMap;33import java.util.List;34import java.util.Map;35import java.util.concurrent.Callable;3637/**38* An abstract class that builds states by applying39* commands one after another40*/41public abstract class AbstractCommandBuilder42implements StateBuilder<CompileCommand> {43protected static final List<Pair<Executable, Callable<?>>> METHODS44= new PoolHelper().getAllMethods();45protected final List<CompileCommand> compileCommands = new ArrayList<>();4647@Override48public void add(CompileCommand command) {49compileCommands.add(command);50CommandStateBuilder.getInstance().add(command);51}5253@Override54public Map<Executable, State> getStates() {55return CommandStateBuilder.getInstance().getStates();56}5758@Override59public List<CompileCommand> getCompileCommands() {60return Collections.unmodifiableList(compileCommands);61}6263@Override64public boolean isValid() {65boolean isValid = true;66for (CompileCommand cmd : compileCommands) {67isValid &= cmd.isValid();68}69return isValid;70}7172/*73* This is an internal class used to build states for commands given from74* options and a file. As all commands are added into a single set in75* CompilerOracle, we need a class that builds states in the same manner76*/77private static class CommandStateBuilder {78private static final CommandStateBuilder INSTANCE79= new CommandStateBuilder();80private final List<CompileCommand> optionCommands = new ArrayList<>();81private final List<CompileCommand> fileCommands = new ArrayList<>();8283private CommandStateBuilder() { }8485public static CommandStateBuilder getInstance() {86return INSTANCE;87}8889public void add(CompileCommand command) {90switch (command.type) {91case OPTION:92optionCommands.add(command);93break;94case FILE:95fileCommands.add(command);96break;97default:98throw new Error("TESTBUG: wrong type: " + command.type);99}100}101102public Map<Executable, State> getStates() {103List<CompileCommand> commandList = new ArrayList<>();104commandList.addAll(optionCommands);105commandList.addAll(fileCommands);106Map<Executable, State> states = new HashMap<>();107for (Pair<Executable, Callable<?>> pair : METHODS) {108Executable exec = pair.first;109State state = getState(commandList, exec);110states.put(exec, state);111}112return states;113}114115private State getState(List<CompileCommand> commandList,116Executable exec) {117State state = new State();118MethodDescriptor execDesc = new MethodDescriptor(exec);119for (CompileCommand compileCommand : commandList) {120if (compileCommand.isValid()) {121// Create a copy without compiler set122CompileCommand cc = new CompileCommand(123compileCommand.command,124compileCommand.methodDescriptor,125/* CompileCommand option and file doesn't support126compiler setting */127null,128compileCommand.type);129MethodDescriptor md = cc.methodDescriptor;130// if executable matches regex then apply the state131if (execDesc.getCanonicalString().matches(md.getRegexp())) {132if (cc.command == Command.COMPILEONLY133&& !state.isCompilable()) {134/* if the method was already excluded it will not135be compilable again */136} else {137state.apply(cc);138}139}140}141}142143/*144* Set compilation states for methods that don't match145* any compileonly command. Such methods should be excluded146* from compilation147*/148for (CompileCommand compileCommand : commandList) {149if (compileCommand.isValid()150&& (compileCommand.command == Command.COMPILEONLY)) {151MethodDescriptor md = compileCommand.methodDescriptor;152if (!execDesc.getCanonicalString().matches(md.getRegexp())153// if compilation state wasn't set before154&& (!state.getCompilableOptional(155// no matter C1, C2 or both156Scenario.Compiler.C2).isPresent())) {157/* compileonly excludes only methods that haven't been158already set to be compilable or excluded */159state.setC1Compilable(false);160state.setC2Compilable(false);161}162}163}164return state;165}166}167}168169170