Lint: Android Lint diagnostics fixes
This commit is contained in:
committed by
Yan Zhulanow
parent
c2ddd943f9
commit
2be9a083ad
+25
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* 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.android.tools.klint.client.api;
|
||||
|
||||
import com.android.annotations.Nullable;
|
||||
import com.intellij.psi.PsiElement;
|
||||
|
||||
public interface ExternalReferenceExpression {
|
||||
@Nullable
|
||||
PsiElement resolve(PsiElement context);
|
||||
}
|
||||
@@ -248,8 +248,7 @@ public abstract class JavaEvaluator {
|
||||
public abstract PsiClassType getClassType(@Nullable PsiClass psiClass);
|
||||
|
||||
@NonNull
|
||||
public abstract PsiAnnotation[] getAllAnnotations(@NonNull PsiModifierListOwner owner,
|
||||
boolean inHierarchy);
|
||||
public abstract PsiAnnotation[] getAllAnnotations(@NonNull PsiModifierListOwner owner);
|
||||
|
||||
@Nullable
|
||||
public abstract PsiAnnotation findAnnotationInHierarchy(
|
||||
|
||||
@@ -504,6 +504,17 @@ public class UElementVisitor {
|
||||
}
|
||||
|
||||
private class DispatchPsiVisitor extends AbstractUastVisitor {
|
||||
|
||||
@Override
|
||||
public boolean visitAnnotation(UAnnotation node) {
|
||||
List<VisitingDetector> list = mNodePsiTypeDetectors.get(UAnnotation.class);
|
||||
if (list != null) {
|
||||
for (VisitingDetector v : list) {
|
||||
v.getVisitor().visitAnnotation(node);
|
||||
}
|
||||
}
|
||||
return super.visitAnnotation(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visitCatchClause(UCatchClause node) {
|
||||
|
||||
@@ -34,15 +34,7 @@ import com.intellij.psi.PsiParameter;
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.intellij.psi.PsiVariable;
|
||||
|
||||
import org.jetbrains.uast.UElement;
|
||||
import org.jetbrains.uast.UExpression;
|
||||
import org.jetbrains.uast.ULiteralExpression;
|
||||
import org.jetbrains.uast.UMethod;
|
||||
import org.jetbrains.uast.UQualifiedReferenceExpression;
|
||||
import org.jetbrains.uast.UResolvable;
|
||||
import org.jetbrains.uast.USimpleNameReferenceExpression;
|
||||
import org.jetbrains.uast.UVariable;
|
||||
import org.jetbrains.uast.UastUtils;
|
||||
import org.jetbrains.uast.*;
|
||||
import org.jetbrains.uast.expressions.UReferenceExpression;
|
||||
import org.jetbrains.uast.java.JavaAbstractUExpression;
|
||||
import org.jetbrains.uast.java.JavaUVariableDeclarationsExpression;
|
||||
@@ -51,6 +43,16 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class UastLintUtils {
|
||||
@Nullable
|
||||
public static PsiElement resolve(ExternalReferenceExpression expression, UElement context) {
|
||||
UDeclaration declaration = UastUtils.getParentOfType(context, UDeclaration.class);
|
||||
if (declaration == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return expression.resolve(declaration.getPsi());
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static String getClassName(PsiClassType type) {
|
||||
PsiClass psiClass = type.resolve();
|
||||
@@ -88,10 +90,8 @@ public class UastLintUtils {
|
||||
(variable instanceof PsiLocalVariable || variable instanceof PsiParameter)) {
|
||||
UMethod containingFunction = UastUtils.getContainingUMethod(call);
|
||||
if (containingFunction != null) {
|
||||
ConstantEvaluator.LastAssignmentFinder
|
||||
finder = new ConstantEvaluator.LastAssignmentFinder(
|
||||
variable, call, context, null,
|
||||
(variable instanceof PsiParameter) ? 1 : 0);
|
||||
ConstantEvaluator.LastAssignmentFinder finder =
|
||||
new ConstantEvaluator.LastAssignmentFinder(variable, call, context, null, -1);
|
||||
containingFunction.accept(finder);
|
||||
lastAssignment = finder.getLastAssignment();
|
||||
}
|
||||
|
||||
+24
-41
@@ -63,21 +63,7 @@ import com.intellij.psi.PsiVariable;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
|
||||
import org.jetbrains.uast.UBinaryExpression;
|
||||
import org.jetbrains.uast.UBinaryExpressionWithType;
|
||||
import org.jetbrains.uast.UBlockExpression;
|
||||
import org.jetbrains.uast.UCallExpression;
|
||||
import org.jetbrains.uast.UElement;
|
||||
import org.jetbrains.uast.UExpression;
|
||||
import org.jetbrains.uast.UIfExpression;
|
||||
import org.jetbrains.uast.ULiteralExpression;
|
||||
import org.jetbrains.uast.UParenthesizedExpression;
|
||||
import org.jetbrains.uast.UPrefixExpression;
|
||||
import org.jetbrains.uast.UResolvable;
|
||||
import org.jetbrains.uast.UVariable;
|
||||
import org.jetbrains.uast.UastBinaryOperator;
|
||||
import org.jetbrains.uast.UastContext;
|
||||
import org.jetbrains.uast.UastPrefixOperator;
|
||||
import org.jetbrains.uast.*;
|
||||
import org.jetbrains.uast.expressions.UReferenceExpression;
|
||||
import org.jetbrains.uast.util.UastExpressionUtils;
|
||||
import org.jetbrains.uast.visitor.AbstractUastVisitor;
|
||||
@@ -1073,7 +1059,6 @@ public class ConstantEvaluator {
|
||||
private final PsiVariable mVariable;
|
||||
private final UElement mEndAt;
|
||||
|
||||
private final JavaContext mContext;
|
||||
private final ConstantEvaluator mConstantEvaluator;
|
||||
|
||||
private boolean mDone = false;
|
||||
@@ -1092,7 +1077,6 @@ public class ConstantEvaluator {
|
||||
mEndAt = endAt;
|
||||
UExpression initializer = context.getUastContext().getInitializerBody(variable);
|
||||
mLastAssignment = initializer;
|
||||
mContext = context;
|
||||
mConstantEvaluator = constantEvaluator;
|
||||
if (initializer != null && constantEvaluator != null) {
|
||||
mCurrentValue = constantEvaluator.evaluate(initializer);
|
||||
@@ -1112,7 +1096,7 @@ public class ConstantEvaluator {
|
||||
|
||||
@Override
|
||||
public boolean visitElement(UElement node) {
|
||||
if (!(node instanceof UBlockExpression)) {
|
||||
if (elementHasLevel(node)) {
|
||||
mCurrentLevel++;
|
||||
}
|
||||
if (node.equals(mEndAt)) {
|
||||
@@ -1123,23 +1107,23 @@ public class ConstantEvaluator {
|
||||
|
||||
@Override
|
||||
public boolean visitVariable(UVariable node) {
|
||||
if (mVariableLevel < 0 && node.equals(mVariable)) {
|
||||
if (mVariableLevel < 0 && node.getPsi().isEquivalentTo(mVariable)) {
|
||||
mVariableLevel = mCurrentLevel;
|
||||
}
|
||||
|
||||
|
||||
return super.visitVariable(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterVisitBinaryExpression(UBinaryExpression node) {
|
||||
if (!mDone
|
||||
&& node.getOperator() instanceof UastBinaryOperator.AssignOperator
|
||||
&& mVariableLevel >= 0) {
|
||||
if (!mDone
|
||||
&& node.getOperator() instanceof UastBinaryOperator.AssignOperator
|
||||
&& mVariableLevel >= 0) {
|
||||
UExpression leftOperand = node.getLeftOperand();
|
||||
UastBinaryOperator operator = node.getOperator();
|
||||
|
||||
if (!(operator instanceof UastBinaryOperator.AssignOperator)
|
||||
|| !(leftOperand instanceof UResolvable)) {
|
||||
|| !(leftOperand instanceof UResolvable)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1148,39 +1132,38 @@ public class ConstantEvaluator {
|
||||
return;
|
||||
}
|
||||
|
||||
// Stop search if we see an assignment inside some conditional or loop statement.
|
||||
if (mCurrentLevel > mVariableLevel) {
|
||||
// Last assignment is unknown if we see an assignment inside
|
||||
// some conditional or loop statement.
|
||||
if (mCurrentLevel > mVariableLevel + 1) {
|
||||
mLastAssignment = null;
|
||||
mCurrentValue = null;
|
||||
mDone = true;
|
||||
return;
|
||||
}
|
||||
|
||||
UExpression rightOperand = node.getRightOperand();
|
||||
ConstantEvaluator constantEvaluator = mConstantEvaluator;
|
||||
|
||||
Object newExpression = (constantEvaluator != null)
|
||||
? constantEvaluator.evaluate(rightOperand)
|
||||
: null;
|
||||
|
||||
//TODO implement other assign operators
|
||||
if (node.getOperator() == UastBinaryOperator.ASSIGN) {
|
||||
mCurrentValue = newExpression;
|
||||
mLastAssignment = rightOperand;
|
||||
} else {
|
||||
mCurrentValue = newExpression;
|
||||
// Technically wrong, just reflect the old behaviour for now
|
||||
mLastAssignment = rightOperand;
|
||||
}
|
||||
mCurrentValue = (constantEvaluator != null)
|
||||
? constantEvaluator.evaluate(rightOperand)
|
||||
: null;
|
||||
mLastAssignment = rightOperand;
|
||||
}
|
||||
|
||||
super.afterVisitBinaryExpression(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterVisitElement(UElement node) {
|
||||
if (!(node instanceof UBlockExpression)) {
|
||||
if (elementHasLevel(node)) {
|
||||
mCurrentLevel--;
|
||||
}
|
||||
super.afterVisitElement(node);
|
||||
}
|
||||
|
||||
private static boolean elementHasLevel(UElement node) {
|
||||
return !(node instanceof UBlockExpression
|
||||
|| node instanceof UVariableDeclarationsExpression);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -478,7 +478,12 @@ public class JavaContext extends Context {
|
||||
}
|
||||
|
||||
public boolean isSuppressedWithComment(@NonNull UElement scope, @NonNull Issue issue) {
|
||||
return false;
|
||||
if (!(scope instanceof PsiElementBacked)) {
|
||||
return false;
|
||||
}
|
||||
PsiElement psi = ((PsiElementBacked) scope).getPsi();
|
||||
return psi != null && isSuppressedWithComment(psi, issue);
|
||||
|
||||
}
|
||||
|
||||
public boolean isSuppressedWithComment(@NonNull PsiElement scope, @NonNull Issue issue) {
|
||||
|
||||
+1
-1
@@ -589,7 +589,7 @@ public class ResourceEvaluator {
|
||||
if (mEvaluator == null) {
|
||||
return null;
|
||||
}
|
||||
for (PsiAnnotation annotation : mEvaluator.getAllAnnotations(owner, true)) {
|
||||
for (PsiAnnotation annotation : mEvaluator.getAllAnnotations(owner)) {
|
||||
String signature = annotation.getQualifiedName();
|
||||
if (signature == null) {
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user