Commit 377621a2 authored by Eric Bruneton's avatar Eric Bruneton

Check code quality with checkstyle

parent a3ac07fc
......@@ -447,12 +447,12 @@ public class Analyzer<V extends Value> implements Opcodes {
/**
* Constructs a new frame with the given size.
*
* @param nLocals the maximum number of local variables of the frame.
* @param nStack the maximum stack size of the frame.
* @param numLocals the maximum number of local variables of the frame.
* @param numStack the maximum stack size of the frame.
* @return the created frame.
*/
protected Frame<V> newFrame(final int nLocals, final int nStack) {
return new Frame<V>(nLocals, nStack);
protected Frame<V> newFrame(final int numLocals, final int numStack) {
return new Frame<V>(numLocals, numStack);
}
/**
......
......@@ -54,28 +54,28 @@ public class Frame<V extends Value> {
private V returnValue;
/**
* The local variables and the operand stack of this frame. The first {@link #nLocals} elements
* correspond to the local variables. The following {@link #nStack} elements correspond to the
* The local variables and the operand stack of this frame. The first {@link #numLocals} elements
* correspond to the local variables. The following {@link #numStack} elements correspond to the
* operand stack.
*/
private V[] values;
/** The number of local variables of this frame. */
private int nLocals;
private int numLocals;
/** The number of elements in the operand stack. */
private int nStack;
private int numStack;
/**
* Constructs a new frame with the given size.
*
* @param nLocals the maximum number of local variables of the frame.
* @param nStack the maximum stack size of the frame.
* @param numLocals the maximum number of local variables of the frame.
* @param numStack the maximum stack size of the frame.
*/
@SuppressWarnings("unchecked")
public Frame(final int nLocals, final int nStack) {
this.values = (V[]) new Value[nLocals + nStack];
this.nLocals = nLocals;
public Frame(final int numLocals, final int numStack) {
this.values = (V[]) new Value[numLocals + numStack];
this.numLocals = numLocals;
}
/**
......@@ -84,7 +84,7 @@ public class Frame<V extends Value> {
* @param frame a frame.
*/
public Frame(final Frame<? extends V> frame) {
this(frame.nLocals, frame.values.length - frame.nLocals);
this(frame.numLocals, frame.values.length - frame.numLocals);
init(frame);
}
......@@ -97,7 +97,7 @@ public class Frame<V extends Value> {
public Frame<V> init(final Frame<? extends V> frame) {
returnValue = frame.returnValue;
System.arraycopy(frame.values, 0, values, 0, values.length);
nStack = frame.nStack;
numStack = frame.numStack;
return this;
}
......@@ -117,7 +117,7 @@ public class Frame<V extends Value> {
* @return the maximum number of local variables of this frame.
*/
public int getLocals() {
return nLocals;
return numLocals;
}
/**
......@@ -126,7 +126,7 @@ public class Frame<V extends Value> {
* @return the maximum stack size of this frame.
*/
public int getMaxStackSize() {
return values.length - nLocals;
return values.length - numLocals;
}
/**
......@@ -137,7 +137,7 @@ public class Frame<V extends Value> {
* @throws IndexOutOfBoundsException if the variable does not exist.
*/
public V getLocal(final int index) {
if (index >= nLocals) {
if (index >= numLocals) {
throw new IndexOutOfBoundsException("Trying to access an inexistant local variable");
}
return values[index];
......@@ -151,7 +151,7 @@ public class Frame<V extends Value> {
* @throws IndexOutOfBoundsException if the variable does not exist.
*/
public void setLocal(final int index, final V value) {
if (index >= nLocals) {
if (index >= numLocals) {
throw new IndexOutOfBoundsException("Trying to access an inexistant local variable " + index);
}
values[index] = value;
......@@ -164,7 +164,7 @@ public class Frame<V extends Value> {
* @return the number of values in the operand stack of this frame.
*/
public int getStackSize() {
return nStack;
return numStack;
}
/**
......@@ -175,7 +175,7 @@ public class Frame<V extends Value> {
* @throws IndexOutOfBoundsException if the operand stack slot does not exist.
*/
public V getStack(final int index) {
return values[nLocals + index];
return values[numLocals + index];
}
/**
......@@ -186,12 +186,12 @@ public class Frame<V extends Value> {
* @throws IndexOutOfBoundsException if the stack slot does not exist.
*/
public void setStack(final int index, final V value) throws IndexOutOfBoundsException {
values[nLocals + index] = value;
values[numLocals + index] = value;
}
/** Clears the operand stack of this frame. */
public void clearStack() {
nStack = 0;
numStack = 0;
}
/**
......@@ -201,10 +201,10 @@ public class Frame<V extends Value> {
* @throws IndexOutOfBoundsException if the operand stack is empty.
*/
public V pop() {
if (nStack == 0) {
if (numStack == 0) {
throw new IndexOutOfBoundsException("Cannot pop operand off an empty stack.");
}
return values[nLocals + (--nStack)];
return values[numLocals + (--numStack)];
}
/**
......@@ -214,10 +214,10 @@ public class Frame<V extends Value> {
* @throws IndexOutOfBoundsException if the operand stack is full.
*/
public void push(final V value) {
if (nLocals + nStack >= values.length) {
if (numLocals + numStack >= values.length) {
throw new IndexOutOfBoundsException("Insufficient maximum stack size.");
}
values[nLocals + (nStack++)] = value;
values[numLocals + (numStack++)] = value;
}
/**
......@@ -653,11 +653,11 @@ public class Frame<V extends Value> {
*/
public boolean merge(final Frame<? extends V> frame, final Interpreter<V> interpreter)
throws AnalyzerException {
if (nStack != frame.nStack) {
if (numStack != frame.numStack) {
throw new AnalyzerException(null, "Incompatible stack heights");
}
boolean changed = false;
for (int i = 0; i < nLocals + nStack; ++i) {
for (int i = 0; i < numLocals + numStack; ++i) {
V v = interpreter.merge(values[i], frame.values[i]);
if (!v.equals(values[i])) {
values[i] = v;
......@@ -680,7 +680,7 @@ public class Frame<V extends Value> {
*/
public boolean merge(final Frame<? extends V> frame, final boolean[] localsUsed) {
boolean changed = false;
for (int i = 0; i < nLocals; ++i) {
for (int i = 0; i < numLocals; ++i) {
if (!localsUsed[i] && !values[i].equals(frame.values[i])) {
values[i] = frame.values[i];
changed = true;
......
......@@ -256,11 +256,11 @@ public class SimpleVerifier extends BasicVerifier {
}
do {
if (type1 == null || isInterface(type1)) {
return newValue(Type.getObjectType("java/lang/Object"), numDimensions);
return newArrayValue(Type.getObjectType("java/lang/Object"), numDimensions);
}
type1 = getSuperClass(type1);
if (isAssignableFrom(type1, type2)) {
return newValue(type1, numDimensions);
return newArrayValue(type1, numDimensions);
}
} while (true);
}
......@@ -269,7 +269,7 @@ public class SimpleVerifier extends BasicVerifier {
return value1;
}
private BasicValue newValue(final Type type, final int dimensions) {
private BasicValue newArrayValue(final Type type, final int dimensions) {
if (dimensions == 0) {
return newValue(type);
} else {
......
......@@ -60,8 +60,8 @@ public class BasicInterpreterTest extends AsmTest {
* Tests that stack map frames are correctly merged when a JSR instruction can be reached from two
* different control flow paths, with different local variable types (#316204).
*
* @throws IOException
* @throws AnalyzerException
* @throws IOException if the test class can't be loaded.
* @throws AnalyzerException if the test class can't be analyzed.
*/
@Test
public void testMergeWithJsrReachableFromTwoDifferentPaths()
......@@ -80,7 +80,7 @@ public class BasicInterpreterTest extends AsmTest {
* does not follow its required contract (namely that if the merge result is equal to the first
* argument, the first argument should be returned - see #316326).
*
* @throws AnalyzerException
* @throws AnalyzerException if the test class can't be analyzed.
*/
@Test
public void testAnalyzeWithBadInterpreter() throws AnalyzerException {
......@@ -103,7 +103,7 @@ public class BasicInterpreterTest extends AsmTest {
* Tests that the precompiled classes can be successfully analyzed with a BasicInterpreter, and
* that Analyzer can be subclassed to use custom frames.
*
* @throws AnalyzerException
* @throws AnalyzerException if the test class can't be analyzed.
*/
@ParameterizedTest
@MethodSource(ALL_CLASSES_AND_LATEST_API)
......@@ -115,8 +115,8 @@ public class BasicInterpreterTest extends AsmTest {
Analyzer<BasicValue> analyzer =
new Analyzer<BasicValue>(new BasicInterpreter()) {
@Override
protected Frame<BasicValue> newFrame(final int nLocals, final int nStack) {
return new CustomFrame(nLocals, nStack);
protected Frame<BasicValue> newFrame(final int numLocals, final int numStack) {
return new CustomFrame(numLocals, numStack);
}
@Override
......@@ -142,8 +142,8 @@ public class BasicInterpreterTest extends AsmTest {
private static class CustomFrame extends Frame<BasicValue> {
CustomFrame(final int nLocals, final int nStack) {
super(nLocals, nStack);
CustomFrame(final int numLocals, final int numStack) {
super(numLocals, numStack);
}
CustomFrame(final Frame<? extends BasicValue> frame) {
......
......@@ -52,7 +52,7 @@ public class BasicVerifierTest extends AsmTest {
/**
* Tests that the precompiled classes can be successfully analyzed with a BasicVerifier.
*
* @throws AnalyzerException
* @throws AnalyzerException if the test class can't be analyzed.
*/
@ParameterizedTest
@MethodSource(ALL_CLASSES_AND_LATEST_API)
......
......@@ -508,9 +508,15 @@ public class SimpleVerifierTest extends AsmTest implements Opcodes {
@Override
protected Class<?> getClass(final Type type) {
// Return dummy classes, to make sure isAssignable in test() does not rely on them.
if (type == baseType) return int.class;
if (type == superType) return float.class;
if (type == interfaceType) return double.class;
if (type == baseType) {
return int.class;
}
if (type == superType) {
return float.class;
}
if (type == interfaceType) {
return double.class;
}
return super.getClass(type);
}
}.test();
......@@ -536,7 +542,7 @@ public class SimpleVerifierTest extends AsmTest implements Opcodes {
* algorithm, due to multiple interface inheritance), but the subtyping check is relaxed if the
* super type is an interface type.
*
* @throws AnalyzerException
* @throws AnalyzerException if the test class can't be analyzed.
*/
@Test
public void testIsAssignableFromInterface() throws AnalyzerException {
......@@ -565,7 +571,7 @@ public class SimpleVerifierTest extends AsmTest implements Opcodes {
/**
* Tests that the precompiled classes can be successfully analyzed with a SimpleVerifier.
*
* @throws AnalyzerException
* @throws AnalyzerException if the test class can't be analyzed.
*/
@ParameterizedTest
@MethodSource(ALL_CLASSES_AND_LATEST_API)
......
......@@ -114,7 +114,7 @@ public class SmallSetTest {
assertThrows(UnsupportedOperationException.class, () -> iterator.remove());
}
private SmallSet<Object> newSmallSet(Object element1, Object element2) {
private SmallSet<Object> newSmallSet(final Object element1, final Object element2) {
return (SmallSet<Object>) new SmallSet<Object>(element1).union(new SmallSet<Object>(element2));
}
}
......@@ -52,7 +52,7 @@ public class SourceInterpreterTest extends AsmTest {
/**
* Tests that the precompiled classes can be successfully analyzed with a SourceInterpreter.
*
* @throws AnalyzerException
* @throws AnalyzerException if the test class can't be analyzed.
*/
@ParameterizedTest
@MethodSource(ALL_CLASSES_AND_LATEST_API)
......
......@@ -436,7 +436,11 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
}
}
/** @deprecated */
/**
* Deprecated.
*
* @deprecated use {@link #visitMethodInsn(int, String, String, String, boolean)} instead.
*/
@Deprecated
@Override
public void visitMethodInsn(
......@@ -564,7 +568,8 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
}
@Override
public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
public void visitTryCatchBlock(
final Label start, final Label end, final Label handler, final String type) {
super.visitTryCatchBlock(start, end, handler, type);
// By definition of 'forwardJumpStackFrames', 'handler' should be pushed only if there is an
// instruction between 'start' and 'end' at which the super class constructor is not yet
......
......@@ -190,16 +190,16 @@ public class AnalyzerAdapter extends MethodVisitor {
@Override
public void visitFrame(
final int type,
final int nLocal,
final int numLocal,
final Object[] local,
final int nStack,
final int numStack,
final Object[] stack) {
if (type != Opcodes.F_NEW) { // Uncompressed frame.
throw new IllegalArgumentException(
"AnalyzerAdapter only accepts expanded frames (see ClassReader.EXPAND_FRAMES)");
}
super.visitFrame(type, nLocal, local, nStack, stack);
super.visitFrame(type, numLocal, local, numStack, stack);
if (this.locals != null) {
this.locals.clear();
......@@ -208,8 +208,8 @@ public class AnalyzerAdapter extends MethodVisitor {
this.locals = new ArrayList<Object>();
this.stack = new ArrayList<Object>();
}
visitFrameTypes(nLocal, local, this.locals);
visitFrameTypes(nStack, stack, this.stack);
visitFrameTypes(numLocal, local, this.locals);
visitFrameTypes(numStack, stack, this.stack);
maxLocals = Math.max(maxLocals, this.locals.size());
maxStack = Math.max(maxStack, this.stack.size());
}
......@@ -279,7 +279,11 @@ public class AnalyzerAdapter extends MethodVisitor {
execute(opcode, 0, descriptor);
}
/** @deprecated */
/**
* Deprecated.
*
* @deprecated use {@link #visitMethodInsn(int, String, String, String, boolean)} instead.
*/
@Deprecated
@Override
public void visitMethodInsn(
......@@ -449,7 +453,12 @@ public class AnalyzerAdapter extends MethodVisitor {
@Override
public void visitLocalVariable(
String name, String descriptor, String signature, Label start, Label end, int index) {
final String name,
final String descriptor,
final String signature,
final Label start,
final Label end,
final int index) {
char firstDescriptorChar = descriptor.charAt(0);
maxLocals =
Math.max(
......
......@@ -111,7 +111,11 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
super.visitFieldInsn(opcode, owner, name, descriptor);
}
/** @deprecated */
/**
* Deprecated.
*
* @deprecated use {@link #visitMethodInsn(int, String, String, String, boolean)} instead.
*/
@Deprecated
@Override
public void visitMethodInsn(
......@@ -138,7 +142,7 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
}
private void doVisitMethodInsn(
int opcode,
final int opcode,
final String owner,
final String name,
final String descriptor,
......
......@@ -50,7 +50,7 @@ import org.objectweb.asm.Type;
* }
* </pre>
*
* can be generated as follows:
* <p>can be generated as follows:
*
* <pre>
* ClassWriter cw = new ClassWriter(0);
......
......@@ -506,7 +506,11 @@ public class InstructionAdapter extends MethodVisitor {
}
}
/** @deprecated */
/**
* Deprecated.
*
* @deprecated use {@link #visitMethodInsn(int, String, String, String, boolean)} instead.
*/
@Deprecated
@Override
public void visitMethodInsn(
......@@ -694,10 +698,21 @@ public class InstructionAdapter extends MethodVisitor {
// -----------------------------------------------------------------------------------------------
/** Generates a nop instruction. */
public void nop() {
mv.visitInsn(Opcodes.NOP);
}
/**
* Generates the instruction to push the given value on the stack.
*
* @param value the constant to be pushed on the stack. This parameter must be an {@link Integer},
* a {@link Float}, a {@link Long}, a {@link Double}, a {@link String}, a {@link Type} of
* OBJECT or ARRAY sort for {@code .class} constants, for classes whose version is 49, a
* {@link Type} of METHOD sort for MethodType, a {@link Handle} for MethodHandle constants,
* for classes whose version is 51 or a {@link ConstantDynamic} for a constant dynamic for
* classes whose version is 55.
*/
public void aconst(final Object value) {
if (value == null) {
mv.visitInsn(Opcodes.ACONST_NULL);
......@@ -706,6 +721,11 @@ public class InstructionAdapter extends MethodVisitor {
}
}
/**
* Generates the instruction to push the given value on the stack.
*
* @param intValue the constant to be pushed on the stack.
*/
public void iconst(final int intValue) {
if (intValue >= -1 && intValue <= 5) {
mv.visitInsn(Opcodes.ICONST_0 + intValue);
......@@ -718,6 +738,11 @@ public class InstructionAdapter extends MethodVisitor {
}
}
/**
* Generates the instruction to push the given value on the stack.
*
* @param longValue the constant to be pushed on the stack.
*/
public void lconst(final long longValue) {
if (longValue == 0L || longValue == 1L) {
mv.visitInsn(Opcodes.LCONST_0 + (int) longValue);
......@@ -726,6 +751,11 @@ public class InstructionAdapter extends MethodVisitor {
}
}
/**
* Generates the instruction to push the given value on the stack.
*
* @param floatValue the constant to be pushed on the stack.
*/
public void fconst(final float floatValue) {
int bits = Float.floatToIntBits(floatValue);
if (bits == 0L || bits == 0x3F800000 || bits == 0x40000000) { // 0..2
......@@ -735,6 +765,11 @@ public class InstructionAdapter extends MethodVisitor {
}
}
/**
* Generates the instruction to push the given value on the stack.
*
* @param doubleValue the constant to be pushed on the stack.
*/
public void dconst(final double doubleValue) {
long bits = Double.doubleToLongBits(doubleValue);
if (bits == 0L || bits == 0x3FF0000000000000L) { // +0.0d and 1.0d
......@@ -744,14 +779,29 @@ public class InstructionAdapter extends MethodVisitor {
}
}
/**
* Generates the instruction to push the given type on the stack.
*
* @param type the type to be pushed on the stack.
*/
public void tconst(final Type type) {
mv.visitLdcInsn(type);
}
/**
* Generates the instruction to push the given handle on the stack.
*
* @param handle the handle to be pushed on the stack.
*/
public void hconst(final Handle handle) {
mv.visitLdcInsn(handle);
}
/**
* Generates the instruction to push the given constant dynamic on the stack.
*
* @param constantDynamic the constant dynamic to be pushed on the stack.
*/
public void cconst(final ConstantDynamic constantDynamic) {
mv.visitLdcInsn(constantDynamic);
}
......@@ -860,6 +910,12 @@ public class InstructionAdapter extends MethodVisitor {
mv.visitIincInsn(var, increment);
}
/**
* Generates the instruction to cast from the first given type to the other.
*
* @param from a Type.
* @param to a Type.
*/
public void cast(final Type from, final Type to) {
if (from != to) {
if (from == Type.DOUBLE_TYPE) {
......@@ -1016,10 +1072,12 @@ public class InstructionAdapter extends MethodVisitor {
}
/**
* Deprecated.
*
* @param owner the internal name of the method's owner class.
* @param name the method's name.
* @param descriptor the method's descriptor (see {@link Type}).
* @deprecated
* @deprecated use {@link #invokevirtual(String, String, String, boolean)} instead.
*/
@Deprecated
public void invokevirtual(final String owner, final String name, final String descriptor) {
......@@ -1030,6 +1088,15 @@ public class InstructionAdapter extends MethodVisitor {
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, name, descriptor);
}
/**
* Generates the instruction to call the given virtual method.
*
* @param owner the internal name of the method's owner class (see {@link
* Type#getInternalName()}).
* @param name the method's name.
* @param descriptor the method's descriptor (see {@link Type}).
* @param isInterface if the method's owner class is an interface.
*/
public void invokevirtual(
final String owner, final String name, final String descriptor, final boolean isInterface) {
if (api < Opcodes.ASM5) {
......@@ -1043,10 +1110,12 @@ public class InstructionAdapter extends MethodVisitor {
}
/**
* Deprecated.
*
* @param owner the internal name of the method's owner class.
* @param name the method's name.
* @param descriptor the method's descriptor (see {@link Type}).
* @deprecated
* @deprecated use {@link #invokespecial(String, String, String, boolean)} instead.
*/
@Deprecated
public void invokespecial(final String owner, final String name, final String descriptor) {
......@@ -1057,6 +1126,15 @@ public class InstructionAdapter extends MethodVisitor {
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, owner, name, descriptor, false);
}
/**
* Generates the instruction to call the given special method.
*
* @param owner the internal name of the method's owner class (see {@link
* Type#getInternalName()}).
* @param name the method's name.
* @param descriptor the method's descriptor (see {@link Type}).
* @param isInterface if the method's owner class is an interface.
*/
public void invokespecial(
final String owner, final String name, final String descriptor, final boolean isInterface) {
if (api < Opcodes.ASM5) {
......@@ -1070,10 +1148,12 @@ public class InstructionAdapter extends MethodVisitor {
}
/**
* Deprecated.
*
* @param owner the internal name of the method's owner class.
* @param name the method's name.
* @param descriptor the method's descriptor (see {@link Type}).
* @deprecated
* @deprecated use {@link #invokestatic(String, String, String, boolean)} instead.
*/
@Deprecated
public void invokestatic(final String owner, final String name, final String descriptor) {
......@@ -1084,6 +1164,15 @@ public class InstructionAdapter extends MethodVisitor {
mv.visitMethodInsn(Opcodes.INVOKESTATIC, owner, name, descriptor, false);
}
/**
* Generates the instruction to call the given static method.
*
* @param owner the internal name of the method's owner class (see {@link
* Type#getInternalName()}).
* @param name the method's name.
* @param descriptor the method's descriptor (see {@link Type}).
* @param isInterface if the method's owner class is an interface.
*/
public void invokestatic(
final String owner, final String name, final String descriptor, final boolean isInterface) {
if (api < Opcodes.ASM5) {
......@@ -1096,10 +1185,29 @@ public class InstructionAdapter extends MethodVisitor {
mv.visitMethodInsn(Opcodes.INVOKESTATIC, owner, name, descriptor, isInterface);