Search in sources :

Example 1 with MemberTypeBinding

use of org.eclipse.jdt.internal.compiler.lookup.MemberTypeBinding in project bazel-jdt-java-toolchain by salesforce.

the class MethodDeclaration method analyseCode.

public void analyseCode(ClassScope classScope, FlowContext flowContext, FlowInfo flowInfo) {
    // starting of the code analysis for methods
    if (this.ignoreFurtherInvestigation)
        return;
    try {
        if (this.binding == null)
            return;
        if (!this.binding.isUsed() && !this.binding.isAbstract()) {
            if (this.binding.isPrivate() || (((this.binding.modifiers & (ExtraCompilerModifiers.AccOverriding | ExtraCompilerModifiers.AccImplementing)) == 0) && this.binding.isOrEnclosedByPrivateType())) {
                if (!classScope.referenceCompilationUnit().compilationResult.hasSyntaxError) {
                    this.scope.problemReporter().unusedPrivateMethod(this);
                }
            }
        }
        // skip enum implicit methods
        if (this.binding.declaringClass.isEnum() && (this.selector == TypeConstants.VALUES || this.selector == TypeConstants.VALUEOF))
            return;
        // may be in a non necessary <clinit> for innerclass with static final constant fields
        if (this.binding.isAbstract() || this.binding.isNative())
            return;
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=385780
        if (this.typeParameters != null && !this.scope.referenceCompilationUnit().compilationResult.hasSyntaxError) {
            for (int i = 0, length = this.typeParameters.length; i < length; ++i) {
                TypeParameter typeParameter = this.typeParameters[i];
                if ((typeParameter.binding.modifiers & ExtraCompilerModifiers.AccLocallyUsed) == 0) {
                    this.scope.problemReporter().unusedTypeParameter(typeParameter);
                }
            }
        }
        ExceptionHandlingFlowContext methodContext = new ExceptionHandlingFlowContext(flowContext, this, this.binding.thrownExceptions, null, this.scope, FlowInfo.DEAD_END);
        // nullity and mark as assigned
        analyseArguments(classScope.environment(), flowInfo, this.arguments, this.binding);
        if (this.binding.declaringClass instanceof MemberTypeBinding && !this.binding.declaringClass.isStatic()) {
            // method of a non-static member type can't be static.
            this.bits &= ~ASTNode.CanBeStatic;
        }
        // propagate to statements
        if (this.statements != null) {
            CompilerOptions compilerOptions = this.scope.compilerOptions();
            boolean enableSyntacticNullAnalysisForFields = compilerOptions.enableSyntacticNullAnalysisForFields;
            int complaintLevel = (flowInfo.reachMode() & FlowInfo.UNREACHABLE) == 0 ? Statement.NOT_COMPLAINED : Statement.COMPLAINED_FAKE_REACHABLE;
            for (int i = 0, count = this.statements.length; i < count; i++) {
                Statement stat = this.statements[i];
                if ((complaintLevel = stat.complainIfUnreachable(flowInfo, this.scope, complaintLevel, true)) < Statement.COMPLAINED_UNREACHABLE) {
                    flowInfo = stat.analyseCode(this.scope, methodContext, flowInfo);
                }
                if (enableSyntacticNullAnalysisForFields) {
                    methodContext.expireNullCheckedFieldInfo();
                }
                if (compilerOptions.analyseResourceLeaks) {
                    FakedTrackingVariable.cleanUpUnassigned(this.scope, stat, flowInfo);
                }
            }
        } else {
            // method with empty body should not be flagged as static.
            this.bits &= ~ASTNode.CanBeStatic;
        }
        // check for missing returning path
        TypeBinding returnTypeBinding = this.binding.returnType;
        if ((returnTypeBinding == TypeBinding.VOID) || isAbstract()) {
            if ((flowInfo.tagBits & FlowInfo.UNREACHABLE_OR_DEAD) == 0) {
                this.bits |= ASTNode.NeedFreeReturn;
            }
        } else {
            if (flowInfo != FlowInfo.DEAD_END) {
                this.scope.problemReporter().shouldReturn(returnTypeBinding, this);
            }
        }
        // check unreachable catch blocks
        methodContext.complainIfUnusedExceptionHandlers(this);
        // check unused parameters
        this.scope.checkUnusedParameters(this.binding);
        // check if the method could have been static
        if (!this.binding.isStatic() && (this.bits & ASTNode.CanBeStatic) != 0 && !this.isDefaultMethod()) {
            if (!this.binding.isOverriding() && !this.binding.isImplementing()) {
                if (this.binding.isPrivate() || this.binding.isFinal() || this.binding.declaringClass.isFinal()) {
                    this.scope.problemReporter().methodCanBeDeclaredStatic(this);
                } else {
                    this.scope.problemReporter().methodCanBePotentiallyDeclaredStatic(this);
                }
            }
        }
        this.scope.checkUnclosedCloseables(flowInfo, null, null, /*don't report against a specific location*/
        null);
    } catch (AbortMethod e) {
        this.ignoreFurtherInvestigation = true;
    }
}
Also used : MemberTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MemberTypeBinding) MemberTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MemberTypeBinding) LocalTypeBinding(org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) ExceptionHandlingFlowContext(org.eclipse.jdt.internal.compiler.flow.ExceptionHandlingFlowContext) AbortMethod(org.eclipse.jdt.internal.compiler.problem.AbortMethod)

Aggregations

ExceptionHandlingFlowContext (org.eclipse.jdt.internal.compiler.flow.ExceptionHandlingFlowContext)1 CompilerOptions (org.eclipse.jdt.internal.compiler.impl.CompilerOptions)1 LocalTypeBinding (org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding)1 MemberTypeBinding (org.eclipse.jdt.internal.compiler.lookup.MemberTypeBinding)1 TypeBinding (org.eclipse.jdt.internal.compiler.lookup.TypeBinding)1 AbortMethod (org.eclipse.jdt.internal.compiler.problem.AbortMethod)1