Commit 66933e31 authored by Eric Bruneton's avatar Eric Bruneton

Improve the code quality of the Module[...]Attribute classes.

parent 3224adf0
Pipeline #1224 passed with stage
in 6 minutes and 17 seconds
......@@ -38,22 +38,27 @@ import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
/**
* ModuleHashes attribute. This attribute is specific to the OpenJDK and may change in the future.
* A ModuleHashes attribute. This attribute is specific to the OpenJDK and may change in the future.
*
* @author Remi Forax
*/
public final class ModuleHashesAttribute extends Attribute {
/** The name of the hashing algorithm. */
public String algorithm;
/** A list of module names. */
public List<String> modules;
/** The hash of the modules in {@link #modules}. The two lists must have the same size. */
public List<byte[]> hashes;
/**
* Constructs an attribute with a hashing algorithm, a list of module names, and a list of the same
* length of hashes.
* Constructs a new {@link ModuleHashesAttribute}.
*
* @param algorithm the hashing algorithm name.
* @param modules a list of module name
* @param hashes a list of hash, one for each module name.
* @param algorithm the name of the hashing algorithm.
* @param modules a list of module names.
* @param hashes the hash of the modules in 'modules'. The two lists must have the same size.
*/
public ModuleHashesAttribute(
final String algorithm, final List<String> modules, final List<byte[]> hashes) {
......@@ -64,8 +69,8 @@ public final class ModuleHashesAttribute extends Attribute {
}
/**
* Constructs an empty attribute that can be used as prototype to be passed as argument of the method
* {@link ClassReader#accept(org.objectweb.asm.ClassVisitor, Attribute[], int)}.
* Constructs an empty {@link ModuleHashesAttribute}. This object can be passed as a prototype to
* the {@link ClassReader#accept(org.objectweb.asm.ClassVisitor, Attribute[], int)} method.
*/
public ModuleHashesAttribute() {
this(null, null, null);
......@@ -73,50 +78,62 @@ public final class ModuleHashesAttribute extends Attribute {
@Override
protected Attribute read(
ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
String hashAlgorithm = cr.readUTF8(off, buf);
final ClassReader classReader,
final int offset,
final int length,
final char[] charBuffer,
final int codeAttributeOffset,
final Label[] labels) {
int currentOffset = offset;
int count = cr.readUnsignedShort(off + 2);
ArrayList<String> modules = new ArrayList<String>(count);
ArrayList<byte[]> hashes = new ArrayList<byte[]>(count);
off += 4;
String hashAlgorithm = classReader.readUTF8(currentOffset, charBuffer);
currentOffset += 2;
for (int i = 0; i < count; i++) {
String module = cr.readModule(off, buf);
int hashLength = cr.readUnsignedShort(off + 2);
off += 4;
int numModules = classReader.readUnsignedShort(currentOffset);
currentOffset += 2;
ArrayList<String> moduleList = new ArrayList<String>(numModules);
ArrayList<byte[]> hashList = new ArrayList<byte[]>(numModules);
for (int i = 0; i < numModules; ++i) {
String module = classReader.readModule(currentOffset, charBuffer);
currentOffset += 2;
moduleList.add(module);
int hashLength = classReader.readUnsignedShort(currentOffset);
currentOffset += 2;
byte[] hash = new byte[hashLength];
for (int j = 0; j < hashLength; j++) {
hash[j] = (byte) (cr.readByte(off + j) & 0xff);
for (int j = 0; j < hashLength; ++j) {
hash[j] = (byte) (classReader.readByte(currentOffset) & 0xFF);
currentOffset += 1;
}
off += hashLength;
modules.add(module);
hashes.add(hash);
hashList.add(hash);
}
return new ModuleHashesAttribute(hashAlgorithm, modules, hashes);
return new ModuleHashesAttribute(hashAlgorithm, moduleList, hashList);
}
@Override
protected ByteVector write(ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals) {
ByteVector v = new ByteVector();
int index = cw.newUTF8(algorithm);
v.putShort(index);
int count = (modules == null) ? 0 : modules.size();
v.putShort(count);
for (int i = 0; i < count; i++) {
String module = modules.get(i);
v.putShort(cw.newModule(module));
byte[] hash = hashes.get(i);
v.putShort(hash.length);
for (byte b : hash) {
v.putByte(b);
protected ByteVector write(
final ClassWriter classWriter,
final byte[] code,
final int codeLength,
final int maxStack,
final int maxLocals) {
ByteVector byteVector = new ByteVector();
byteVector.putShort(classWriter.newUTF8(algorithm));
if (modules == null) {
byteVector.putShort(0);
} else {
int numModules = modules.size();
byteVector.putShort(numModules);
for (int i = 0; i < numModules; ++i) {
byte[] hash = hashes.get(i);
byteVector
.putShort(classWriter.newModule(modules.get(i)))
.putShort(hash.length)
.putByteArray(hash, 0, hash.length);
}
}
return v;
return byteVector;
}
}
......@@ -35,39 +35,45 @@ import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
/**
* ModuleResolution_attribute. This attribute is specific to the OpenJDK and may change in the
* A ModuleResolution attribute. This attribute is specific to the OpenJDK and may change in the
* future.
*
* @author Remi Forax
*/
public final class ModuleResolutionAttribute extends Attribute {
/**
* Resolution state of a module meaning that the module is not available from the class-path by
* default.
* The resolution state of a module meaning that the module is not available from the class-path
* by default.
*/
public static final int RESOLUTION_DO_NOT_RESOLVE_BY_DEFAULT = 1;
/** Resolution state of a module meaning the module is marked as deprecated. */
/** The resolution state of a module meaning the module is marked as deprecated. */
public static final int RESOLUTION_WARN_DEPRECATED = 2;
/**
* Resolution state of a module meaning the module is marked as deprecated and will be removed in
* a future release.
* The resolution state of a module meaning the module is marked as deprecated and will be removed
* in a future release.
*/
public static final int RESOLUTION_WARN_DEPRECATED_FOR_REMOVAL = 4;
/**
* Resolution state of a module meaning the module is not yet standardized, so in incubating mode.
* The resolution state of a module meaning the module is not yet standardized, so in incubating
* mode.
*/
public static final int RESOLUTION_WARN_INCUBATING = 8;
/**
* The resolution state of the module. Must be one of {@link #RESOLUTION_WARN_DEPRECATED}, {@link
* #RESOLUTION_WARN_DEPRECATED_FOR_REMOVAL}, and {@link #RESOLUTION_WARN_INCUBATING}.
*/
public int resolution;
/**
* Constructs an attribute with a resolution state value.
* Constructs a new {@link ModuleResolutionAttribute}.
*
* @param resolution the resolution state among {@link #RESOLUTION_WARN_DEPRECATED}, {@link
* #RESOLUTION_WARN_DEPRECATED_FOR_REMOVAL}, and {@link #RESOLUTION_WARN_INCUBATING}.
* @param resolution the resolution state of the module. Must be one of {@link
* #RESOLUTION_WARN_DEPRECATED}, {@link #RESOLUTION_WARN_DEPRECATED_FOR_REMOVAL}, and {@link
* #RESOLUTION_WARN_INCUBATING}.
*/
public ModuleResolutionAttribute(final int resolution) {
super("ModuleResolution");
......@@ -75,8 +81,8 @@ public final class ModuleResolutionAttribute extends Attribute {
}
/**
* Constructs an empty attribute that can be used as prototype to be passed as argument of the method
* {@link ClassReader#accept(org.objectweb.asm.ClassVisitor, Attribute[], int)}.
* Constructs an empty {@link ModuleResolutionAttribute}. This object can be passed as a prototype
* to the {@link ClassReader#accept(org.objectweb.asm.ClassVisitor, Attribute[], int)} method.
*/
public ModuleResolutionAttribute() {
this(0);
......@@ -84,15 +90,24 @@ public final class ModuleResolutionAttribute extends Attribute {
@Override
protected Attribute read(
ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
int resolution = cr.readUnsignedShort(off);
return new ModuleResolutionAttribute(resolution);
final ClassReader classReader,
final int offset,
final int length,
final char[] charBuffer,
final int codeOffset,
final Label[] labels) {
return new ModuleResolutionAttribute(classReader.readUnsignedShort(offset));
}
@Override
protected ByteVector write(ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals) {
ByteVector v = new ByteVector();
v.putShort(resolution);
return v;
protected ByteVector write(
final ClassWriter classWriter,
final byte[] code,
final int codeLength,
final int maxStack,
final int maxLocals) {
ByteVector byteVector = new ByteVector();
byteVector.putShort(resolution);
return byteVector;
}
}
......@@ -35,17 +35,19 @@ import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
/**
* ModuleTarget attribute. This attribute is specific to the OpenJDK and may change in the future.
* A ModuleTarget attribute. This attribute is specific to the OpenJDK and may change in the future.
*
* @author Remi Forax
*/
public final class ModuleTargetAttribute extends Attribute {
/** The name of the platform on which the module can run. */
public String platform;
/**
* Constructs an attribute with a platform name.
* Constructs a new {@link ModuleTargetAttribute}.
*
* @param platform the platform name on which the module can run.
* @param platform the name of the platform on which the module can run.
*/
public ModuleTargetAttribute(final String platform) {
super("ModuleTarget");
......@@ -53,8 +55,8 @@ public final class ModuleTargetAttribute extends Attribute {
}
/**
* Constructs an empty attribute that can be used as prototype to be passed as argument of the method
* {@link ClassReader#accept(org.objectweb.asm.ClassVisitor, Attribute[], int)}.
* Constructs an empty {@link ModuleTargetAttribute}. This object can be passed as a prototype to
* the {@link ClassReader#accept(org.objectweb.asm.ClassVisitor, Attribute[], int)} method.
*/
public ModuleTargetAttribute() {
this(null);
......@@ -62,16 +64,24 @@ public final class ModuleTargetAttribute extends Attribute {
@Override
protected Attribute read(
ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
String platform = cr.readUTF8(off, buf);
return new ModuleTargetAttribute(platform);
final ClassReader classReader,
final int offset,
final int length,
final char[] charBuffer,
final int codeOffset,
final Label[] labels) {
return new ModuleTargetAttribute(classReader.readUTF8(offset, charBuffer));
}
@Override
protected ByteVector write(ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals) {
ByteVector v = new ByteVector();
int index = (platform == null) ? 0 : cw.newUTF8(platform);
v.putShort(index);
return v;
protected ByteVector write(
final ClassWriter classWriter,
final byte[] code,
final int codeLength,
final int maxStack,
final int maxLocals) {
ByteVector byteVector = new ByteVector();
byteVector.putShort(platform == null ? 0 : classWriter.newUTF8(platform));
return byteVector;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment