From 51c12c174bffab01312dbe6e2a8329027ec3e462 Mon Sep 17 00:00:00 2001 From: Arno Unkrig Date: Tue, 20 Feb 2018 17:03:01 +0100 Subject: [PATCH] Removed some unnecessary "(short)" type casts. --- .../codehaus/commons/compiler/Location.java | 2 +- .../java/org/codehaus/janino/CodeContext.java | 61 ++++++++++++------- .../java/org/codehaus/janino/Scanner.java | 26 ++++---- .../org/codehaus/janino/ScriptEvaluator.java | 2 +- .../org/codehaus/janino/UnitCompiler.java | 24 ++++---- .../org/codehaus/janino/tests/AstTest.java | 4 +- 6 files changed, 69 insertions(+), 50 deletions(-) diff --git a/commons-compiler/src/main/java/org/codehaus/commons/compiler/Location.java b/commons-compiler/src/main/java/org/codehaus/commons/compiler/Location.java index 7945a708e..ee5ddfdf2 100644 --- a/commons-compiler/src/main/java/org/codehaus/commons/compiler/Location.java +++ b/commons-compiler/src/main/java/org/codehaus/commons/compiler/Location.java @@ -40,7 +40,7 @@ class Location implements Serializable { /** * Representation of an unspecified location. */ - public static final Location NOWHERE = new Location("", (short) -1, (short) -1); + public static final Location NOWHERE = new Location("", -1, -1); @Nullable private final String optionalFileName; private final int lineNumber; diff --git a/janino/src/main/java/org/codehaus/janino/CodeContext.java b/janino/src/main/java/org/codehaus/janino/CodeContext.java index 0bbf03d86..66b5e0fc2 100644 --- a/janino/src/main/java/org/codehaus/janino/CodeContext.java +++ b/janino/src/main/java/org/codehaus/janino/CodeContext.java @@ -211,7 +211,7 @@ class CodeContext { short lineNumberTableAttributeNameIndex, short localVariableTableAttributeNameIndex ) throws IOException { - dos.writeShort((short) this.maxStack); // max_stack + dos.writeShort(this.maxStack); // max_stack dos.writeShort(this.maxLocals); // max_locals dos.writeInt(this.end.offset); // code_length dos.write(this.code, 0, this.end.offset); // code @@ -372,14 +372,24 @@ class CodeContext { } } + /** + * @param functionName + * @param code + * @param codeSize + * @param offset Where to start the analysis + * @param stackSize Stack size on start + * @param stackSizes Stack sizes at offsets within code; {@link #UNEXAMINED} value + * indicates that the stack size at a given offset has not yet been + * calculated + */ private void flowAnalysis( String functionName, - byte[] code, // Bytecode - int codeSize, // Size - int offset, // Current PC - int stackSize, // Stack size on entry - int[] stackSizes // Stack sizes in code + byte[] code, + int codeSize, + int offset, + int stackSize, + int[] stackSizes ) { for (;;) { CodeContext.LOGGER.entering( @@ -465,17 +475,13 @@ class CodeContext { case Opcode.SD_GETFIELD: --stackSize; case Opcode.SD_GETSTATIC: // SUPPRESS CHECKSTYLE FallThrough - stackSize += this.determineFieldSize((short) ( - CodeContext.extract16BitValue(0, operandOffset, code) - )); + stackSize += this.determineFieldSize(CodeContext.extract16BitValue(operandOffset, code)); break; case Opcode.SD_PUTFIELD: --stackSize; case Opcode.SD_PUTSTATIC: // SUPPRESS CHECKSTYLE FallThrough - stackSize -= this.determineFieldSize((short) ( - CodeContext.extract16BitValue(0, operandOffset, code) - )); + stackSize -= this.determineFieldSize(CodeContext.extract16BitValue(operandOffset, code)); break; case Opcode.SD_INVOKEVIRTUAL: @@ -483,9 +489,7 @@ class CodeContext { case Opcode.SD_INVOKEINTERFACE: --stackSize; case Opcode.SD_INVOKESTATIC: // SUPPRESS CHECKSTYLE FallThrough - stackSize -= this.determineArgumentsSize((short) ( - CodeContext.extract16BitValue(0, operandOffset, code) - )); + stackSize -= this.determineArgumentsSize(CodeContext.extract16BitValue(operandOffset, code)); break; case Opcode.SD_MULTIANEWARRAY: @@ -689,12 +693,27 @@ class CodeContext { } /** - * Extracts a 16 bit value at offset in code and adds bias to it. + * Extracts a 16 bit value at the given offset in the code. + */ + private static short + extract16BitValue(int offset, byte[] code) { + CodeContext.LOGGER.entering( + null, + "extract16BitValue", + new Object[] { offset, code[offset], code[offset + 1] } + ); + + short result = (short) (((code[offset]) << 8) + (code[offset + 1] & 0xff)); + + CodeContext.LOGGER.exiting(null, "extract16BitValue", result); + return result; + } + + /** + * Extracts a 16 bit value at the given offset in the code and adds a + * bias to it. * - * @param bias An int to skew the final result by (useful for calculating relative offsets) - * @param offset The position in the code array to extract the bytes from - * @param code The array of bytes - * @return An integer that treats the two bytes at position offset as an UNSIGNED SHORT + * @return An integer that treats the two bytes at position offset as an UNSIGNED SHORT */ private static int extract16BitValue(int bias, int offset, byte[] code) { @@ -780,7 +799,7 @@ class CodeContext { } /** - * Analyzes the descriptor of the Fieldref and return its size. + * Analyzes the descriptor of the Fieldref at index idx and return its size. */ private int determineFieldSize(short idx) { diff --git a/janino/src/main/java/org/codehaus/janino/Scanner.java b/janino/src/main/java/org/codehaus/janino/Scanner.java index ceb6c90cd..47a3dced6 100644 --- a/janino/src/main/java/org/codehaus/janino/Scanner.java +++ b/janino/src/main/java/org/codehaus/janino/Scanner.java @@ -109,10 +109,10 @@ class Scanner { public Scanner(@Nullable String optionalFileName, InputStream is) throws IOException { this( - optionalFileName, + optionalFileName, // optionalFileName new InputStreamReader(is), // in - (short) 1, // initialLineNumber - (short) 0 // initialColumnNumber + 1, // initialLineNumber + 0 // initialColumnNumber ); } @@ -137,8 +137,8 @@ class Scanner { ? new InputStreamReader(is) : new InputStreamReader(is, optionalEncoding) ), - (short) 1, // initialLineNumber - (short) 0 // initialColumnNumber + 1, // initialLineNumber + 0 // initialColumnNumber ); } @@ -159,8 +159,8 @@ class Scanner { this( optionalFileName, // optionalFileName in, // in - (short) 1, // initialLineNumber - (short) 0 // initialColumnNumber + 1, // initialLineNumber + 0 // initialColumnNumber ); } @@ -171,8 +171,8 @@ class Scanner { Scanner( @Nullable String optionalFileName, Reader in, - short initialLineNumber, // "1" is a good idea - short initialColumnNumber // "0" is a good idea + int initialLineNumber, // "1" is a good idea + int initialColumnNumber // "0" is a good idea ) throws IOException { // Debugging on source code level is only possible if the code comes from a "real" Java source file which the @@ -771,19 +771,19 @@ class Scanner { private int nextChar = -1; private int nextButOneChar = -1; private boolean crLfPending; - private short nextCharLineNumber; - private short nextCharColumnNumber; + private int nextCharLineNumber; + private int nextCharColumnNumber; /** * Line number of the previously produced token (typically starting at one). */ - private short tokenLineNumber; + private int tokenLineNumber; /** * Column number of the first character of the previously produced token (1 if token is immediately preceded by a * line break). */ - private short tokenColumnNumber; + private int tokenColumnNumber; private static final Set JAVA_KEYWORDS = new HashSet(Arrays.asList( diff --git a/janino/src/main/java/org/codehaus/janino/ScriptEvaluator.java b/janino/src/main/java/org/codehaus/janino/ScriptEvaluator.java index 4ee3da135..481007f06 100644 --- a/janino/src/main/java/org/codehaus/janino/ScriptEvaluator.java +++ b/janino/src/main/java/org/codehaus/janino/ScriptEvaluator.java @@ -991,7 +991,7 @@ class MethodWrapper { location, // location null, // optionalDocComment new Java.Modifiers( // modifiers - staticMethod ? (short) (Mod.PUBLIC | Mod.STATIC) : (short) Mod.PUBLIC, + staticMethod ? (short) (Mod.PUBLIC | Mod.STATIC) : Mod.PUBLIC, annotations ), null, // optionalTypeParameters diff --git a/janino/src/main/java/org/codehaus/janino/UnitCompiler.java b/janino/src/main/java/org/codehaus/janino/UnitCompiler.java index 1fe5170d5..ab177fefb 100644 --- a/janino/src/main/java/org/codehaus/janino/UnitCompiler.java +++ b/janino/src/main/java/org/codehaus/janino/UnitCompiler.java @@ -5370,12 +5370,12 @@ private enum SwitchKind { INT, ENUM, STRING } if (cv == UnitCompiler.NOT_CONSTANT) return UnitCompiler.NOT_CONSTANT; - if (cv instanceof Byte) return new Byte((byte) -((Byte) cv).byteValue()); - if (cv instanceof Short) return new Short((short) -((Short) cv).shortValue()); - if (cv instanceof Integer) return new Integer(-((Integer) cv).intValue()); - if (cv instanceof Long) return new Long(-((Long) cv).longValue()); - if (cv instanceof Float) return new Float(-((Float) cv).floatValue()); - if (cv instanceof Double) return new Double(-((Double) cv).doubleValue()); + if (cv instanceof Byte) return Byte .valueOf((byte) -((Byte) cv)); + if (cv instanceof Short) return Short .valueOf((short) -((Short) cv)); + if (cv instanceof Integer) return Integer.valueOf( -((Integer) cv)); + if (cv instanceof Long) return Long .valueOf( -((Long) cv)); + if (cv instanceof Float) return Float .valueOf( -((Float) cv)); + if (cv instanceof Double) return Double .valueOf( -((Double) cv)); return UnitCompiler.NOT_CONSTANT; } @@ -10327,7 +10327,7 @@ interface Compilable { void compile() throws CompileException; } } else if (iv >= Short.MIN_VALUE && iv <= Short.MAX_VALUE) { this.writeOpcode(locatable, Opcode.SIPUSH); - this.writeShort((short) iv); + this.writeShort(iv); } else { this.writeLdc(locatable, this.addConstantIntegerInfo(iv)); @@ -11856,7 +11856,7 @@ interface Compilable { void compile() throws CompileException; } if (v > Short.MAX_VALUE - Short.MIN_VALUE) { throw new InternalCompilerException("Short value out of legal range"); } - this.getCodeContext().write((short) -1, (byte) (v >> 8), (byte) v); + this.getCodeContext().write(-1, (byte) (v >> 8), (byte) v); } private void writeInt(int v) { @@ -11880,7 +11880,7 @@ interface Compilable { void compile() throws CompileException; } private void writeOffset(CodeContext.Offset src, final CodeContext.Offset dst) { - this.getCodeContext().writeOffset((short) -1, src, dst); + this.getCodeContext().writeOffset(-1, src, dst); } // Wrappers for "ClassFile.addConstant...Info()". Saves us some coding overhead. @@ -11907,7 +11907,7 @@ interface Compilable { void compile() throws CompileException; } } /* UNUSED private void writeConstantStringInfo(String value) { - this.codeContext.writeShort((short) -1, this.addConstantStringInfo(value)); + this.codeContext.writeShort(-1, this.addConstantStringInfo(value)); } */ private short @@ -11916,7 +11916,7 @@ private void writeConstantStringInfo(String value) { } /* UNUSED private void writeConstantIntegerInfo(int value) { - this.codeContext.writeShort((short) -1, this.addConstantIntegerInfo(value)); + this.codeContext.writeShort(-1, this.addConstantIntegerInfo(value)); } */ private short @@ -11925,7 +11925,7 @@ private void writeConstantIntegerInfo(int value) { } /* UNUSED private void writeConstantFloatInfo(float value) { - this.codeContext.writeShort((short) -1, this.addConstantFloatInfo(value)); + this.codeContext.writeShort(-1, this.addConstantFloatInfo(value)); } */ private short diff --git a/janino/src/test/java/org/codehaus/janino/tests/AstTest.java b/janino/src/test/java/org/codehaus/janino/tests/AstTest.java index a53e020db..e4b440f27 100644 --- a/janino/src/test/java/org/codehaus/janino/tests/AstTest.java +++ b/janino/src/test/java/org/codehaus/janino/tests/AstTest.java @@ -161,8 +161,8 @@ class AstTest { StackTraceElement ste = e.getStackTrace()[1]; //we only care about our caller return new Location( ste.getFileName(), - (short) ste.getLineNumber(), - (short) 0 + ste.getLineNumber(), + 0 ); }