mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
[java-inspections] IDEA-201932 Provide inspection to highlight redundant creation operations in java date time api
GitOrigin-RevId: 6b246167dda52272122d356c3f6a787eab5a2542
This commit is contained in:
committed by
intellij-monorepo-bot
parent
0653f59f3c
commit
f73a4d96be
@@ -1676,6 +1676,9 @@ redundant.call.problem.descriptor=Redundant call to <code>#ref()</code> #loc
|
||||
inspection.explicit.chrono.field.display.name=Calls of 'java.time' methods with explicit 'ChronoField' or 'ChronoUnit' arguments can be simplified
|
||||
inspection.explicit.chrono.field.problem.descriptor=Calls with explicit 'ChronoField' or 'ChronoUnit' arguments call can be simplified
|
||||
inspection.explicit.chrono.field.family.name=Simplify calls with explicit 'ChronoField' or 'ChronoUnit' arguments
|
||||
inspection.redundant.creation.java.time.display.name=Redundant creation of 'java.time' objects
|
||||
inspection.redundant.creation.java.time.error.message=Redundant creation of ''{0}'' object
|
||||
inspection.redundant.creation.java.time.family.name=Simplify creation of 'java.time' object
|
||||
inspection.simplifiable.compare.java.time.display.name=Expression with 'java.time' 'compareTo()' call can be simplified
|
||||
inspection.simplifiable.compare.java.time.family.name=Simplify expression with 'java.time' 'compareTo()' call
|
||||
inspection.simplifiable.compare.java.time.problem.descriptor=Expression with 'java.time' <code>#ref()</code> call can be simplified
|
||||
|
||||
@@ -2686,6 +2686,11 @@
|
||||
enabledByDefault="true" level="WARNING" key="inspection.explicit.chrono.field.display.name"
|
||||
bundle="messages.InspectionGadgetsBundle" implementationClass="com.siyeh.ig.redundancy.RedundantExplicitChronoFieldInspection"
|
||||
cleanupTool="true"/>
|
||||
<localInspection groupPath="Java" language="JAVA"
|
||||
groupBundle="messages.InspectionsBundle" groupKey="group.names.verbose.or.redundant.code.constructs"
|
||||
enabledByDefault="true" level="WEAK WARNING" key="inspection.redundant.creation.java.time.display.name"
|
||||
bundle="messages.InspectionGadgetsBundle" implementationClass="com.siyeh.ig.redundancy.RedundantCreationJavaTimeInspection"
|
||||
cleanupTool="true"/>
|
||||
|
||||
<!--group.names.visibility.issues-->
|
||||
<localInspection groupPath="Java" language="JAVA" shortName="AmbiguousMethodCall" bundle="messages.InspectionGadgetsBundle" key="ambiguous.method.call.display.name"
|
||||
|
||||
@@ -0,0 +1,272 @@
|
||||
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.siyeh.ig.redundancy;
|
||||
|
||||
import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool;
|
||||
import com.intellij.codeInspection.CleanupLocalInspectionTool;
|
||||
import com.intellij.codeInspection.ProblemsHolder;
|
||||
import com.intellij.modcommand.ModCommandQuickFix;
|
||||
import com.intellij.modcommand.ModPsiUpdater;
|
||||
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.callMatcher.CallMatcher;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import static com.intellij.psi.CommonClassNames.*;
|
||||
|
||||
|
||||
public final class RedundantCreationJavaTimeInspection extends AbstractBaseJavaLocalInspectionTool implements CleanupLocalInspectionTool {
|
||||
private static final CallMatcher FROM_MATCHER = CallMatcher.anyOf(
|
||||
CallMatcher.staticCall(JAVA_TIME_LOCAL_TIME, "from").parameterCount(1),
|
||||
CallMatcher.staticCall(JAVA_TIME_LOCAL_DATE, "from").parameterCount(1),
|
||||
CallMatcher.staticCall(JAVA_TIME_LOCAL_DATE_TIME, "from")
|
||||
.parameterCount(1),
|
||||
CallMatcher.staticCall(JAVA_TIME_OFFSET_DATE_TIME, "from")
|
||||
.parameterCount(1),
|
||||
CallMatcher.staticCall(JAVA_TIME_OFFSET_TIME, "from").parameterCount(1),
|
||||
CallMatcher.staticCall(JAVA_TIME_ZONED_DATE_TIME, "from")
|
||||
.parameterCount(1));
|
||||
|
||||
private static final CallMatcher LOCAL_DATE_OF_MATCHER =
|
||||
CallMatcher.anyOf(CallMatcher.staticCall(JAVA_TIME_LOCAL_DATE, "of").parameterTypes("int", "int", "int"),
|
||||
CallMatcher.staticCall(JAVA_TIME_LOCAL_DATE, "of").parameterTypes("int", "java.time.Month", "int"));
|
||||
|
||||
private static final CallMatcher GET_YEAR_MATCHER =
|
||||
CallMatcher.anyOf(
|
||||
CallMatcher.instanceCall(JAVA_TIME_LOCAL_DATE, "getYear").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_LOCAL_DATE_TIME, "getYear").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_OFFSET_DATE_TIME, "getYear").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_ZONED_DATE_TIME, "getYear").parameterCount(0));
|
||||
|
||||
private static final CallMatcher GET_MONTH_MATCHER =
|
||||
CallMatcher.anyOf(
|
||||
CallMatcher.instanceCall(JAVA_TIME_LOCAL_DATE, "getMonth").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_LOCAL_DATE_TIME, "getMonth").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_OFFSET_DATE_TIME, "getMonth").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_ZONED_DATE_TIME, "getMonth").parameterCount(0));
|
||||
|
||||
private static final CallMatcher GET_MONTH_VALUE_MATCHER =
|
||||
CallMatcher.anyOf(
|
||||
CallMatcher.instanceCall(JAVA_TIME_LOCAL_DATE, "getMonthValue").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_LOCAL_DATE_TIME, "getMonthValue").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_OFFSET_DATE_TIME, "getMonthValue").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_ZONED_DATE_TIME, "getMonthValue").parameterCount(0));
|
||||
|
||||
private static final CallMatcher GET_DAY_OF_MONTH_MATCHER =
|
||||
CallMatcher.anyOf(
|
||||
CallMatcher.instanceCall(JAVA_TIME_LOCAL_DATE, "getDayOfMonth").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_LOCAL_DATE_TIME, "getDayOfMonth").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_OFFSET_DATE_TIME, "getDayOfMonth").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_ZONED_DATE_TIME, "getDayOfMonth").parameterCount(0));
|
||||
|
||||
private static final CallMatcher LOCAL_TIME_OF_MATCHER =
|
||||
CallMatcher.staticCall(JAVA_TIME_LOCAL_TIME, "of").parameterTypes("int", "int", "int", "int");
|
||||
|
||||
private static final CallMatcher GET_HOUR_MATCHER =
|
||||
CallMatcher.anyOf(CallMatcher.instanceCall(JAVA_TIME_LOCAL_TIME, "getHour").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_LOCAL_DATE_TIME, "getHour").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_OFFSET_DATE_TIME, "getHour").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_ZONED_DATE_TIME, "getHour").parameterCount(0));
|
||||
|
||||
private static final CallMatcher GET_MINUTE_MATCHER =
|
||||
CallMatcher.anyOf(CallMatcher.instanceCall(JAVA_TIME_LOCAL_TIME, "getMinute").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_LOCAL_DATE_TIME, "getMinute").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_OFFSET_DATE_TIME, "getMinute").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_ZONED_DATE_TIME, "getMinute").parameterCount(0));
|
||||
|
||||
private static final CallMatcher GET_SECOND_MATCHER =
|
||||
CallMatcher.anyOf(CallMatcher.instanceCall(JAVA_TIME_LOCAL_TIME, "getSecond").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_LOCAL_DATE_TIME, "getSecond").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_OFFSET_DATE_TIME, "getSecond").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_ZONED_DATE_TIME, "getSecond").parameterCount(0));
|
||||
|
||||
private static final CallMatcher GET_NANO_MATCHER =
|
||||
CallMatcher.anyOf(CallMatcher.instanceCall(JAVA_TIME_LOCAL_TIME, "getNano").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_LOCAL_DATE_TIME, "getNano").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_OFFSET_DATE_TIME, "getNano").parameterCount(0),
|
||||
CallMatcher.instanceCall(JAVA_TIME_ZONED_DATE_TIME, "getNano").parameterCount(0));
|
||||
private static final String TO_LOCAL_TIME = "toLocalTime";
|
||||
private static final String TO_LOCAL_DATE = "toLocalDate";
|
||||
|
||||
@Override
|
||||
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
|
||||
return new JavaElementVisitor() {
|
||||
@SuppressWarnings("UnnecessaryReturnStatement")
|
||||
@Override
|
||||
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) {
|
||||
if (fixRedundantFrom(call)) return;
|
||||
if (fixRedundantOfLocalDate(call)) return;
|
||||
if (fixRedundantOfLocalTime(call)) return;
|
||||
}
|
||||
|
||||
|
||||
private boolean fixRedundantOfLocalTime(@NotNull PsiMethodCallExpression call) {
|
||||
if (!LOCAL_TIME_OF_MATCHER.test(call)) return false;
|
||||
PsiExpression[] arguments = call.getArgumentList().getExpressions();
|
||||
if (arguments.length != 4) return false;
|
||||
if (!(arguments[0] instanceof PsiMethodCallExpression firstArgumentCall)) return false;
|
||||
if (!GET_HOUR_MATCHER.test(firstArgumentCall)) return false;
|
||||
if (!(arguments[1] instanceof PsiMethodCallExpression secondArgumentCall)) return false;
|
||||
if (!GET_MINUTE_MATCHER.test(secondArgumentCall)) return false;
|
||||
if (!(arguments[2] instanceof PsiMethodCallExpression thirdArgumentCall)) return false;
|
||||
if (!GET_SECOND_MATCHER.test(thirdArgumentCall)) return false;
|
||||
if (!(arguments[3] instanceof PsiMethodCallExpression fourthArgumentCall)) return false;
|
||||
if (!GET_NANO_MATCHER.test(fourthArgumentCall)) return false;
|
||||
PsiExpression expression1 = firstArgumentCall.getMethodExpression().getQualifierExpression();
|
||||
if (!(PsiUtil.skipParenthesizedExprDown(expression1) instanceof PsiReferenceExpression referenceExpression1 &&
|
||||
referenceExpression1.resolve() instanceof PsiVariable variable1)) {
|
||||
return false;
|
||||
}
|
||||
if (!(areSameVariableReferences(holder.getProject(),
|
||||
expression1,
|
||||
secondArgumentCall.getMethodExpression().getQualifierExpression(),
|
||||
thirdArgumentCall.getMethodExpression().getQualifierExpression(),
|
||||
fourthArgumentCall.getMethodExpression().getQualifierExpression()))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PsiClass variableClass = PsiUtil.resolveClassInClassTypeOnly(variable1.getType());
|
||||
if (variableClass == null) return false;
|
||||
PsiElement referenceNameElement = call.getMethodExpression().getReferenceNameElement();
|
||||
if (referenceNameElement == null) return false;
|
||||
|
||||
PsiIdentifier identifier = variable1.getNameIdentifier();
|
||||
if (identifier == null) return false;
|
||||
if(JAVA_TIME_LOCAL_TIME.equals(variableClass.getQualifiedName())) {
|
||||
holder.registerProblem(referenceNameElement,
|
||||
InspectionGadgetsBundle.message("inspection.redundant.creation.java.time.error.message", "LocalTime"),
|
||||
RedundantCreationFix.create(identifier.getText()));
|
||||
return true;
|
||||
}
|
||||
PsiMethod[] localTimes = variableClass.findMethodsByName(TO_LOCAL_TIME, false);
|
||||
if (localTimes.length != 1) return false;
|
||||
|
||||
String newText = identifier.getText() + "." + TO_LOCAL_TIME + "()";
|
||||
|
||||
holder.registerProblem(referenceNameElement,
|
||||
InspectionGadgetsBundle.message("inspection.redundant.creation.java.time.error.message", "LocalTime"),
|
||||
RedundantCreationFix.create(newText));
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean fixRedundantOfLocalDate(@NotNull PsiMethodCallExpression call) {
|
||||
if (!LOCAL_DATE_OF_MATCHER.test(call)) return false;
|
||||
PsiExpression[] arguments = call.getArgumentList().getExpressions();
|
||||
if (arguments.length != 3) return false;
|
||||
if (!(arguments[0] instanceof PsiMethodCallExpression firstArgumentCall)) return false;
|
||||
if (!GET_YEAR_MATCHER.test(firstArgumentCall)) return false;
|
||||
if (!(arguments[1] instanceof PsiMethodCallExpression secondArgumentCall)) return false;
|
||||
if (!GET_MONTH_VALUE_MATCHER.test(secondArgumentCall) && !GET_MONTH_MATCHER.test(secondArgumentCall)) return false;
|
||||
if (!(arguments[2] instanceof PsiMethodCallExpression thirdArgumentCall)) return false;
|
||||
if (!GET_DAY_OF_MONTH_MATCHER.test(thirdArgumentCall)) return false;
|
||||
|
||||
PsiExpression firstArgument = PsiUtil.skipParenthesizedExprDown(firstArgumentCall.getMethodExpression().getQualifierExpression());
|
||||
if (!(firstArgument instanceof PsiReferenceExpression referenceExpression &&
|
||||
referenceExpression.resolve() instanceof PsiVariable firstVariable)) return false;
|
||||
if (!areSameVariableReferences(holder.getProject(),
|
||||
firstArgument,
|
||||
secondArgumentCall.getMethodExpression().getQualifierExpression(),
|
||||
thirdArgumentCall.getMethodExpression().getQualifierExpression())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PsiClass variableClass = PsiUtil.resolveClassInClassTypeOnly(firstArgument.getType());
|
||||
if (variableClass == null) return false;
|
||||
PsiElement referenceNameElement = call.getMethodExpression().getReferenceNameElement();
|
||||
if (referenceNameElement == null) return false;
|
||||
PsiIdentifier identifier = firstVariable.getNameIdentifier();
|
||||
if (identifier == null) return false;
|
||||
|
||||
if (JAVA_TIME_LOCAL_DATE.equals(variableClass.getQualifiedName())) {
|
||||
holder.registerProblem(referenceNameElement,
|
||||
InspectionGadgetsBundle.message("inspection.redundant.creation.java.time.error.message", "LocalDate"),
|
||||
RedundantCreationFix.create(identifier.getText()));
|
||||
return true;
|
||||
}
|
||||
|
||||
PsiMethod[] localDates = variableClass.findMethodsByName(TO_LOCAL_DATE, false);
|
||||
if (localDates.length != 1) return false;
|
||||
String newText = identifier.getText() + "." + TO_LOCAL_DATE + "()";
|
||||
|
||||
holder.registerProblem(referenceNameElement,
|
||||
InspectionGadgetsBundle.message("inspection.redundant.creation.java.time.error.message", "LocalDate"),
|
||||
RedundantCreationFix.create(newText));
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean areSameVariableReferences(@NotNull Project project, PsiExpression... expressions) {
|
||||
if (expressions.length == 0) return false;
|
||||
PsiManager psiManager = PsiManager.getInstance(project);
|
||||
PsiVariable firstVariable = resolveVariable(expressions[0]);
|
||||
if (firstVariable == null) return false;
|
||||
for (PsiExpression expression : expressions) {
|
||||
PsiVariable variable = resolveVariable(expression);
|
||||
if (variable == null || !psiManager.areElementsEquivalent(firstVariable, variable)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiVariable resolveVariable(@Nullable PsiExpression expression) {
|
||||
if (expression == null) return null;
|
||||
PsiExpression unwrappedExpression = PsiUtil.skipParenthesizedExprDown(expression);
|
||||
if (!(unwrappedExpression instanceof PsiReferenceExpression referenceExpression)) return null;
|
||||
PsiElement resolvedElement = referenceExpression.resolve();
|
||||
return (resolvedElement instanceof PsiVariable variable) ? variable : null;
|
||||
}
|
||||
|
||||
private boolean fixRedundantFrom(@NotNull PsiMethodCallExpression call) {
|
||||
if (!FROM_MATCHER.test(call)) return false;
|
||||
PsiExpression[] arguments = call.getArgumentList().getExpressions();
|
||||
if (arguments.length != 1) return false;
|
||||
PsiExpression expression = arguments[0];
|
||||
PsiClass classOfArgument = PsiUtil.resolveClassInClassTypeOnly(expression.getType());
|
||||
PsiMethod method = call.resolveMethod();
|
||||
if (method == null) return false;
|
||||
PsiClass classOfMethod = method.getContainingClass();
|
||||
if (classOfMethod == null) return false;
|
||||
PsiManager manager = classOfMethod.getManager();
|
||||
if (!manager.areElementsEquivalent(classOfArgument, classOfMethod)) return false;
|
||||
String newText = expression.getText();
|
||||
if (newText == null) return false;
|
||||
PsiElement identifier = call.getMethodExpression().getReferenceNameElement();
|
||||
if (identifier == null) return false;
|
||||
String className = classOfMethod.getName();
|
||||
if (className == null) return false;
|
||||
holder.registerProblem(identifier,
|
||||
InspectionGadgetsBundle.message("inspection.redundant.creation.java.time.error.message", className),
|
||||
RedundantCreationFix.create(newText));
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static class RedundantCreationFix extends PsiUpdateModCommandQuickFix {
|
||||
@NotNull private final String myNewText;
|
||||
|
||||
private RedundantCreationFix(@NotNull String text) { myNewText = text; }
|
||||
|
||||
@Override
|
||||
protected void applyFix(@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) {
|
||||
PsiMethodCallExpression callExpression = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class, false);
|
||||
if (callExpression == null) return;
|
||||
new CommentTracker().replaceAndRestoreComments(callExpression, myNewText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getFamilyName() {
|
||||
return InspectionGadgetsBundle.message("inspection.redundant.creation.java.time.family.name");
|
||||
}
|
||||
|
||||
private static @NotNull ModCommandQuickFix create(@NotNull String newText) {
|
||||
return new RedundantCreationFix(newText);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<!-- Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -->
|
||||
<html>
|
||||
<body>
|
||||
Reports redundant creation of date/time objects using <code>java.time</code> classes
|
||||
when simpler method calls can be used or creation can be avoided.
|
||||
|
||||
<p>The main <code>java.date</code> classes are marked as <code>@jdk.internal.ValueBased</code>.
|
||||
Such creations should be avoided as these classes are designed to be used in a value-based manner.</p>
|
||||
|
||||
<p>Example:</p>
|
||||
<pre><code>
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
return LocalDateTime.from(now);
|
||||
</code></pre>
|
||||
<p>After the quick-fix is applied:</p>
|
||||
<pre><code>
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
return now;
|
||||
</code></pre>
|
||||
|
||||
<!-- tooltip end -->
|
||||
<p><small>New in 2024.3</small></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,9 @@
|
||||
import java.time.*;
|
||||
|
||||
class Main {
|
||||
|
||||
LocalTime convert(LocalTime source)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import java.time.*;
|
||||
|
||||
public class DateRedundant {
|
||||
class First {
|
||||
LocalDateTime convert(LocalDateTime source)
|
||||
{
|
||||
return source<caret>;
|
||||
}
|
||||
|
||||
LocalTime convert(LocalTime source)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
LocalDate convert(LocalDate source)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
OffsetDateTime convert(OffsetDateTime source)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
OffsetTime convert(OffsetTime source)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
ZonedDateTime convert(ZonedDateTime source)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static class Third {
|
||||
private LocalDate getLocalDate(LocalDate fullTime)
|
||||
{
|
||||
return fullTime;
|
||||
}
|
||||
|
||||
private LocalDate getLocalDate(ZonedDateTime fullTime)
|
||||
{
|
||||
return fullTime.toLocalDate();
|
||||
}
|
||||
|
||||
private LocalTime getLocalTime(ZonedDateTime fullTime)
|
||||
{
|
||||
return fullTime.toLocalTime();
|
||||
}
|
||||
|
||||
private LocalDate getLocalDate(LocalDateTime fullTime)
|
||||
{
|
||||
return fullTime.toLocalDate();
|
||||
}
|
||||
|
||||
private LocalDate getLocalDate2(LocalDateTime fullTime)
|
||||
{
|
||||
return fullTime.toLocalDate();
|
||||
}
|
||||
|
||||
private LocalTime getLocalTime(LocalDateTime fullTime)
|
||||
{
|
||||
return fullTime.toLocalTime();
|
||||
}
|
||||
|
||||
private LocalDate getLocalDate(OffsetDateTime fullTime)
|
||||
{
|
||||
return fullTime.toLocalDate();
|
||||
}
|
||||
|
||||
private LocalTime getLocalTime(OffsetDateTime fullTime)
|
||||
{
|
||||
return fullTime.toLocalTime();
|
||||
}
|
||||
|
||||
private LocalTime getLocalTime(LocalTime fullTime)
|
||||
{
|
||||
return fullTime;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import java.time.*;
|
||||
|
||||
class Main {
|
||||
|
||||
LocalTime convert(LocalTime source)
|
||||
{
|
||||
return LocalTime.from<caret>(source);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
import java.time.*;
|
||||
|
||||
public class DateRedundant {
|
||||
class First {
|
||||
LocalDateTime convert(LocalDateTime source)
|
||||
{
|
||||
return LocalDateTime.from<caret>(source);
|
||||
}
|
||||
|
||||
LocalTime convert(LocalTime source)
|
||||
{
|
||||
return LocalTime.from(source);
|
||||
}
|
||||
|
||||
LocalDate convert(LocalDate source)
|
||||
{
|
||||
return LocalDate.from(source);
|
||||
}
|
||||
|
||||
OffsetDateTime convert(OffsetDateTime source)
|
||||
{
|
||||
return OffsetDateTime.from(source);
|
||||
}
|
||||
|
||||
OffsetTime convert(OffsetTime source)
|
||||
{
|
||||
return OffsetTime.from(source);
|
||||
}
|
||||
|
||||
ZonedDateTime convert(ZonedDateTime source)
|
||||
{
|
||||
return ZonedDateTime.from(source);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static class Third {
|
||||
private LocalDate getLocalDate(LocalDate fullTime)
|
||||
{
|
||||
return LocalDate.of(fullTime.getYear(),
|
||||
fullTime.getMonth(),
|
||||
fullTime.getDayOfMonth()
|
||||
);
|
||||
}
|
||||
|
||||
private LocalDate getLocalDate(ZonedDateTime fullTime)
|
||||
{
|
||||
return LocalDate.of(fullTime.getYear(),
|
||||
fullTime.getMonth(),
|
||||
fullTime.getDayOfMonth()
|
||||
);
|
||||
}
|
||||
|
||||
private LocalTime getLocalTime(ZonedDateTime fullTime)
|
||||
{
|
||||
return LocalTime.of(fullTime.getHour(),
|
||||
fullTime.getMinute(),
|
||||
fullTime.getSecond(),
|
||||
fullTime.getNano());
|
||||
}
|
||||
|
||||
private LocalDate getLocalDate(LocalDateTime fullTime)
|
||||
{
|
||||
return LocalDate.of(fullTime.getYear(),
|
||||
fullTime.getMonth(),
|
||||
fullTime.getDayOfMonth()
|
||||
);
|
||||
}
|
||||
|
||||
private LocalDate getLocalDate2(LocalDateTime fullTime)
|
||||
{
|
||||
return LocalDate.of(fullTime.getYear(),
|
||||
fullTime.getMonthValue(),
|
||||
fullTime.getDayOfMonth()
|
||||
);
|
||||
}
|
||||
|
||||
private LocalTime getLocalTime(LocalDateTime fullTime)
|
||||
{
|
||||
return LocalTime.of(fullTime.getHour(),
|
||||
fullTime.getMinute(),
|
||||
fullTime.getSecond(),
|
||||
fullTime.getNano());
|
||||
}
|
||||
|
||||
private LocalDate getLocalDate(OffsetDateTime fullTime)
|
||||
{
|
||||
return LocalDate.of(fullTime.getYear(),
|
||||
fullTime.getMonthValue(),
|
||||
fullTime.getDayOfMonth()
|
||||
);
|
||||
}
|
||||
|
||||
private LocalTime getLocalTime(OffsetDateTime fullTime)
|
||||
{
|
||||
return LocalTime.
|
||||
of(fullTime.getHour(),
|
||||
fullTime.getMinute(),
|
||||
fullTime.getSecond(),
|
||||
fullTime.getNano());
|
||||
}
|
||||
|
||||
private LocalTime getLocalTime(LocalTime fullTime)
|
||||
{
|
||||
return LocalTime.of(fullTime.getHour(),
|
||||
fullTime.getMinute(),
|
||||
fullTime.getSecond(),
|
||||
fullTime.getNano());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.siyeh.ig.redundancy;
|
||||
|
||||
import com.intellij.codeInspection.InspectionsBundle;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
|
||||
public class RedundantCreationJavaTimeInspectionTest extends LightJavaCodeInsightFixtureTestCase {
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/java/java-tests/testData/ig/com/siyeh/igtest/redundancy/redundant_creation_java_time/";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
myFixture.enableInspections(new RedundantCreationJavaTimeInspection());
|
||||
}
|
||||
|
||||
public void testPreview() {
|
||||
doTest("Simplify creation of 'java.time' object");
|
||||
}
|
||||
|
||||
public void testRedundantCreationOfDateTime() {
|
||||
doTest(null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected LightProjectDescriptor getProjectDescriptor() {
|
||||
return JAVA_21;
|
||||
}
|
||||
|
||||
private void doTest(@Nullable String quickFixName) {
|
||||
myFixture.configureByFile(getTestDataPath() + "before" + getTestName(false) + ".java");
|
||||
if (quickFixName != null) {
|
||||
myFixture.checkPreviewAndLaunchAction(myFixture.findSingleIntention(quickFixName));
|
||||
}
|
||||
else {
|
||||
myFixture.launchAction(myFixture.findSingleIntention(InspectionsBundle.message("fix.all.inspection.problems.in.file",
|
||||
InspectionGadgetsBundle.message(
|
||||
"inspection.redundant.creation.java.time.display.name"))));
|
||||
}
|
||||
myFixture.checkResultByFile("after" + getTestName(false) + ".java");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user