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 1 commit
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
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
2 changes: 1 addition & 1 deletion src/main/javassist/bytecode/analysis/Executor.java
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ private void evalNewObjectArray(int pos, CodeIterator iter, Frame frame) throws
if (opcode == MULTIANEWARRAY) {
dimensions = iter.byteAt(pos + 3);
} else {
name = name + "[]";
name += "[]";
dimensions = 1;
}

Expand Down
14 changes: 4 additions & 10 deletions src/main/javassist/bytecode/analysis/MultiArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,10 @@ public boolean isAssignableTo(Type type) {
return false;

if (typeDims < dims) {
if (eq(typeRoot.getCtClass(), Type.OBJECT.getCtClass()))
return true;

if (eq(typeRoot.getCtClass(), Type.CLONEABLE.getCtClass()))
return true;

if (eq(typeRoot.getCtClass(), Type.SERIALIZABLE.getCtClass()))
return true;

return false;
CtClass c = typeRoot.getCtClass();
return eq(c, Type.OBJECT.getCtClass())
|| eq(c, Type.CLONEABLE.getCtClass())
|| eq(c, Type.SERIALIZABLE.getCtClass());
Comment on lines +111 to +114
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoids calling getCtClass() multiple times (performance) and reduces the code size.

}

return component.isAssignableTo(typeRoot);
Expand Down
1 change: 0 additions & 1 deletion src/main/javassist/convert/TransformCallToStatic.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package javassist.convert;

import javassist.CtMethod;
import javassist.bytecode.BadBytecode;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import was unused

import javassist.bytecode.CodeIterator;
import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;
Expand Down
108 changes: 58 additions & 50 deletions src/main/javassist/expr/ExprEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,60 +190,68 @@ final boolean loopBody(CodeIterator iterator, CtClass clazz,
if (c < Opcode.GETSTATIC) // c < 178
/* skip */;
else if (c < Opcode.NEWARRAY) { // c < 188
if (c == Opcode.INVOKESTATIC
|| c == Opcode.INVOKEINTERFACE
|| c == Opcode.INVOKEVIRTUAL) {
expr = new MethodCall(pos, iterator, clazz, minfo);
edit((MethodCall)expr);
}
else if (c == Opcode.GETFIELD || c == Opcode.GETSTATIC
|| c == Opcode.PUTFIELD
|| c == Opcode.PUTSTATIC) {
expr = new FieldAccess(pos, iterator, clazz, minfo, c);
edit((FieldAccess)expr);
}
else if (c == Opcode.NEW) {
int index = iterator.u16bitAt(pos + 1);
context.newList = new NewOp(context.newList, pos,
minfo.getConstPool().getClassInfo(index));
}
else if (c == Opcode.INVOKESPECIAL) {
NewOp newList = context.newList;
if (newList != null
&& minfo.getConstPool().isConstructor(newList.type,
iterator.u16bitAt(pos + 1)) > 0) {
expr = new NewExpr(pos, iterator, clazz, minfo,
newList.type, newList.pos);
edit((NewExpr)expr);
context.newList = newList.next;
}
else {
MethodCall mcall = new MethodCall(pos, iterator, clazz, minfo);
if (mcall.getMethodName().equals(MethodInfo.nameInit)) {
ConstructorCall ccall = new ConstructorCall(pos, iterator, clazz, minfo);
expr = ccall;
edit(ccall);
switch (c) {
case Opcode.INVOKESTATIC:
case Opcode.INVOKEINTERFACE:
case Opcode.INVOKEVIRTUAL:
expr = new MethodCall(pos, iterator, clazz, minfo);
edit((MethodCall) expr);
break;
case Opcode.GETFIELD:
case Opcode.GETSTATIC:
case Opcode.PUTFIELD:
case Opcode.PUTSTATIC:
expr = new FieldAccess(pos, iterator, clazz, minfo, c);
edit((FieldAccess) expr);
break;
case Opcode.NEW:
int index = iterator.u16bitAt(pos + 1);
context.newList = new NewOp(context.newList, pos,
minfo.getConstPool().getClassInfo(index));
break;
case Opcode.INVOKESPECIAL:
NewOp newList = context.newList;
if (newList != null
&& minfo.getConstPool().isConstructor(newList.type,
iterator.u16bitAt(pos + 1)) > 0) {
expr = new NewExpr(pos, iterator, clazz, minfo,
newList.type, newList.pos);
edit((NewExpr) expr);
context.newList = newList.next;
} else {
MethodCall mcall = new MethodCall(pos, iterator, clazz, minfo);
if (mcall.getMethodName().equals(MethodInfo.nameInit)) {
ConstructorCall ccall = new ConstructorCall(pos, iterator, clazz, minfo);
expr = ccall;
edit(ccall);
} else {
expr = mcall;
edit(mcall);
}
}
else {
expr = mcall;
edit(mcall);
}
}
break;
default:
break;
}
}
else { // c >= 188
if (c == Opcode.NEWARRAY || c == Opcode.ANEWARRAY
|| c == Opcode.MULTIANEWARRAY) {
expr = new NewArray(pos, iterator, clazz, minfo, c);
edit((NewArray)expr);
}
else if (c == Opcode.INSTANCEOF) {
expr = new Instanceof(pos, iterator, clazz, minfo);
edit((Instanceof)expr);
}
else if (c == Opcode.CHECKCAST) {
expr = new Cast(pos, iterator, clazz, minfo);
edit((Cast)expr);
switch (c) {
case Opcode.NEWARRAY:
case Opcode.ANEWARRAY:
case Opcode.MULTIANEWARRAY:
expr = new NewArray(pos, iterator, clazz, minfo, c);
edit((NewArray)expr);
break;
case Opcode.INSTANCEOF:
expr = new Instanceof(pos, iterator, clazz, minfo);
edit((Instanceof)expr);
break;
case Opcode.CHECKCAST:
expr = new Cast(pos, iterator, clazz, minfo);
edit((Cast)expr);
break;
default:
break;
}
}

Expand Down
50 changes: 29 additions & 21 deletions src/main/javassist/expr/MethodCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,24 @@ public void replace(String statement) throws CannotCompileException {
String classname, methodname, signature;
int opcodeSize;
int c = iterator.byteAt(pos);
if (c == INVOKEINTERFACE) {
opcodeSize = 5;
classname = constPool.getInterfaceMethodrefClassName(index);
methodname = constPool.getInterfaceMethodrefName(index);
signature = constPool.getInterfaceMethodrefType(index);
switch (c) {
case INVOKEINTERFACE:
opcodeSize = 5;
classname = constPool.getInterfaceMethodrefClassName(index);
methodname = constPool.getInterfaceMethodrefName(index);
signature = constPool.getInterfaceMethodrefType(index);
break;
case INVOKESTATIC:
case INVOKESPECIAL:
case INVOKEVIRTUAL:
opcodeSize = 3;
classname = constPool.getMethodrefClassName(index);
methodname = constPool.getMethodrefName(index);
signature = constPool.getMethodrefType(index);
break;
default:
throw new CannotCompileException("not method invocation");
}
else if (c == INVOKESTATIC
|| c == INVOKESPECIAL || c == INVOKEVIRTUAL) {
opcodeSize = 3;
classname = constPool.getMethodrefClassName(index);
methodname = constPool.getMethodrefName(index);
signature = constPool.getMethodrefType(index);
}
else
throw new CannotCompileException("not method invocation");

Javac jc = new Javac(thisClass);
ClassPool cp = thisClass.getClassPool();
Expand All @@ -227,13 +230,18 @@ else if (c == INVOKESTATIC
jc.recordParams(classname, params,
true, paramVar, withinStatic());
int retVar = jc.recordReturnType(retType, true);
if (c == INVOKESTATIC)
jc.recordStaticProceed(classname, methodname);
else if (c == INVOKESPECIAL)
jc.recordSpecialProceed(Javac.param0Name, classname,
methodname, signature, index);
else
jc.recordProceed(Javac.param0Name, methodname);
switch (c) {
case INVOKESTATIC:
jc.recordStaticProceed(classname, methodname);
break;
case INVOKESPECIAL:
jc.recordSpecialProceed(Javac.param0Name, classname,
methodname, signature, index);
break;
default:
jc.recordProceed(Javac.param0Name, methodname);
break;
}

/* Is $_ included in the source code?
*/
Expand Down
Loading