mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 23:34:52 +02:00
defineField
This commit is contained in:
parent
c8b10320af
commit
8268427dde
2 changed files with 27 additions and 0 deletions
|
@ -28,6 +28,12 @@ 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
|
||||
|
@ -48,4 +54,18 @@ public abstract class Library {
|
|||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ 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.$;
|
||||
import static compiler.lib.template_framework.Template.addName;
|
||||
import static compiler.lib.template_framework.Template.weighNames;
|
||||
import static compiler.lib.template_framework.Template.sampleName;
|
||||
|
@ -43,6 +44,7 @@ import static compiler.lib.template_framework.Template.sampleName;
|
|||
*/
|
||||
public record Value(Object defTokens, Object useTokens) {
|
||||
private static final Random RANDOM = Utils.getRandomInstance();
|
||||
private static int nextUniqueId = 0;
|
||||
|
||||
public static Value fromUseToken(Object useToken) {
|
||||
return new Value("", useToken);
|
||||
|
@ -59,6 +61,11 @@ public record Value(Object defTokens, Object useTokens) {
|
|||
}
|
||||
// Define new field, load from it.
|
||||
if (RANDOM.nextInt(4) == 0 && Library.CLASS_HOOK.isSet()) {
|
||||
String fieldName = $("field") + "_" + (nextUniqueId++);
|
||||
Name name = new Name(fieldName, type, false, 1);
|
||||
Object value = type.con();
|
||||
TemplateWithArgs def = Library.defineField(name, true, value);
|
||||
return new Value(def, fieldName);
|
||||
}
|
||||
return fromUseToken(type.con());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue