8339710: Avoid initializing AccessFlag related classes in write-only cases

Reviewed-by: liach
This commit is contained in:
Claes Redestad 2024-09-09 05:53:29 +00:00
parent a18d9d84cd
commit b45fe17450
3 changed files with 39 additions and 3 deletions

View File

@ -25,8 +25,10 @@
package jdk.internal.classfile.impl; package jdk.internal.classfile.impl;
import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDescs; import java.lang.constant.ConstantDescs;
import java.lang.constant.MethodTypeDesc; import java.lang.constant.MethodTypeDesc;
import java.lang.reflect.AccessFlag;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -82,18 +84,39 @@ public final class DirectClassBuilder
return this; return this;
} }
@Override
public ClassBuilder withFlags(int flags) {
setFlags(flags);
return this;
}
@Override
public ClassBuilder withField(String name,
ClassDesc descriptor,
int flags) {
return withField(new DirectFieldBuilder(constantPool, context,
constantPool.utf8Entry(name), constantPool.utf8Entry(descriptor), flags, null));
}
@Override
public ClassBuilder withField(Utf8Entry name,
Utf8Entry descriptor,
int flags) {
return withField(new DirectFieldBuilder(constantPool, context, name, descriptor, flags, null));
}
@Override @Override
public ClassBuilder withField(Utf8Entry name, public ClassBuilder withField(Utf8Entry name,
Utf8Entry descriptor, Utf8Entry descriptor,
Consumer<? super FieldBuilder> handler) { Consumer<? super FieldBuilder> handler) {
return withField(new DirectFieldBuilder(constantPool, context, name, descriptor, null) return withField(new DirectFieldBuilder(constantPool, context, name, descriptor, 0, null)
.run(handler)); .run(handler));
} }
@Override @Override
public ClassBuilder transformField(FieldModel field, FieldTransform transform) { public ClassBuilder transformField(FieldModel field, FieldTransform transform) {
DirectFieldBuilder builder = new DirectFieldBuilder(constantPool, context, field.fieldName(), DirectFieldBuilder builder = new DirectFieldBuilder(constantPool, context, field.fieldName(),
field.fieldType(), field); field.fieldType(), 0, field);
builder.transform(field, transform); builder.transform(field, transform);
return withField(builder); return withField(builder);
} }

View File

@ -44,12 +44,13 @@ public final class DirectFieldBuilder
ClassFileImpl context, ClassFileImpl context,
Utf8Entry name, Utf8Entry name,
Utf8Entry type, Utf8Entry type,
int flags,
FieldModel original) { FieldModel original) {
super(constantPool, context); super(constantPool, context);
setOriginal(original); setOriginal(original);
this.name = name; this.name = name;
this.desc = type; this.desc = type;
this.flags = 0; this.flags = flags;
} }
@Override @Override
@ -67,6 +68,12 @@ public final class DirectFieldBuilder
return this; return this;
} }
@Override
public FieldBuilder withFlags(int flags) {
setFlags(flags);
return this;
}
void setFlags(int flags) { void setFlags(int flags) {
this.flags = flags; this.flags = flags;
} }

View File

@ -61,6 +61,12 @@ public final class DirectMethodBuilder
this.flags = flags; this.flags = flags;
} }
@Override
public MethodBuilder withFlags(int flags) {
setFlags(flags);
return this;
}
void setFlags(int flags) { void setFlags(int flags) {
boolean wasStatic = (this.flags & ClassFile.ACC_STATIC) != 0; boolean wasStatic = (this.flags & ClassFile.ACC_STATIC) != 0;
boolean isStatic = (flags & ClassFile.ACC_STATIC) != 0; boolean isStatic = (flags & ClassFile.ACC_STATIC) != 0;