@Override public void run(@NonNull Context context) { String contents = context.getContents(); if (contents != null) { if (context.isEnabled(WRONG_KEEP)) { int index = contents.indexOf( " public <init>(android."); //$NON-NLS-1$ if (index != -1) { context.report(WRONG_KEEP, Location.create(context.file, contents, index, index), "Obsolete ProGuard file; use `-keepclasseswithmembers` instead of " + if (context.isEnabled(SPLIT_CONFIG)) { int index = contents.indexOf("-keep public class * extends android.app.Activity"); if (index != -1) { return; String properties = context.getClient().readFile(propertyFile); int i = properties.indexOf(PROGUARD_CONFIG); if (i == -1) { context.report(SPLIT_CONFIG, Location.create(context.file, contents, index, index), String.format(
@Override public void afterCheckProject(@NonNull Context context) { if (!mSeenAllowBackup && context.isEnabled(ALLOW_BACKUP) && !context.getProject().isLibrary() && context.getMainProject().getMinSdk() >= 4) { Location location = getMainApplicationTagLocation(context); context.report(ALLOW_BACKUP, location, "Should explicitly set `android:allowBackup` to `true` or " + "`false` (it's `true` by default, and that can have some security " + "implications for the application's data)"); } if (!context.getMainProject().isLibrary() && !mSeenAppIcon && context.isEnabled(APPLICATION_ICON)) { Location location = getMainApplicationTagLocation(context); context.report(APPLICATION_ICON, location, "Should explicitly set `android:icon`, there is no default"); } }
private static void report(Context context, Issue issue, Handle handle, String message) { Location location = handle.resolve(); Object clientData = handle.getClientData(); if (clientData instanceof Node) { if (context.getDriver().isSuppressed(null, issue, (Node) clientData)) { return; } } context.report(issue, location, message); }
/** Returns whether this file contains any suppress comment markers */ public boolean containsCommentSuppress() { if (containsCommentSuppress == null) { containsCommentSuppress = false; String prefix = getSuppressCommentPrefix(); if (prefix != null) { CharSequence contents = getContents(); if (contents != null) { containsCommentSuppress = indexOf(contents, prefix) != -1; } } } return containsCommentSuppress; }
@Override public void run(Context context) { String contents = context.getContents(); if (contents != null) { int index = contents.indexOf( // Old pattern: "-keepclasseswithmembernames class * {\n" + //$NON-NLS-1$ " public <init>(android."); //$NON-NLS-1$ if (index != -1) { context.client.report(context, ISSUE, context.getLocation(context), "Obsolete proguard file; use -keepclasseswithmembers instead of -keepclasseswithmembernames", null); } } }
@Override public void afterCheckProject(@NonNull Context context) { // Don't report issues on libraries if (context.getProject() == context.getMainProject() && !context.getMainProject().isLibrary() && mApplicationTagHandle != null) { if (!mSuppress && mHasNativeLibs) { context.report(ISSUE, mApplicationTagHandle.resolve(), "Missing attribute android:extractNativeLibs=\"false\"" + " on the `<application>` tag."); } } }
/** * Constructs a new {@link DefaultLocationHandle} * * @param context the context pointing to the file and its contents * @param startOffset the start offset within the file * @param endOffset the end offset within the file */ public DefaultLocationHandle(@NonNull Context context, int startOffset, int endOffset) { file = context.file; contents = context.getContents(); this.startOffset = startOffset; this.endOffset = endOffset; }
@Override public void afterCheckLibraryProject(@NonNull Context context) { if (!context.getProject().getReportIssues()) { // If this is a library project not being analyzed, ignore it return; } checkResourceFolder(context, context.getProject()); }
@Override public void run(@NonNull Context context) { if (!context.getProject().getReportIssues()) { CharSequence source = context.getContents(); if (source == null) { return; || startsWith(source, ">>>>>>> ", offset)) { Location location = Location.create(context.file, source, offset, offset + 7); context.report(ISSUE, location, "Missing merge marker?");
@NonNull String message, @NonNull TextFormat format) { assert context.isEnabled(issue) || issue == LINT_ERROR; Warning warning = new Warning(issue, message, severity, context.getProject()); warnings.add(warning); File file = location.getFile(); warning.file = file; warning.path = getDisplayPath(context.getProject(), file); warning.fileContents = context.getContents();
private void report(@NonNull Context context, @NonNull Object cookie, @NonNull Issue issue, @NonNull String message) { if (context.isEnabled(issue)) { // Suppressed? // Temporarily unconditionally checking for suppress comments in Gradle files // since Studio insists on an AndroidLint id prefix boolean checkComments = /*context.getClient().checkForSuppressComments() &&*/ context.containsCommentSuppress(); if (checkComments) { int startOffset = getStartOffset(context, cookie); if (startOffset >= 0 && context.isSuppressedWithComment(startOffset, issue)) { return; } } context.report(issue, createLocation(context, cookie), message); } }
if ("5.2.08".equals(dependency.getFullRevision()) && context.isEnabled(COMPATIBILITY)) { File sdkHome = context.getClient().getSdkHome(); File repository = SdkMavenRepository.GOOGLE.getRepositoryLocation(sdkHome, true); if (repository != null) { context.log(e, null); if (context.getScope().size() > 1 && context.isEnabled(REMOTE_VERSION)) { PreciseRevision latest = getLatestVersion(context, dependency, dependency.isPreview()); if (latest != null && isOlderThan(dependency, latest.getMajor(), latest.getMinor(),
private int resolveDimensionValue(String name, Context context){ LintClient client = context.getDriver().getClient(); LintProject project = context.getDriver().getProject(); AbstractResourceRepository resources = client.getProjectResources(project, true); return Integer.valueOf(resources.getResourceItem(ResourceType.DIMEN, name).get(0).getResourceValue(false).getValue()); }
@Override public void afterCheckProject(@NonNull Context context) { int phase = context.getPhase(); if (phase == 1 && mApplicableResources != null) { // We found resources for the string "Cancel"; perform a second pass // where we check layout text attributes against these strings. context.getDriver().requestRepeat(this, Scope.RESOURCE_FILE_SCOPE); } }
@Override public void afterCheckProject(@NonNull Context context) { if (context.getProject() == context.getMainProject() && // Full analysis? Don't tie check to any specific Gradle DSL element context.getScope().contains(Scope.ALL_RESOURCE_FILES)) { checkConsistentPlayServices(context, null); checkConsistentSupportLibraries(context, null); checkConsistentWearableLibraries(context, null); } }
private void checkClasses(Project project, List<File> binFolders, List<Detector> checks) { Context context = new Context(mClient, project, project.getDir(), mScope); fireEvent(EventType.SCANNING_FILE, context); for (Detector detector : checks) { ((Detector.ClassScanner) detector).checkJavaClasses(context); if (mCanceled) { return; } } }
@Override public void afterCheckFile(@NonNull Context context) { XmlContext xmlContext = (XmlContext) context; Element element = xmlContext.document.getDocumentElement(); if (element != null) { checkDocumentElement(xmlContext, element); } if (mSeenUsesSdk == 0 && context.isEnabled(USES_SDK) // Not required in Gradle projects; typically defined in build.gradle instead // and inserted at build time && !context.getMainProject().isGradleProject()) { context.report(USES_SDK, Location.create(context.file), "Manifest should specify a minimum API level with " + "`<uses-sdk android:minSdkVersion=\"?\" />`; if it really supports " + "all versions of Android set it to 1."); } }
if (bits == null) { try { bits = context.getClient().readBytes(file); fileContents.put(file, bits); } catch (IOException e) { context.log(e, null); lastName, sb.toString()); if (location != null) { context.report(DUPLICATES_CONFIGURATIONS, location, message); "The following unrelated icon files have identical contents: %1$s", sb.toString()); context.report(DUPLICATES_NAMES, location, message);
String prefix = getSuppressCommentPrefix(); if (prefix == null) { return false; CharSequence contents = getContents(); assert contents != null; // otherwise we wouldn't be here if (startOffset >= contents.length()) { return false; int index = findPrefixOnPreviousLine(contents, lineStart, prefix); if (index != -1 &&index+prefix.length() < lineStart) { String line = contents.subSequence(index + prefix.length(), lineStart).toString();
@Override public void beforeCheckFile(@NonNull Context context) { CharSequence source = context.getContents(); if (source == null) { return; context.report(BOM, location, message);