|
| 1 | +/* |
| 2 | + * Copyright 2018 The Error Prone Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.errorprone.bugpatterns.time; |
| 17 | + |
| 18 | +import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; |
| 19 | +import static com.google.errorprone.bugpatterns.time.NearbyCallers.containsCallToSameReceiverNearby; |
| 20 | +import static com.google.errorprone.matchers.Matchers.allOf; |
| 21 | +import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod; |
| 22 | + |
| 23 | +import com.google.errorprone.BugPattern; |
| 24 | +import com.google.errorprone.VisitorState; |
| 25 | +import com.google.errorprone.bugpatterns.BugChecker; |
| 26 | +import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; |
| 27 | +import com.google.errorprone.fixes.SuggestedFixes; |
| 28 | +import com.google.errorprone.matchers.Description; |
| 29 | +import com.google.errorprone.matchers.Matcher; |
| 30 | +import com.google.errorprone.matchers.Matchers; |
| 31 | +import com.sun.source.tree.ExpressionTree; |
| 32 | +import com.sun.source.tree.MethodInvocationTree; |
| 33 | + |
| 34 | +/** Error prone checking for {@code Duration.getSeconds()} without {@code Duration.getNano()}. */ |
| 35 | +@BugPattern( |
| 36 | + summary = "Prefer duration.toSeconds() over duration.getSeconds()", |
| 37 | + explanation = |
| 38 | + "duration.getSeconds() is a decomposition API which should always be used alongside" |
| 39 | + + " duration.getNano(). duration.toSeconds() is a conversion API, and the preferred way" |
| 40 | + + " to convert to seconds.", |
| 41 | + severity = WARNING) |
| 42 | +public final class JavaDurationGetSecondsToToSeconds extends BugChecker |
| 43 | + implements MethodInvocationTreeMatcher { |
| 44 | + |
| 45 | + private static final Matcher<ExpressionTree> GET_SECONDS = |
| 46 | + instanceMethod().onExactClass("java.time.Duration").named("getSeconds"); |
| 47 | + private static final Matcher<ExpressionTree> GET_NANO = |
| 48 | + allOf( |
| 49 | + instanceMethod().onExactClass("java.time.Duration").named("getNano"), |
| 50 | + Matchers.not(Matchers.packageStartsWith("java."))); |
| 51 | + |
| 52 | + @Override |
| 53 | + public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { |
| 54 | + if (GET_SECONDS.matches(tree, state)) { |
| 55 | + if (!containsCallToSameReceiverNearby(tree, GET_NANO, state, /* checkProtoChains= */ false)) { |
| 56 | + return describeMatch(tree, SuggestedFixes.renameMethodInvocation(tree, "toSeconds", state)); |
| 57 | + } |
| 58 | + } |
| 59 | + return Description.NO_MATCH; |
| 60 | + } |
| 61 | +} |
0 commit comments