jdk/test/hotspot/jtreg/compiler/lib/template_library/Library.java
2025-02-24 12:51:51 +01:00

71 lines
2.6 KiB
Java

/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package compiler.lib.template_library;
import java.util.List;
import java.util.Random;
import jdk.test.lib.Utils;
import compiler.lib.template_framework.Hook;
import compiler.lib.template_framework.Template;
import compiler.lib.template_framework.Name;
import compiler.lib.template_framework.TemplateWithArgs;
import static compiler.lib.template_framework.Template.body;
import static compiler.lib.template_framework.Template.let;
import static compiler.lib.template_framework.Template.addName;
/**
* TODO
* The Library provides a collection of helpful Templates and Hooks.
*
* TODO more operators
* TODO more templates
* TODO configure choice and other randomization?
*/
public abstract class Library {
private static final Random RANDOM = Utils.getRandomInstance();
public static final Hook CLASS_HOOK = new Hook("Class");
public static final Hook METHOD_HOOK = new Hook("Method");
static <T> T choice(List<T> list) {
if (list.isEmpty()) { return null; }
int i = RANDOM.nextInt(list.size());
return list.get(i);
}
public static TemplateWithArgs defineField(Name name, boolean isStatic, Object valueToken) {
var define = Template.make(() -> body(
let("static", isStatic ? "static" : ""),
let("type", name.type()),
let("name", name.name()),
"public #static #type #name = ", valueToken, ";\n",
addName(name)
));
var template = Template.make(() -> body(
CLASS_HOOK.insert(define.withArgs())
));
return template.withArgs();
}
}