Skip to content

Commit

Permalink
Removed some unnecessary "(short)" type casts.
Browse files Browse the repository at this point in the history
  • Loading branch information
aunkrig committed Feb 20, 2018
1 parent 7ae72c1 commit 51c12c1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 50 deletions.
Expand Up @@ -40,7 +40,7 @@ class Location implements Serializable {
/**
* Representation of an unspecified location.
*/
public static final Location NOWHERE = new Location("<internally generated location>", (short) -1, (short) -1);
public static final Location NOWHERE = new Location("<internally generated location>", -1, -1);

@Nullable private final String optionalFileName;
private final int lineNumber;
Expand Down
61 changes: 40 additions & 21 deletions janino/src/main/java/org/codehaus/janino/CodeContext.java
Expand Up @@ -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
Expand Down Expand Up @@ -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 <var>code</var>; {@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(
Expand Down Expand Up @@ -465,27 +475,21 @@ 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:
case Opcode.SD_INVOKESPECIAL:
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:
Expand Down Expand Up @@ -689,12 +693,27 @@ class CodeContext {
}

/**
* Extracts a 16 bit value at offset in code and adds <var>bias</var> to it.
* Extracts a 16 bit value at the given <var>offset</var> in the <var>code</var>.
*/
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 <var>offset</var> in the <var>code</var> and adds a
* <var>bias</var> 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) {
Expand Down Expand Up @@ -780,7 +799,7 @@ class CodeContext {
}

/**
* Analyzes the descriptor of the Fieldref and return its size.
* Analyzes the descriptor of the Fieldref at index <var>idx</var> and return its size.
*/
private int
determineFieldSize(short idx) {
Expand Down
26 changes: 13 additions & 13 deletions janino/src/main/java/org/codehaus/janino/Scanner.java
Expand Up @@ -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
);
}

Expand All @@ -137,8 +137,8 @@ class Scanner {
? new InputStreamReader(is)
: new InputStreamReader(is, optionalEncoding)
),
(short) 1, // initialLineNumber
(short) 0 // initialColumnNumber
1, // initialLineNumber
0 // initialColumnNumber
);
}

Expand All @@ -159,8 +159,8 @@ class Scanner {
this(
optionalFileName, // optionalFileName
in, // in
(short) 1, // initialLineNumber
(short) 0 // initialColumnNumber
1, // initialLineNumber
0 // initialColumnNumber
);
}

Expand All @@ -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
Expand Down Expand Up @@ -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<String> JAVA_KEYWORDS = new HashSet<String>(Arrays.asList(

Expand Down
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions janino/src/main/java/org/codehaus/janino/UnitCompiler.java
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions janino/src/test/java/org/codehaus/janino/tests/AstTest.java
Expand Up @@ -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
);
}

Expand Down

0 comments on commit 51c12c1

Please sign in to comment.