Polishing

(cherry picked from commit 40efcc9)
This commit is contained in:
Juergen Hoeller
2018-06-28 14:51:31 +02:00
parent 3e64388b20
commit a631af80c1
113 changed files with 648 additions and 693 deletions

View File

@@ -530,24 +530,27 @@ public class CodeFlow implements Opcodes {
}
/**
* Returns if the descriptor is for a boolean primitive or boolean reference type.
* @param descriptor type descriptor
* @return {@code true} if the descriptor is for a boolean primitive or boolean reference type
* @return {@code true} if the descriptor is boolean compatible
*/
public static boolean isBooleanCompatible(@Nullable String descriptor) {
return (descriptor != null && (descriptor.equals("Z") || descriptor.equals("Ljava/lang/Boolean")));
}
/**
* Returns if the descriptor is for a primitive type.
* @param descriptor type descriptor
* @return {@code true} if the descriptor is for a primitive type
* @return {@code true} if a primitive type
*/
public static boolean isPrimitive(@Nullable String descriptor) {
return (descriptor != null && descriptor.length() == 1);
}
/**
* Returns if the descriptor is for a primitive array (e.g. "[[I").
* @param descriptor the descriptor for a possible primitive array
* @return {@code true} if the descriptor is for a primitive array (e.g. "[[I")
* @return {@code true} if the descriptor a primitive array
*/
public static boolean isPrimitiveArray(@Nullable String descriptor) {
if (descriptor == null) {
@@ -662,6 +665,7 @@ public class CodeFlow implements Opcodes {
}
/**
* Convert a type descriptor to the single character primitive descriptor.
* @param descriptor a descriptor for a type that should have a primitive representation
* @return the single character descriptor for a primitive input descriptor
*/
@@ -903,16 +907,33 @@ public class CodeFlow implements Opcodes {
public static void insertArrayStore(MethodVisitor mv, String arrayElementType) {
if (arrayElementType.length()==1) {
switch (arrayElementType.charAt(0)) {
case 'I': mv.visitInsn(IASTORE); break;
case 'J': mv.visitInsn(LASTORE); break;
case 'F': mv.visitInsn(FASTORE); break;
case 'D': mv.visitInsn(DASTORE); break;
case 'B': mv.visitInsn(BASTORE); break;
case 'C': mv.visitInsn(CASTORE); break;
case 'S': mv.visitInsn(SASTORE); break;
case 'Z': mv.visitInsn(BASTORE); break;
case 'I':
mv.visitInsn(IASTORE);
break;
case 'J':
mv.visitInsn(LASTORE);
break;
case 'F':
mv.visitInsn(FASTORE);
break;
case 'D':
mv.visitInsn(DASTORE);
break;
case 'B':
mv.visitInsn(BASTORE);
break;
case 'C':
mv.visitInsn(CASTORE);
break;
case 'S':
mv.visitInsn(SASTORE);
break;
case 'Z':
mv.visitInsn(BASTORE);
break;
default:
throw new IllegalArgumentException("Unexpected arraytype "+arrayElementType.charAt(0));
throw new IllegalArgumentException(
"Unexpected arraytype " + arrayElementType.charAt(0));
}
}
else {
@@ -941,13 +962,15 @@ public class CodeFlow implements Opcodes {
}
/**
* @return true if the supplied array type has a core component reference type
* Return if the supplied array type has a core component reference type.
*/
public static boolean isReferenceTypeArray(String arraytype) {
int length = arraytype.length();
for (int i = 0; i < length; i++) {
char ch = arraytype.charAt(i);
if (ch == '[') continue;
if (ch == '[') {
continue;
}
return ch=='L';
}
return false;
@@ -1003,21 +1026,7 @@ public class CodeFlow implements Opcodes {
}
}
@FunctionalInterface
public interface FieldAdder {
void generateField(ClassWriter cw, CodeFlow codeflow);
}
@FunctionalInterface
public interface ClinitAdder {
void generateCode(MethodVisitor mv, CodeFlow codeflow);
}
public static String toBoxedDescriptor(String primitiveDescriptor) {
public static final String toBoxedDescriptor(String primitiveDescriptor) {
switch (primitiveDescriptor.charAt(0)) {
case 'I': return "Ljava/lang/Integer";
case 'J': return "Ljava/lang/Long";
@@ -1029,7 +1038,27 @@ public class CodeFlow implements Opcodes {
case 'Z': return "Ljava/lang/Boolean";
default:
throw new IllegalArgumentException("Unexpected non primitive descriptor "+primitiveDescriptor);
}
}
}
/**
* Interface used to generate fields.
*/
@FunctionalInterface
public interface FieldAdder {
void generateField(ClassWriter cw, CodeFlow codeflow);
}
/**
* Interface used to generate {@code clinit} static initializer blocks.
*/
@FunctionalInterface
public interface ClinitAdder {
void generateCode(MethodVisitor mv, CodeFlow codeflow);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -205,10 +205,10 @@ public class OpPlus extends Operator {
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
}
}
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
if (this.exitTypeDescriptor == "Ljava/lang/String") {
if ("Ljava/lang/String".equals(this.exitTypeDescriptor)) {
mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
@@ -236,12 +236,12 @@ public class OpPlus extends Operator {
case 'J':
mv.visitInsn(LADD);
break;
case 'F':
case 'F':
mv.visitInsn(FADD);
break;
case 'D':
mv.visitInsn(DADD);
break;
break;
default:
throw new IllegalStateException(
"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");