Skip to content

Commit d3ea092

Browse files
author
Mourad Abbay
committedFeb 14, 2024
Review FieldLoadOp
Reviewed-by: psandoz
1 parent 21dd5be commit d3ea092

File tree

3 files changed

+81
-17
lines changed

3 files changed

+81
-17
lines changed
 

‎src/java.base/share/classes/java/lang/reflect/code/op/CoreOps.java

+41-9
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,8 @@ public final FieldDesc fieldDescriptor() {
14181418
public static final class FieldLoadOp extends FieldAccessOp implements Op.Pure {
14191419
public static final String NAME = "field.load";
14201420

1421+
final TypeElement resultType;
1422+
14211423
public static FieldLoadOp create(OpDefinition def) {
14221424
if (def.operands().size() > 1) {
14231425
throw new IllegalArgumentException("Operation must accept zero or one operand");
@@ -1434,10 +1436,14 @@ public static FieldLoadOp create(OpDefinition def) {
14341436

14351437
FieldLoadOp(OpDefinition opdef, FieldDesc fieldDescriptor) {
14361438
super(opdef, fieldDescriptor);
1439+
1440+
resultType = opdef.resultType();
14371441
}
14381442

14391443
FieldLoadOp(FieldLoadOp that, CopyContext cc) {
14401444
super(that, cc);
1445+
1446+
resultType = that.resultType();
14411447
}
14421448

14431449
@Override
@@ -1446,20 +1452,22 @@ public FieldLoadOp transform(CopyContext cc, OpTransformer ot) {
14461452
}
14471453

14481454
// instance
1449-
FieldLoadOp(FieldDesc descriptor, Value receiver) {
1450-
super(NAME,
1451-
List.of(receiver), descriptor);
1455+
FieldLoadOp(TypeElement resultType, FieldDesc descriptor, Value receiver) {
1456+
super(NAME, List.of(receiver), descriptor);
1457+
1458+
this.resultType = resultType;
14521459
}
14531460

14541461
// static
1455-
FieldLoadOp(FieldDesc descriptor) {
1456-
super(NAME,
1457-
List.of(), descriptor);
1462+
FieldLoadOp(TypeElement resultType, FieldDesc descriptor) {
1463+
super(NAME, List.of(), descriptor);
1464+
1465+
this.resultType = resultType;
14581466
}
14591467

14601468
@Override
14611469
public TypeElement resultType() {
1462-
return fieldDescriptor().type();
1470+
return resultType;
14631471
}
14641472
}
14651473

@@ -3219,16 +3227,40 @@ public static NewOp newArray(TypeElement arrayType, Value length) {
32193227
* @return the field load operation
32203228
*/
32213229
public static FieldAccessOp.FieldLoadOp fieldLoad(FieldDesc descriptor, Value receiver) {
3222-
return new FieldAccessOp.FieldLoadOp(descriptor, receiver);
3230+
return new FieldAccessOp.FieldLoadOp(descriptor.type(), descriptor, receiver);
3231+
}
3232+
3233+
/**
3234+
* Creates a field load operation to a non-static field.
3235+
*
3236+
* @param resultType the result type of the operation
3237+
* @param descriptor the field descriptor
3238+
* @param receiver the receiver value
3239+
* @return the field load operation
3240+
*/
3241+
public static FieldAccessOp.FieldLoadOp fieldLoad(TypeElement resultType, FieldDesc descriptor, Value receiver) {
3242+
return new FieldAccessOp.FieldLoadOp(resultType, descriptor, receiver);
32233243
}
32243244

32253245
/**
32263246
* Creates a field load operation to a static field.
32273247
*
3248+
* @param descriptor the field descriptor
32283249
* @return the field load operation
32293250
*/
32303251
public static FieldAccessOp.FieldLoadOp fieldLoad(FieldDesc descriptor) {
3231-
return new FieldAccessOp.FieldLoadOp(descriptor);
3252+
return new FieldAccessOp.FieldLoadOp(descriptor.type(), descriptor);
3253+
}
3254+
3255+
/**
3256+
* Creates a field load operation to a static field.
3257+
*
3258+
* @param resultType the result type of the operation
3259+
* @param descriptor the field descriptor
3260+
* @return the field load operation
3261+
*/
3262+
public static FieldAccessOp.FieldLoadOp fieldLoad(TypeElement resultType, FieldDesc descriptor) {
3263+
return new FieldAccessOp.FieldLoadOp(resultType, descriptor);
32323264
}
32333265

32343266
/**

‎src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ReflectMethods.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -797,10 +797,11 @@ void applyCompoundAssign(JCTree.JCExpression lhs, Function<Value, Value> scanRhs
797797
FieldDesc fd = symbolToFieldDesc(sym, symbolSiteType(sym));
798798

799799
Op.Result lhsOpValue;
800+
TypeElement resultType = typeToTypeElement(sym.type);
800801
if (sym.isStatic()) {
801-
lhsOpValue = append(CoreOps.fieldLoad(fd));
802+
lhsOpValue = append(CoreOps.fieldLoad(resultType, fd));
802803
} else {
803-
lhsOpValue = append(CoreOps.fieldLoad(fd, thisValue()));
804+
lhsOpValue = append(CoreOps.fieldLoad(resultType, fd, thisValue()));
804805
}
805806
// Scan the rhs
806807
Value r = scanRhs.apply(lhsOpValue);
@@ -826,10 +827,11 @@ void applyCompoundAssign(JCTree.JCExpression lhs, Function<Value, Value> scanRhs
826827
FieldDesc fd = symbolToFieldDesc(sym, assign.selected.type);
827828

828829
Op.Result lhsOpValue;
830+
TypeElement resultType = typeToTypeElement(sym.type);
829831
if (sym.isStatic()) {
830-
lhsOpValue = append(CoreOps.fieldLoad(fd));
832+
lhsOpValue = append(CoreOps.fieldLoad(resultType, fd));
831833
} else {
832-
lhsOpValue = append(CoreOps.fieldLoad(fd, receiver));
834+
lhsOpValue = append(CoreOps.fieldLoad(resultType, fd, receiver));
833835
}
834836
// Scan the rhs
835837
Value r = scanRhs.apply(lhsOpValue);
@@ -877,10 +879,11 @@ public void visitIdent(JCIdent tree) {
877879
result = thisValue();
878880
} else {
879881
FieldDesc fd = symbolToFieldDesc(sym, symbolSiteType(sym));
882+
TypeElement resultType = typeToTypeElement(sym.type);
880883
if (sym.isStatic()) {
881-
result = append(CoreOps.fieldLoad(fd));
884+
result = append(CoreOps.fieldLoad(resultType, fd));
882885
} else {
883-
result = append(CoreOps.fieldLoad(fd, thisValue()));
886+
result = append(CoreOps.fieldLoad(resultType, fd, thisValue()));
884887
}
885888
}
886889
}
@@ -927,10 +930,11 @@ public void visitSelect(JCFieldAccess tree) {
927930
case FIELD, ENUM_CONSTANT -> {
928931
FieldDesc fd = symbolToFieldDesc(sym, qualifierTarget.hasTag(NONE) ?
929932
tree.selected.type : qualifierTarget);
933+
TypeElement resultType = typeToTypeElement(types.memberType(tree.selected.type, sym));
930934
if (sym.isStatic()) {
931-
result = append(CoreOps.fieldLoad(fd));
935+
result = append(CoreOps.fieldLoad(resultType, fd));
932936
} else {
933-
result = append(CoreOps.fieldLoad(fd, receiver));
937+
result = append(CoreOps.fieldLoad(resultType, fd, receiver));
934938
}
935939
}
936940
case INTERFACE, CLASS, ENUM -> {

‎test/langtools/tools/javac/reflect/FieldAccessTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -635,4 +635,32 @@ void test23() {
635635
void test24() {
636636
PrintStream ps = out;
637637
}
638+
639+
static class Box<T> {
640+
public T v;
641+
642+
public Box(T v) {
643+
this.v = v;
644+
}
645+
}
646+
647+
@CodeReflection
648+
@IR("""
649+
func @"test25" ()void -> {
650+
%0 : java.lang.String = constant @"abc";
651+
%1 : FieldAccessTest$Box<java.lang.String> = new %0 @"(java.lang.Object)FieldAccessTest$Box";
652+
%2 : Var<FieldAccessTest$Box<java.lang.String>> = var %1 @"b";
653+
%3 : FieldAccessTest$Box<java.lang.String> = var.load %2;
654+
%4 : java.lang.String = field.load %3 @"FieldAccessTest$Box::v()java.lang.Object";
655+
%5 : Var<java.lang.String> = var %4 @"s";
656+
return;
657+
};
658+
""")
659+
static void test25() {
660+
Box<String> b = new Box<>("abc");
661+
String s = b.v;
662+
}
663+
664+
//@@@ unqualified access to field of generic type needs to be tested
665+
// waiting for a new way of modeling types, so that type variables are captured in the IR
638666
}

0 commit comments

Comments
 (0)
Please sign in to comment.