aboutsummaryrefslogtreecommitdiffstats
path: root/lint/libs/lint_checks/src/com/android/tools/lint/checks/ToastDetector.java
blob: f199438aa80f6497027007a7d7ca65a6b41def06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
 *
 * 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.lint.checks;

import com.android.tools.lint.detector.api.Category;
import com.android.tools.lint.detector.api.Context;
import com.android.tools.lint.detector.api.Detector;
import com.android.tools.lint.detector.api.Issue;
import com.android.tools.lint.detector.api.JavaContext;
import com.android.tools.lint.detector.api.Scope;
import com.android.tools.lint.detector.api.Severity;
import com.android.tools.lint.detector.api.Speed;

import java.io.File;
import java.util.Collections;
import java.util.List;

import lombok.ast.AstVisitor;
import lombok.ast.Expression;
import lombok.ast.ForwardingAstVisitor;
import lombok.ast.IntegralLiteral;
import lombok.ast.MethodDeclaration;
import lombok.ast.MethodInvocation;
import lombok.ast.Node;
import lombok.ast.Return;
import lombok.ast.StrictListAccessor;

/** Detector looking for Toast.makeText() without a corresponding show() call */
public class ToastDetector extends Detector implements Detector.JavaScanner {
    /** The main issue discovered by this detector */
    public static final Issue ISSUE = Issue.create(
            "ShowToast", //$NON-NLS-1$
            "Looks for code creating a Toast but forgetting to call show() on it",

            "Toast.makeText() creates a Toast but does *not* show it. You must call " +
            "show() on the resulting object to actually make the Toast appear.",

            Category.CORRECTNESS,
            6,
            Severity.WARNING,
            ToastDetector.class,
            Scope.JAVA_FILE_SCOPE);


    /** Constructs a new {@link ToastDetector} check */
    public ToastDetector() {
    }

    @Override
    public boolean appliesTo(Context context, File file) {
        return true;
    }


    @Override
    public Speed getSpeed() {
        return Speed.NORMAL;
    }

    // ---- Implements JavaScanner ----

    @Override
    public List<String> getApplicableMethodNames() {
        return Collections.singletonList("makeText"); //$NON-NLS-1$
    }

    private MethodDeclaration findSurroundingMethod(Node scope) {
        while (scope != null) {
            Class<? extends Node> type = scope.getClass();
            // The Lombok AST uses a flat hierarchy of node type implementation classes
            // so no need to do instanceof stuff here.
            if (type == MethodDeclaration.class) {
                return (MethodDeclaration) scope;
            }

            scope = scope.getParent();
        }

        return null;
    }

    @Override
    public void visitMethod(JavaContext context, AstVisitor visitor, MethodInvocation node) {
        assert node.astName().astValue().equals("makeText");
        if (node.astOperand() == null) {
            // "makeText()" in the code with no operand
            return;
        }

        String operand = node.astOperand().toString();
        if (!(operand.equals("Toast") || operand.endsWith(".Toast"))) {
            return;
        }

        // Make sure you pass the right kind of duration: it's not a delay, it's
        //  LENGTH_SHORT or LENGTH_LONG
        // (see http://code.google.com/p/android/issues/detail?id=3655)
        StrictListAccessor<Expression, MethodInvocation> args = node.astArguments();
        if (args.size() == 3) {
            Expression duration = args.last();
            if (duration instanceof IntegralLiteral) {
                context.report(ISSUE, context.getLocation(duration),
                        "Expected duration Toast.LENGTH_SHORT or Toast.LENGTH_LONG, a custom " +
                        "duration value is not supported",
                        null);
            }
        }

        MethodDeclaration method = findSurroundingMethod(node.getParent());
        if (method == null) {
            return;
        }

        ShowFinder finder = new ShowFinder(node);
        method.accept(finder);
        if (!finder.isShowCalled()) {
            context.report(ISSUE, method, context.getLocation(node),
                    "Toast created but not shown: did you forget to call show() ?", null);
        }
    }

    private class ShowFinder extends ForwardingAstVisitor {
        /** Whether we've found the show method */
        private boolean mFound;
        /** The target makeText call */
        private MethodInvocation mTarget;
        /** Whether we've seen the target makeText node yet */
        private boolean mSeenTarget;

        private ShowFinder(MethodInvocation target) {
            mTarget = target;
        }

        @Override
        public boolean visitMethodInvocation(MethodInvocation node) {
            if (node == mTarget) {
                mSeenTarget = true;
            } else if ((mSeenTarget || node.astOperand() == mTarget)
                    && "show".equals(node.astName().astValue())) { //$NON-NLS-1$
                // TODO: Do more flow analysis to see whether we're really calling show
                // on the right type of object?
                mFound = true;
            }

            return true;
        }

        @Override
        public boolean visitReturn(Return node) {
            if (node.astValue() == mTarget) {
                // If you just do "return Toast.makeText(...) don't warn
                mFound = true;
            }
            return super.visitReturn(node);
        }

        boolean isShowCalled() {
            return mFound;
        }
    }
}