Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert chain of ifs to switch, add overrides #384

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/javassist/ByteArrayClassPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public URL find(String classname) {
}

private class BytecodeURLStreamHandler extends URLStreamHandler {
@Override
protected URLConnection openConnection(final URL u) {
return new BytecodeURLConnection(u);
}
Expand All @@ -108,13 +109,16 @@ protected BytecodeURLConnection(URL url) {
super(url);
}

@Override
public void connect() throws IOException {
}

@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(classfile);
}

@Override
public int getContentLength() {
return classfile.length;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/javassist/ClassPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ protected CtClass removeCached(String classname) {
/**
* Returns the class search path.
*/
@Override
public String toString() {
return source.toString();
}
Expand Down
41 changes: 22 additions & 19 deletions src/main/javassist/CtBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -1049,25 +1049,28 @@ private int insertAfterHandler(boolean asFinally, Bytecode b,
b.addAstore(var); // store an exception
if (rtype.isPrimitive()) {
char c = ((CtPrimitiveType)rtype).getDescriptor();
if (c == 'D') {
b.addDconst(0.0);
b.addDstore(returnVarNo);
}
else if (c == 'F') {
b.addFconst(0);
b.addFstore(returnVarNo);
}
else if (c == 'J') {
b.addLconst(0);
b.addLstore(returnVarNo);
}
else if (c == 'V') {
b.addOpcode(Opcode.ACONST_NULL);
b.addAstore(returnVarNo);
}
else { // int, boolean, char, short, ...
b.addIconst(0);
b.addIstore(returnVarNo);
switch (c) {
case 'D':
b.addDconst(0.0);
b.addDstore(returnVarNo);
break;
case 'F':
b.addFconst(0);
b.addFstore(returnVarNo);
break;
case 'J':
b.addLconst(0);
b.addLstore(returnVarNo);
break;
case 'V':
b.addOpcode(Opcode.ACONST_NULL);
b.addAstore(returnVarNo);
break;
default:
// int, boolean, char, short, ...
b.addIconst(0);
b.addIstore(returnVarNo);
break;
}
}
else {
Expand Down
4 changes: 2 additions & 2 deletions src/main/javassist/SerialVersionUID.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public static long calculateDefault(CtClass clazz)
int classMods = clazz.getModifiers();
if ((classMods & Modifier.INTERFACE) != 0)
if (methods.length > 0)
classMods = classMods | Modifier.ABSTRACT;
classMods |= Modifier.ABSTRACT;
else
classMods = classMods & ~Modifier.ABSTRACT;
classMods &= ~Modifier.ABSTRACT;

out.writeInt(classMods);

Expand Down
45 changes: 24 additions & 21 deletions src/main/javassist/bytecode/AnnotationsAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,27 +417,30 @@ int memberValuePair(int pos, int nameIndex) throws Exception {
*/
final int memberValue(int pos) throws Exception {
int tag = info[pos] & 0xff;
if (tag == 'e') {
int typeNameIndex = ByteArray.readU16bit(info, pos + 1);
int constNameIndex = ByteArray.readU16bit(info, pos + 3);
enumMemberValue(pos, typeNameIndex, constNameIndex);
return pos + 5;
}
else if (tag == 'c') {
int index = ByteArray.readU16bit(info, pos + 1);
classMemberValue(pos, index);
return pos + 3;
}
else if (tag == '@')
return annotationMemberValue(pos + 1);
else if (tag == '[') {
int num = ByteArray.readU16bit(info, pos + 1);
return arrayMemberValue(pos + 3, num);
}
else { // primitive types or String.
int index = ByteArray.readU16bit(info, pos + 1);
constValueMember(tag, index);
return pos + 3;
switch (tag) {
case 'e': {
int typeNameIndex = ByteArray.readU16bit(info, pos + 1);
int constNameIndex = ByteArray.readU16bit(info, pos + 3);
enumMemberValue(pos, typeNameIndex, constNameIndex);
return pos + 5;
}
case 'c': {
int index = ByteArray.readU16bit(info, pos + 1);
classMemberValue(pos, index);
return pos + 3;
}
case '@': {
return annotationMemberValue(pos + 1);
}
case '[': {
int num = ByteArray.readU16bit(info, pos + 1);
return arrayMemberValue(pos + 3, num);
}
default: { // primitive types or String.
int index = ByteArray.readU16bit(info, pos + 1);
constValueMember(tag, index);
return pos + 3;
}
}
}

Expand Down
18 changes: 11 additions & 7 deletions src/main/javassist/bytecode/ClassFilePrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,17 @@ else if (ai instanceof SignatureAttribute) {
out.println("signature: " + sig);
try {
String s;
if (kind == 'c')
s = SignatureAttribute.toClassSignature(sig).toString();
else if (kind == 'm')
s = SignatureAttribute.toMethodSignature(sig).toString();
else
s = SignatureAttribute.toFieldSignature(sig).toString();

switch (kind) {
case 'c':
s = SignatureAttribute.toClassSignature(sig).toString();
break;
case 'm':
s = SignatureAttribute.toMethodSignature(sig).toString();
break;
default:
s = SignatureAttribute.toFieldSignature(sig).toString();
break;
}
out.println(" " + s);
}
catch (BadBytecode e) {
Expand Down
59 changes: 35 additions & 24 deletions src/main/javassist/bytecode/Descriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,42 @@ public static String toClassName(String descriptor) {
}

String name;
if (c == 'L') {
int i2 = descriptor.indexOf(';', i++);
name = descriptor.substring(i, i2).replace('/', '.');
i = i2;
switch (c) {
case 'L':
int i2 = descriptor.indexOf(';', i++);
name = descriptor.substring(i, i2).replace('/', '.');
i = i2;
break;
case 'V':
name = "void";
break;
case 'I':
name = "int";
break;
case 'B':
name = "byte";
break;
case 'J':
name = "long";
break;
case 'D':
name = "double";
break;
case 'F':
name = "float";
break;
case 'C':
name = "char";
break;
case 'S':
name = "short";
break;
case 'Z':
name = "boolean";
break;
default:
throw new RuntimeException("bad descriptor: " + descriptor);
}
else if (c == 'V')
name = "void";
else if (c == 'I')
name = "int";
else if (c == 'B')
name = "byte";
else if (c == 'J')
name = "long";
else if (c == 'D')
name = "double";
else if (c == 'F')
name = "float";
else if (c == 'C')
name = "char";
else if (c == 'S')
name = "short";
else if (c == 'Z')
name = "boolean";
else
throw new RuntimeException("bad descriptor: " + descriptor);

if (i + 1 != descriptor.length())
throw new RuntimeException("multiple descriptors?: " + descriptor);
Expand Down
20 changes: 10 additions & 10 deletions src/main/javassist/bytecode/SignatureAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -558,16 +558,16 @@ public static TypeArgument superOf(ObjectType t) {
*/
@Override
public String toString() {
if (wildcard == '*')
return "?";

String type = arg.toString();
if (wildcard == ' ')
return type;
else if (wildcard == '+')
return "? extends " + type;
else
return "? super " + type;
switch (this.wildcard) {
case '*':
return "?";
case ' ':
return arg.toString();
case '+':
return "? extends " + arg.toString();
default:
return "? super " + arg.toString();
}
}

static void encode(StringBuffer sb, TypeArgument[] args) {
Expand Down
102 changes: 56 additions & 46 deletions src/main/javassist/bytecode/StackMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,22 @@ public int typeInfoArray(int pos, int offset, int num, boolean isLocals) {

int typeInfoArray2(int k, int pos) {
byte tag = info[pos];
if (tag == OBJECT) {
int clazz = ByteArray.readU16bit(info, pos + 1);
objectVariable(pos, clazz);
pos += 3;
}
else if (tag == UNINIT) {
int offsetOfNew = ByteArray.readU16bit(info, pos + 1);
uninitialized(pos, offsetOfNew);
pos += 3;
}
else {
typeInfo(pos, tag);
pos++;
switch (tag) {
case OBJECT:
int clazz = ByteArray.readU16bit(info, pos + 1);
objectVariable(pos, clazz);
pos += 3;
break;
case UNINIT:
int offsetOfNew = ByteArray.readU16bit(info, pos + 1);
uninitialized(pos, offsetOfNew);
pos += 3;
break;
default:
typeInfo(pos, tag);
pos++;
break;
}

return pos;
}

Expand Down Expand Up @@ -374,12 +375,17 @@ public int typeInfoArray(int pos, int offset, int num, boolean isLocals) {
}

private void writeVarTypeInfo() {
if (varTag == OBJECT)
writer.writeVerifyTypeInfo(OBJECT, varData);
else if (varTag == UNINIT)
writer.writeVerifyTypeInfo(UNINIT, varData);
else
writer.writeVerifyTypeInfo(varTag, 0);
switch (varTag) {
case OBJECT:
writer.writeVerifyTypeInfo(OBJECT, varData);
break;
case UNINIT:
writer.writeVerifyTypeInfo(UNINIT, varData);
break;
default:
writer.writeVerifyTypeInfo(varTag, 0);
break;
}
}
}

Expand Down Expand Up @@ -473,38 +479,42 @@ private int stackTypeInfoArray(int pos, int offset, int num) {
int p = pos;
int count = 0;
for (int k = 0; k < num; k++) {
byte tag = info[p];
if (tag == OBJECT)
p += 3;
else if (tag == UNINIT) {
int offsetOfNew = ByteArray.readU16bit(info, p + 1);
if (offsetOfNew == posOfNew)
count++;

p += 3;
switch (info[p]) {
case OBJECT:
p += 3;
break;
case UNINIT:
if (ByteArray.readU16bit(info, p + 1) == posOfNew) {
count++;
}
p += 3;
break;
default:
p++;
break;
}
else
p++;
}

writer.write16bit(num - count);
for (int k = 0; k < num; k++) {
byte tag = info[pos];
if (tag == OBJECT) {
int clazz = ByteArray.readU16bit(info, pos + 1);
objectVariable(pos, clazz);
pos += 3;
}
else if (tag == UNINIT) {
int offsetOfNew = ByteArray.readU16bit(info, pos + 1);
if (offsetOfNew != posOfNew)
uninitialized(pos, offsetOfNew);

pos += 3;
}
else {
typeInfo(pos, tag);
pos++;
switch (tag) {
case OBJECT:
int clazz = ByteArray.readU16bit(info, pos + 1);
objectVariable(pos, clazz);
pos += 3;
break;
case UNINIT:
int offsetOfNew = ByteArray.readU16bit(info, pos + 1);
if (offsetOfNew != posOfNew) {
uninitialized(pos, offsetOfNew);
}
pos += 3;
break;
default:
typeInfo(pos, tag);
pos++;
break;
}
}

Expand Down
Loading