All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.errorprone.bugpatterns.threadsafety.LockMethodChecker Maven / Gradle / Ivy

There is a newer version: 2.28.0
Show newest version
/*
 * Copyright 2014 The Error Prone Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.errorprone.bugpatterns.threadsafety;

import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.annotations.concurrent.LockMethod;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.MethodTree;
import java.util.Set;

/** @author [email protected] (Liam Miller-Cushon) */
@BugPattern(
    name = "LockMethodChecker",
    altNames = {"GuardedBy"},
    summary = "This method does not acquire the locks specified by its @LockMethod annotation",
    severity = ERROR)
public class LockMethodChecker extends AbstractLockMethodChecker {

  @Override
  protected ImmutableList getLockExpressions(MethodTree tree) {
    LockMethod lockMethod = ASTHelpers.getAnnotation(tree, LockMethod.class);
    return lockMethod == null
        ? ImmutableList.of()
        : ImmutableList.copyOf(lockMethod.value());
  }

  @Override
  protected Set getActual(MethodTree tree, VisitorState state) {
    return ImmutableSet.copyOf(
        HeldLockAnalyzer.AcquiredLockFinder.find(tree.getBody(), state, GuardedByFlags.allOn()));
  }

  @Override
  protected Set getUnwanted(MethodTree tree, VisitorState state) {
    return ImmutableSet.copyOf(
        HeldLockAnalyzer.ReleasedLockFinder.find(tree.getBody(), state, GuardedByFlags.allOn()));
  }

  @Override
  protected String buildMessage(String unhandled) {
    return "The following locks are specifed in this method's @LockMethod annotation but are not"
        + " acquired: "
        + unhandled;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy