Skip to content

Commit

Permalink
Simplify code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ansalond committed May 12, 2021
1 parent e91ca09 commit ded768e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,23 +393,17 @@ private static void addFactoryMethods(ClassVisitor cv, Class<?> storageClass, Cl
}
}

private static Collection<StaticProperty> generateStorageProperties() {
return Arrays.asList(
new DefaultStaticProperty("primitive", StaticPropertyKind.BYTE_ARRAY, true),
new DefaultStaticProperty("object", StaticPropertyKind.OBJECT_ARRAY, true),
new DefaultStaticProperty("shape", StaticPropertyKind.Object, true));
}

private static Class<?> generateStorage(Class<?> storageSuperClass) {
String storageSuperName = Type.getInternalName(storageSuperClass);
String storageName = generateStorageName();
Collection<StaticProperty> arrayProperties = generateStorageProperties();

ClassWriter storageWriter = new ClassWriter(0);
int storageAccess = ACC_PUBLIC | ACC_SUPER | ACC_SYNTHETIC;
storageWriter.visit(V1_8, storageAccess, storageName, null, storageSuperName, null);
addStorageConstructors(storageWriter, storageName, storageSuperClass, storageSuperName);
addStorageFields(storageWriter, arrayProperties);
addStorageField(storageWriter, "primitive", StaticPropertyKind.BYTE_ARRAY, true);
addStorageField(storageWriter, "object", StaticPropertyKind.OBJECT_ARRAY, true);
addStorageField(storageWriter, "shape", StaticPropertyKind.Object.toByte(), true);
if (Cloneable.class.isAssignableFrom(storageSuperClass)) {
addCloneMethod(storageSuperClass, storageWriter, storageName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ public final class DefaultStaticProperty extends StaticProperty {
private final String name;
private final boolean isFinal;

DefaultStaticProperty(String name, byte internalKind, boolean isFinal) {
super(internalKind);
public DefaultStaticProperty(String name, StaticPropertyKind kind, boolean isFinal) {
super(kind);
this.name = name;
this.isFinal = isFinal;
}

public DefaultStaticProperty(String name, StaticPropertyKind kind, boolean isFinal) {
this(name, kind.toByte(), isFinal);
}

@Override
public String getName() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,16 @@ static String generateFactoryName(Class<?> generatedStorageClass) {

static void addStorageFields(ClassVisitor cv, Collection<StaticProperty> staticProperties) {
for (StaticProperty staticProperty : staticProperties) {
int access = ACC_PUBLIC;
if (staticProperty.isFinal()) {
access |= ACC_FINAL;
}
FieldVisitor fv = cv.visitField(access, staticProperty.getName(), StaticPropertyKind.getDescriptor(staticProperty.getInternalKind()), null, null);
fv.visitEnd();
addStorageField(cv, staticProperty.getName(), staticProperty.getInternalKind(), staticProperty.isFinal());
}
}

static void addStorageField(ClassVisitor cv, String propertyName, byte internalKind, boolean isFinal) {
int access = isFinal ? ACC_FINAL | ACC_PUBLIC : ACC_PUBLIC;
FieldVisitor fv = cv.visitField(access, propertyName, StaticPropertyKind.getDescriptor(internalKind), null, null);
fv.visitEnd();
}

@SuppressWarnings("unchecked")
static <T> Class<? extends T> load(String name, byte[] bytes, Class<T> referenceClass) {
Object clazz;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ public abstract class StaticProperty {
@CompilationFinal //
private int offset;

StaticProperty(byte internalKind) {
this.internalKind = internalKind;
}

protected StaticProperty(StaticPropertyKind kind) {
this(getInternalKind(kind));
this.internalKind = getInternalKind(kind);
}

public abstract String getName();
Expand Down

0 comments on commit ded768e

Please sign in to comment.