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 17 package com.android.tools.lint; 18 19 import com.android.tools.lint.client.api.LintClient; 20 import com.android.tools.lint.detector.api.Issue; 21 import com.android.tools.lint.detector.api.Location; 22 import com.android.tools.lint.detector.api.Project; 23 import com.android.tools.lint.detector.api.Severity; 24 25 import java.io.File; 26 27 /** 28 * A {@link Warning} represents a specific warning that a {@link LintClient} 29 * has been told about. The context stores these as they are reported into a 30 * list of warnings such that it can sort them all before presenting them all at 31 * the end. 32 */ 33 class Warning implements Comparable<Warning> { 34 public final Issue issue; 35 public final String message; 36 public final Severity severity; 37 public final Object data; 38 public final Project project; 39 public Location location; 40 public File file; 41 public String path; 42 public int line = -1; 43 public int offset = -1; 44 public String errorLine; 45 public String fileContents; 46 Warning(Issue issue, String message, Severity severity, Project project, Object data)47 public Warning(Issue issue, String message, Severity severity, Project project, Object data) { 48 this.issue = issue; 49 this.message = message; 50 this.severity = severity; 51 this.project = project; 52 this.data = data; 53 } 54 55 // ---- Implements Comparable<Warning> ---- 56 @Override compareTo(Warning other)57 public int compareTo(Warning other) { 58 // Sort by category, then by priority, then by id, 59 // then by file, then by line 60 int categoryDelta = issue.getCategory().compareTo(other.issue.getCategory()); 61 if (categoryDelta != 0) { 62 return categoryDelta; 63 } 64 // DECREASING priority order 65 int priorityDelta = other.issue.getPriority() - issue.getPriority(); 66 if (priorityDelta != 0) { 67 return priorityDelta; 68 } 69 String id1 = issue.getId(); 70 String id2 = other.issue.getId(); 71 if (id1 == null || id2 == null) { 72 return file.getName().compareTo(other.file.getName()); 73 } 74 int idDelta = id1.compareTo(id2); 75 if (idDelta != 0) { 76 return idDelta; 77 } 78 if (file != null && other.file != null) { 79 int fileDelta = file.getName().compareTo( 80 other.file.getName()); 81 if (fileDelta != 0) { 82 return fileDelta; 83 } 84 } 85 if (line != other.line) { 86 return line - other.line; 87 } 88 89 return message.compareTo(other.message); 90 } 91 }