Java Code Examples for com.android.tools.lint.detector.api.Location#Handle

The following examples show how to use com.android.tools.lint.detector.api.Location#Handle . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LintDriver.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public Location.Handle createResourceItemHandle(@NonNull ResourceItem item) {
    return mDelegate.createResourceItemHandle(item);
}
 
Example 2
Source File: MissingClassDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {
    if (!context.getProject().isLibrary() && mHaveClasses
            && mReferencedClasses != null && !mReferencedClasses.isEmpty()
            && context.getDriver().getScope().contains(Scope.CLASS_FILE)) {
        List<String> classes = new ArrayList<String>(mReferencedClasses.keySet());
        Collections.sort(classes);
        for (String owner : classes) {
            Location.Handle handle = mReferencedClasses.get(owner);
            String fqcn = ClassContext.getFqcn(owner);

            String signature = ClassContext.getInternalName(fqcn);
            if (!signature.equals(owner)) {
                if (!mReferencedClasses.containsKey(signature)) {
                    continue;
                }
            } else if (signature.indexOf('$') != -1) {
                signature = signature.replace('$', '/');
                if (!mReferencedClasses.containsKey(signature)) {
                    continue;
                }
            }
            mReferencedClasses.remove(owner);

            // Ignore usages of platform libraries
            if (owner.startsWith("android/")) { //$NON-NLS-1$
                continue;
            }

            String message = String.format(
                    "Class referenced in the manifest, `%1$s`, was not found in the " +
                            "project or the libraries", fqcn);
            Location location = handle.resolve();
            File parentFile = location.getFile().getParentFile();
            if (parentFile != null) {
                String parent = parentFile.getName();
                ResourceFolderType type = ResourceFolderType.getFolderType(parent);
                if (type == LAYOUT) {
                    message = String.format(
                        "Class referenced in the layout file, `%1$s`, was not found in "
                            + "the project or the libraries", fqcn);
                } else if (type == XML) {
                    message = String.format(
                            "Class referenced in the preference header file, `%1$s`, was not "
                                    + "found in the project or the libraries", fqcn);

                } else if (type == VALUES) {
                    message = String.format(
                            "Class referenced in the analytics file, `%1$s`, was not "
                                    + "found in the project or the libraries", fqcn);
                }
            }

            context.report(MISSING, location, message);
        }
    }
}
 
Example 3
Source File: OnClickDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void visitAttribute(@NonNull XmlContext context, @NonNull Attr attribute) {
    String value = attribute.getValue();
    if (value.isEmpty() || value.trim().isEmpty()) {
        context.report(ISSUE, attribute, context.getLocation(attribute),
                "`onClick` attribute value cannot be empty");
    } else if (!value.equals(value.trim())) {
        context.report(ISSUE, attribute, context.getLocation(attribute),
                "There should be no whitespace around attribute values");
    } else if (!value.startsWith(PREFIX_RESOURCE_REF)) { // Not resolved
        if (!context.getProject().getReportIssues()) {
            // If this is a library project not being analyzed, ignore it
            return;
        }

        if (mNames == null) {
            mNames = new HashMap<String, Location.Handle>();
        }
        Handle handle = context.createLocationHandle(attribute);
        handle.setClientData(attribute);

        // Replace unicode characters with the actual value since that's how they
        // appear in the ASM signatures
        if (value.contains("\\u")) { //$NON-NLS-1$
            Pattern pattern = Pattern.compile("\\\\u(\\d\\d\\d\\d)"); //$NON-NLS-1$
            Matcher matcher = pattern.matcher(value);
            StringBuilder sb = new StringBuilder(value.length());
            int remainder = 0;
            while (matcher.find()) {
                sb.append(value.substring(0, matcher.start()));
                String unicode = matcher.group(1);
                int hex = Integer.parseInt(unicode, 16);
                sb.append((char) hex);
                remainder = matcher.end();
            }
            sb.append(value.substring(remainder));
            value = sb.toString();
        }

        mNames.put(value, handle);
    }
}
 
