1/* 2 * Copyright (C) 2011 The Android Open Source Project 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 17package com.android.tools.lint.detector.api; 18 19import com.android.annotations.NonNull; 20import com.android.annotations.Nullable; 21import com.android.tools.lint.client.api.IJavaParser; 22import com.android.tools.lint.client.api.LintDriver; 23 24import java.io.File; 25 26import lombok.ast.Node; 27 28/** 29 * A {@link Context} used when checking Java files. 30 * <p/> 31 * <b>NOTE: This is not a public or final API; if you rely on this be prepared 32 * to adjust your code for the next tools release.</b> 33 */ 34public class JavaContext extends Context { 35 /** The parse tree */ 36 public Node compilationUnit; 37 /** The parser which produced the parse tree */ 38 public IJavaParser parser; 39 40 /** 41 * Constructs a {@link JavaContext} for running lint on the given file, with 42 * the given scope, in the given project reporting errors to the given 43 * client. 44 * 45 * @param driver the driver running through the checks 46 * @param project the project to run lint on which contains the given file 47 * @param main the main project if this project is a library project, or 48 * null if this is not a library project. The main project is 49 * the root project of all library projects, not necessarily the 50 * directly including project. 51 * @param file the file to be analyzed 52 */ 53 public JavaContext( 54 @NonNull LintDriver driver, 55 @NonNull Project project, 56 @Nullable Project main, 57 @NonNull File file) { 58 super(driver, project, main, file); 59 } 60 61 /** 62 * Returns a location for the given node 63 * 64 * @param node the AST node to get a location for 65 * @return a location for the given node 66 */ 67 @NonNull 68 public Location getLocation(@NonNull Node node) { 69 if (parser != null) { 70 return parser.getLocation(this, node); 71 } 72 73 return new Location(file, null, null); 74 } 75 76 @Override 77 public void report(@NonNull Issue issue, @Nullable Location location, 78 @NonNull String message, @Nullable Object data) { 79 if (mDriver.isSuppressed(issue, compilationUnit)) { 80 return; 81 } 82 super.report(issue, location, message, data); 83 } 84 85 /** 86 * Reports an issue applicable to a given AST node. The AST node is used as the 87 * scope to check for suppress lint annotations. 88 * 89 * @param issue the issue to report 90 * @param scope the AST node scope the error applies to. The lint infrastructure 91 * will check whether there are suppress annotations on this node (or its enclosing 92 * nodes) and if so suppress the warning without involving the client. 93 * @param location the location of the issue, or null if not known 94 * @param message the message for this warning 95 * @param data any associated data, or null 96 */ 97 public void report( 98 @NonNull Issue issue, 99 @Nullable Node scope, 100 @Nullable Location location, 101 @NonNull String message, 102 @Nullable Object data) { 103 if (scope != null && mDriver.isSuppressed(issue, scope)) { 104 return; 105 } 106 super.report(issue, location, message, data); 107 } 108} 109