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 jdk.test.lib.jittester.factories;
25
26import java.util.ArrayList;
27
28import jdk.test.lib.jittester.LocalVariable;
29import jdk.test.lib.jittester.ProductionFailedException;
30import jdk.test.lib.jittester.Symbol;
31import jdk.test.lib.jittester.SymbolTable;
32import jdk.test.lib.jittester.Type;
33import jdk.test.lib.jittester.VariableInfo;
34import jdk.test.lib.jittester.utils.PseudoRandom;
35
36class LocalVariableFactory extends Factory<LocalVariable> {
37    private final Type type;
38    private final int flags;
39
40    LocalVariableFactory(Type type, int flags) {
41        this.type = type;
42        this.flags = flags;
43    }
44
45    @Override
46    public LocalVariable produce() throws ProductionFailedException {
47        // Get the variables of the requested type from SymbolTable
48        ArrayList<Symbol> allVariables = new ArrayList<>(SymbolTable.get(type, VariableInfo.class));
49        if (!allVariables.isEmpty()) {
50            PseudoRandom.shuffle(allVariables);
51            for (Symbol symbol : allVariables) {
52                VariableInfo varInfo = (VariableInfo) symbol;
53                if ((varInfo.flags & VariableInfo.FINAL) == (flags & VariableInfo.FINAL)
54                        && (varInfo.flags & VariableInfo.INITIALIZED) == (flags & VariableInfo.INITIALIZED)
55                        && (varInfo.flags & VariableInfo.LOCAL) > 0) {
56                    return new LocalVariable(varInfo);
57                }
58            }
59        }
60        throw new ProductionFailedException();
61    }
62}
63