Browse Source

updates ConstPool to support Module and Package

tags/list
chibash 6 years ago
parent
commit
78223965e5

+ 1
- 0
.gitignore View File

@@ -4,6 +4,7 @@ target/
runtest/
html/
lib/
tmp/

.DS_Store
.classpath

BIN
javassist.jar View File


+ 3
- 2
src/main/javassist/bytecode/AccessFlag.java View File

@@ -41,12 +41,13 @@ public class AccessFlag {
public static final int MANDATED = 0x8000;

public static final int SUPER = 0x0020;
public static final int MODULE = 0x8000;

// Note: 0x0020 is assigned to both ACC_SUPER and ACC_SYNCHRONIZED
// although java.lang.reflect.Modifier does not recognize ACC_SUPER.

/**
* Truns the public bit on. The protected and private bits are
* Turns the public bit on. The protected and private bits are
* cleared.
*/
public static int setPublic(int accflags) {
@@ -54,7 +55,7 @@ public class AccessFlag {
}

/**
* Truns the protected bit on. The protected and public bits are
* Turns the protected bit on. The protected and public bits are
* cleared.
*/
public static int setProtected(int accflags) {

+ 147
- 0
src/main/javassist/bytecode/ConstPool.java View File

@@ -108,6 +108,16 @@ public final class ConstPool {
*/
public static final int CONST_InvokeDynamic = InvokeDynamicInfo.tag;

/**
* <code>CONSTANT_Module</code>
*/
public static final int CONST_Module = ModuleInfo.tag;

/**
* <code>CONSTANT_Package</code>
*/
public static final int CONST_Package = PackageInfo.tag;

/**
* Represents the class using this constant pool table.
*/
@@ -737,6 +747,31 @@ public final class ConstPool {
}
}

/**
* Reads the <code>name_index</code> field of the
* <code>CONSTANT_Module_info</code> structure at the given index.
*
* @return the module name at <code>name_index</code>.
* @since 3.22
*/
public String getModuleInfo(int index) {
ModuleInfo mi = (ModuleInfo)getItem(index);
return getUtf8Info(mi.name);
}

/**
* Reads the <code>name_index</code> field of the
* <code>CONSTANT_Package_info</code> structure at the given index.
*
* @return the package name at <code>name_index</code>. It is a slash-separated name
* such as com/oracle/net.
* @since 3.22
*/
public String getPackageInfo(int index) {
PackageInfo mi = (PackageInfo)getItem(index);
return getUtf8Info(mi.name);
}

/**
* Determines whether <code>CONSTANT_Methodref_info</code>
* structure at the given index represents the constructor
@@ -1117,6 +1152,26 @@ public final class ConstPool {
return addItem(new InvokeDynamicInfo(bootstrap, nameAndType, numOfItems));
}

/**
* Adds a new <code>CONSTANT_Module_info</code>
* @param nameIndex the index of the Utf8 entry.
* @return the index of the added entry.
* @since 3.22
*/
public int addModuleInfo(int nameIndex) {
return addItem(new ModuleInfo(nameIndex, numOfItems));
}

/**
* Adds a new <code>CONSTANT_Package_info</code>
* @param nameIndex the index of the Utf8 entry.
* @return the index of the added entry.
* @since 3.22
*/
public int addPackageInfo(int nameIndex) {
return addItem(new PackageInfo(nameIndex, numOfItems));
}

/**
* Get all the class names.
*
@@ -1240,6 +1295,12 @@ public final class ConstPool {
case InvokeDynamicInfo.tag : // 18
info = new InvokeDynamicInfo(in, numOfItems);
break;
case ModuleInfo.tag : // 19
info = new ModuleInfo(in, numOfItems);
break;
case PackageInfo.tag : // 20
info = new PackageInfo(in, numOfItems);
break;
default :
throw new IOException("invalid constant type: " + tag + " at " + numOfItems);
}
@@ -1996,3 +2057,89 @@ class InvokeDynamicInfo extends ConstInfo {
out.println(nameAndType);
}
}

class ModuleInfo extends ConstInfo {
static final int tag = 19;
int name;

public ModuleInfo(int moduleName, int index) {
super(index);
name = moduleName;
}

public ModuleInfo(DataInputStream in, int index) throws IOException {
super(index);
name = in.readUnsignedShort();
}

public int hashCode() { return name; }

public boolean equals(Object obj) {
return obj instanceof ModuleInfo && ((ModuleInfo)obj).name == name;
}

public int getTag() { return tag; }

public String getModuleName(ConstPool cp) {
return cp.getUtf8Info(name);
}

public int copy(ConstPool src, ConstPool dest, Map map) {
String moduleName = src.getUtf8Info(name);
int newName = dest.addUtf8Info(moduleName);
return dest.addModuleInfo(newName);
}

public void write(DataOutputStream out) throws IOException {
out.writeByte(tag);
out.writeShort(name);
}

public void print(PrintWriter out) {
out.print("Module #");
out.println(name);
}
}

class PackageInfo extends ConstInfo {
static final int tag = 20;
int name;

public PackageInfo(int moduleName, int index) {
super(index);
name = moduleName;
}

public PackageInfo(DataInputStream in, int index) throws IOException {
super(index);
name = in.readUnsignedShort();
}

public int hashCode() { return name; }

public boolean equals(Object obj) {
return obj instanceof PackageInfo && ((PackageInfo)obj).name == name;
}

public int getTag() { return tag; }

public String getPackageName(ConstPool cp) {
return cp.getUtf8Info(name);
}

public int copy(ConstPool src, ConstPool dest, Map map) {
String packageName = src.getUtf8Info(name);
int newName = dest.addUtf8Info(packageName);
return dest.addModuleInfo(newName);
}

public void write(DataOutputStream out) throws IOException {
out.writeByte(tag);
out.writeShort(name);
}

public void print(PrintWriter out) {
out.print("Package #");
out.println(name);
}
}

Loading…
Cancel
Save