aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/javassist
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/javassist')
-rw-r--r--src/main/javassist/compiler/CodeGen.java24
-rw-r--r--src/main/javassist/compiler/JvstCodeGen.java37
2 files changed, 50 insertions, 11 deletions
diff --git a/src/main/javassist/compiler/CodeGen.java b/src/main/javassist/compiler/CodeGen.java
index 60170dbb..e0b2dfa4 100644
--- a/src/main/javassist/compiler/CodeGen.java
+++ b/src/main/javassist/compiler/CodeGen.java
@@ -192,6 +192,30 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId {
return sbuf.toString();
}
+ protected static int jvmTypeNameToExprType(char type) {
+ switch(type) {
+ case 'Z' :
+ return BOOLEAN;
+ case 'B' :
+ return BYTE;
+ case 'C' :
+ return CHAR;
+ case 'S' :
+ return SHORT;
+ case 'I' :
+ return INT;
+ case 'J' :
+ return LONG;
+ case 'F' :
+ return FLOAT;
+ case 'D' :
+ return DOUBLE;
+ case 'V' :
+ return VOID;
+ default :
+ return CLASS;
+ }
+ }
public void atASTList(ASTList n) throws CompileError { fatal(); }
diff --git a/src/main/javassist/compiler/JvstCodeGen.java b/src/main/javassist/compiler/JvstCodeGen.java
index 0090f851..40d39a11 100644
--- a/src/main/javassist/compiler/JvstCodeGen.java
+++ b/src/main/javassist/compiler/JvstCodeGen.java
@@ -184,10 +184,18 @@ public class JvstCodeGen extends MemberCodeGen {
*/
protected void atCastToRtype(CastExpr expr) throws CompileError {
expr.getOprand().accept(this);
- if (!isRefType(exprType) || arrayDim > 0)
- throw new CompileError("invalid type for " + returnCastName);
-
- compileUnwrapValue(returnType, bytecode);
+ if (exprType == VOID || isRefType(exprType) || arrayDim > 0)
+ compileUnwrapValue(returnType, bytecode);
+ else if (returnType instanceof CtPrimitiveType) {
+ CtPrimitiveType pt = (CtPrimitiveType)returnType;
+ int destType = jvmTypeNameToExprType(pt.getDescriptor());
+ atNumCastExpr(exprType, destType);
+ exprType = destType;
+ arrayDim = 0;
+ className = null;
+ }
+ else
+ throw new CompileError("invalid cast");
}
protected void atCastToWrapper(CastExpr expr) throws CompileError {
@@ -583,15 +591,22 @@ public class JvstCodeGen extends MemberCodeGen {
protected void compileUnwrapValue(CtClass type, Bytecode code)
throws CompileError
{
+ if (type == CtClass.voidType) {
+ addNullIfVoid();
+ return;
+ }
+
+ if (exprType == VOID)
+ throw new CompileError("invalid type for " + returnCastName);
+
if (type instanceof CtPrimitiveType) {
CtPrimitiveType pt = (CtPrimitiveType)type;
- if (pt != CtClass.voidType) {
- String wrapper = pt.getWrapperName();
- code.addCheckcast(wrapper);
- code.addInvokevirtual(wrapper, pt.getGetMethodName(),
- pt.getGetMethodDescriptor());
- setType(type);
- }
+ // pt is not voidType.
+ String wrapper = pt.getWrapperName();
+ code.addCheckcast(wrapper);
+ code.addInvokevirtual(wrapper, pt.getGetMethodName(),
+ pt.getGetMethodDescriptor());
+ setType(type);
}
else {
code.addCheckcast(type);