/** Analysing arguments of MessageSend, ExplicitConstructorCall, AllocationExpression. */ protected void analyseArguments( BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo, MethodBinding methodBinding, Expression[] arguments) { // compare actual null-status against parameter annotations of the called method: if (arguments != null) { CompilerOptions compilerOptions = currentScope.compilerOptions(); boolean considerTypeAnnotations = compilerOptions.sourceLevel >= ClassFileConstants.JDK1_8 && compilerOptions.isAnnotationBasedNullAnalysisEnabled; boolean hasJDK15NullAnnotations = methodBinding.parameterNonNullness != null; int numParamsToCheck = methodBinding.parameters.length; if (considerTypeAnnotations || hasJDK15NullAnnotations) { // check if varargs need special treatment: boolean passThrough = false; if (methodBinding.isVarargs()) { int varArgPos = numParamsToCheck - 1; // this if-block essentially copied from generateArguments(..): if (numParamsToCheck == arguments.length) { TypeBinding varArgsType = methodBinding.parameters[varArgPos]; TypeBinding lastType = arguments[varArgPos].resolvedType; if (lastType == TypeBinding.NULL || (varArgsType.dimensions() == lastType.dimensions() && lastType.isCompatibleWith(varArgsType))) passThrough = true; // pass directly as-is } if (!passThrough) numParamsToCheck--; // with non-passthrough varargs last param is fed from individual // args -> don't check } } if (considerTypeAnnotations) { for (int i = 0; i < numParamsToCheck; i++) { TypeBinding expectedType = methodBinding.parameters[i]; Expression argument = arguments[i]; // prefer check based on type annotations: int severity = findNullTypeAnnotationMismatch(expectedType, argument.resolvedType); if (severity > 0) { // immediate reporting: currentScope .problemReporter() .nullityMismatchingTypeAnnotation( argument, argument.resolvedType, expectedType, severity == 1, currentScope.environment()); // next check flow-based null status against null JDK15-style annotations: } else if (hasJDK15NullAnnotations && methodBinding.parameterNonNullness[i] == Boolean.TRUE) { int nullStatus = argument.nullStatus( flowInfo, flowContext); // slight loss of precision: should also use the null info from // the receiver. if (nullStatus != FlowInfo.NON_NULL) // if required non-null is not provided flowContext.recordNullityMismatch( currentScope, argument, argument.resolvedType, expectedType, nullStatus); } } } else if (hasJDK15NullAnnotations) { for (int i = 0; i < numParamsToCheck; i++) { if (methodBinding.parameterNonNullness[i] == Boolean.TRUE) { TypeBinding expectedType = methodBinding.parameters[i]; Expression argument = arguments[i]; int nullStatus = argument.nullStatus( flowInfo, flowContext); // slight loss of precision: should also use the null info from // the receiver. if (nullStatus != FlowInfo.NON_NULL) // if required non-null is not provided flowContext.recordNullityMismatch( currentScope, argument, argument.resolvedType, expectedType, nullStatus); } } } } }
/** Generate invocation arguments, considering varargs methods */ public void generateArguments( MethodBinding binding, Expression[] arguments, BlockScope currentScope, CodeStream codeStream) { if (binding.isVarargs()) { // 5 possibilities exist for a call to the vararg method foo(int i, int ... value) : // foo(1), foo(1, null), foo(1, 2), foo(1, 2, 3, 4) & foo(1, new int[] {1, 2}) TypeBinding[] params = binding.parameters; int paramLength = params.length; int varArgIndex = paramLength - 1; for (int i = 0; i < varArgIndex; i++) { arguments[i].generateCode(currentScope, codeStream, true); } ArrayBinding varArgsType = (ArrayBinding) params[varArgIndex]; // parameterType has to be an array type ArrayBinding codeGenVarArgsType = (ArrayBinding) binding.parameters[varArgIndex].erasure(); int elementsTypeID = varArgsType.elementsType().id; int argLength = arguments == null ? 0 : arguments.length; if (argLength > paramLength) { // right number but not directly compatible or too many arguments - wrap extra into array // called with (argLength - lastIndex) elements : foo(1, 2) or foo(1, 2, 3, 4) // need to gen elements into an array, then gen each remaining element into created array codeStream.generateInlinedValue(argLength - varArgIndex); codeStream.newArray(null, codeGenVarArgsType); // create a mono-dimensional array for (int i = varArgIndex; i < argLength; i++) { codeStream.dup(); codeStream.generateInlinedValue(i - varArgIndex); arguments[i].generateCode(currentScope, codeStream, true); codeStream.arrayAtPut(elementsTypeID, false); } } else if (argLength == paramLength) { // right number of arguments - could be inexact - pass argument as is TypeBinding lastType = arguments[varArgIndex].resolvedType; if (lastType == TypeBinding.NULL || (varArgsType.dimensions() == lastType.dimensions() && lastType.isCompatibleWith(varArgsType))) { // foo(1, new int[]{2, 3}) or foo(1, null) --> last arg is passed as-is arguments[varArgIndex].generateCode(currentScope, codeStream, true); } else { // right number but not directly compatible or too many arguments - wrap extra into array // need to gen elements into an array, then gen each remaining element into created array codeStream.generateInlinedValue(1); codeStream.newArray(null, codeGenVarArgsType); // create a mono-dimensional array codeStream.dup(); codeStream.generateInlinedValue(0); arguments[varArgIndex].generateCode(currentScope, codeStream, true); codeStream.arrayAtPut(elementsTypeID, false); } } else { // not enough arguments - pass extra empty array // scenario: foo(1) --> foo(1, new int[0]) // generate code for an empty array of parameterType codeStream.generateInlinedValue(0); codeStream.newArray(null, codeGenVarArgsType); // create a mono-dimensional array } } else if (arguments != null) { // standard generation for method arguments for (int i = 0, max = arguments.length; i < max; i++) arguments[i].generateCode(currentScope, codeStream, true); } }