Example 4
Source File: NegativeMarginDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private void checkMarginValue(
        @NonNull XmlContext context,
        @NonNull String value,
        @Nullable Node scope,
        @Nullable Location.Handle handle) {
    if (isNegativeDimension(value)) {
        String message = "Margin values should not be negative";
        if (scope != null) {
            context.report(ISSUE, scope, context.getLocation(scope), message);
        } else {
            assert handle != null;
            context.report(ISSUE, handle.resolve(), message);
        }
    } else if (value.startsWith(DIMEN_PREFIX) && scope != null) {
        ResourceUrl url = ResourceUrl.parse(value);
        if (url == null) {
            return;
        }
        if (context.getClient().supportsProjectResources()) {
            // Typically interactive IDE usage, where we are only analyzing a single file,
            // but we can use the IDE to resolve resource URLs
            LintClient client = context.getClient();
            Project project = context.getProject();
            AbstractResourceRepository resources = client.getProjectResources(project, true);
            if (resources != null) {
                List<ResourceItem> items = resources.getResourceItem(url.type, url.name);
                if (items != null) {
                    for (ResourceItem item : items) {
                        ResourceValue resourceValue = item.getResourceValue(false);
                        if (resourceValue != null) {
                            String dimenValue = resourceValue.getValue();
                            if (dimenValue != null && isNegativeDimension(dimenValue)) {
                                ResourceFile sourceFile = item.getSource();
                                assert sourceFile != null;
                                String message = String.format(
                                        "Margin values should not be negative "
                                                + "(`%1$s` is defined as `%2$s` in `%3$s`",
                                        value, dimenValue, sourceFile.getFile());
                                context.report(ISSUE, scope,
                                        context.getLocation(scope),
                                        message);
                                break;
                            }
                        }
                    }
                }
            }
        } else if (!context.getDriver().isSuppressed(context, ISSUE, scope)) {
            // Batch mode where we process layouts then values in order
            if (mDimenUsage == null) {
                mDimenUsage = new HashMap<String, Location.Handle>();
            }
            mDimenUsage.put(url.name, context.createLocationHandle(scope));
        }
    }
}
 
Example 5
Source File: EcjParser.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public
Location.Handle createLocationHandle(@NonNull JavaContext context, @NonNull Node node) {
    return new LocationHandle(context.file, node);
}
 
Example 6
Source File: JavaParser.java    From javaide with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a light-weight handle to a location for the given node. It can be
 * turned into a full fledged location by
 * {@link com.android.tools.lint.detector.api.Location.Handle#resolve()}.
 *
 * @param context the context providing the node
 * @param node the node (element or attribute) to create a location handle
 *            for
 * @return a location handle
 */
@NonNull
public abstract Location.Handle createLocationHandle(@NonNull JavaContext context,
        @NonNull Node node);
 
Example 7
Source File: XmlParser.java    From javaide with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a light-weight handle to a location for the given node. It can be
 * turned into a full fledged location by
 * {@link com.android.tools.lint.detector.api.Location.Handle#resolve()}.
 *
 * @param context the context providing the node
 * @param node the node (element or attribute) to create a location handle
 *            for
 * @return a location handle
 */
@NonNull
public abstract Location.Handle createLocationHandle(@NonNull XmlContext context,
        @NonNull Node node);
 
Example 8
Source File: LintClient.java    From javaide with GNU General Public License v3.0 2 votes vote down vote up
/**
 * For a lint client which supports resource items (via {@link #supportsProjectResources()})
 * return a handle for a resource item
 *
 * @param item the resource item to look up a location handle for
 * @return a corresponding handle
 */
@NonNull
public Location.Handle createResourceItemHandle(@NonNull ResourceItem item) {
    return new Location.ResourceItemHandle(item);
}