Search in sources :

Example 31 with Nullable

use of com.android.annotations.Nullable in project android by JetBrains.

the class GradleImport method resolveWorkspacePath.

@Nullable
public File resolveWorkspacePath(@Nullable EclipseProject fromProject, @NonNull String path, boolean record) {
    if (path.isEmpty()) {
        return null;
    }
    // If file within project, must match on all prefixes
    for (Map.Entry<String, File> entry : myPathMap.entrySet()) {
        String workspacePath = entry.getKey();
        File file = entry.getValue();
        if (file != null && path.startsWith(workspacePath)) {
            if (path.equals(workspacePath)) {
                return file;
            } else {
                path = path.substring(workspacePath.length());
                if (path.charAt(0) == '/' || path.charAt(0) == separatorChar) {
                    path = path.substring(1);
                }
                File resolved = new File(file, path.replace('/', separatorChar));
                if (resolved.exists()) {
                    return resolved;
                }
            }
        }
    }
    if (fromProject != null && myWorkspaceLocation == null) {
        guessWorkspace(fromProject.getDir());
    }
    if (myWorkspaceLocation != null) {
        // Is the file present directly in the workspace?
        char first = path.charAt(0);
        if (first != '/') {
            return null;
        }
        File f = new File(myWorkspaceLocation, path.substring(1).replace('/', separatorChar));
        if (f.exists()) {
            myPathMap.put(path, f);
            return f;
        }
        // workspace metadata
        if (myWorkspaceProjects == null) {
            myWorkspaceProjects = Maps.newHashMap();
            File projectDir = new File(myWorkspaceLocation, ".metadata" + separator + ".plugins" + separator + "org.eclipse.core.resources" + separator + ".projects");
            File[] projects = projectDir.exists() ? projectDir.listFiles() : null;
            byte[] target = "URI//file:".getBytes(Charsets.US_ASCII);
            if (projects != null) {
                for (File project : projects) {
                    File location = new File(project, ".location");
                    if (location.exists()) {
                        try {
                            byte[] bytes = Files.toByteArray(location);
                            int start = Bytes.indexOf(bytes, target);
                            if (start != -1) {
                                int end = start + target.length;
                                for (; end < bytes.length; end++) {
                                    if (bytes[end] == (byte) 0) {
                                        break;
                                    }
                                }
                                try {
                                    int length = end - start;
                                    String s = new String(bytes, start, length, UTF_8);
                                    // skip URI//
                                    s = s.substring(5);
                                    File file = SdkUtils.urlToFile(s);
                                    if (file.exists()) {
                                        String name = project.getName();
                                        myWorkspaceProjects.put('/' + name, file);
                                    //noinspection ConstantConditions
                                    }
                                } catch (Throwable t) {
                                // Ignore binary data we can't read
                                }
                            }
                        } catch (IOException e) {
                            reportWarning((ImportModule) null, location, "Can't read .location file");
                        }
                    }
                }
            }
        }
        // Is it just a project root?
        File project = myWorkspaceProjects.get(path);
        if (project != null) {
            myPathMap.put(path, project);
            return project;
        }
        // If file within project, must match on all prefixes
        for (Map.Entry<String, File> entry : myWorkspaceProjects.entrySet()) {
            String workspacePath = entry.getKey();
            File file = entry.getValue();
            if (file != null && path.startsWith(workspacePath)) {
                if (path.equals(workspacePath)) {
                    return file;
                } else {
                    path = path.substring(workspacePath.length());
                    if (path.charAt(0) == '/' || path.charAt(0) == separatorChar) {
                        path = path.substring(1);
                    }
                    File resolved = new File(file, path.replace('/', separatorChar));
                    if (resolved.exists()) {
                        return resolved;
                    }
                }
            }
        }
        // Record path as one we need to resolve
        if (record) {
            myPathMap.put(path, null);
        }
    } else if (record) {
        // Record path as one we need to resolve
        myPathMap.put(path, null);
    }
    return null;
}
Also used : IOException(java.io.IOException) File(java.io.File) Nullable(com.android.annotations.Nullable)

Example 32 with Nullable

use of com.android.annotations.Nullable in project android by JetBrains.

the class LayoutMetadata method getNodeBinding.

@Nullable
private static AdapterBinding getNodeBinding(@Nullable Object viewObject, @Nullable String header, @Nullable String footer, @Nullable String layout, int count) {
    if (layout != null || header != null || footer != null) {
        AdapterBinding binding = new AdapterBinding(count);
        if (header != null) {
            boolean isFramework = header.startsWith(ANDROID_LAYOUT_RESOURCE_PREFIX);
            binding.addHeader(new ResourceReference(stripLayoutPrefix(header), isFramework));
        }
        if (footer != null) {
            boolean isFramework = footer.startsWith(ANDROID_LAYOUT_RESOURCE_PREFIX);
            binding.addFooter(new ResourceReference(stripLayoutPrefix(footer), isFramework));
        }
        if (layout != null) {
            boolean isFramework = layout.startsWith(ANDROID_LAYOUT_RESOURCE_PREFIX);
            if (isFramework) {
                layout = layout.substring(ANDROID_LAYOUT_RESOURCE_PREFIX.length());
            } else if (layout.startsWith(LAYOUT_RESOURCE_PREFIX)) {
                layout = layout.substring(LAYOUT_RESOURCE_PREFIX.length());
            }
            binding.addItem(new DataBindingItem(layout, isFramework, 1));
        } else if (viewObject != null) {
            String listFqcn = LayoutlibCallbackImpl.getListAdapterViewFqcn(viewObject.getClass());
            if (listFqcn != null) {
                if (listFqcn.endsWith(EXPANDABLE_LIST_VIEW)) {
                    binding.addItem(new DataBindingItem(DEFAULT_EXPANDABLE_LIST_ITEM, true, /* isFramework */
                    1));
                } else {
                    binding.addItem(new DataBindingItem(DEFAULT_LIST_ITEM, true, /* isFramework */
                    1));
                }
            }
        } else {
            binding.addItem(new DataBindingItem(DEFAULT_LIST_ITEM, true, /* isFramework */
            1));
        }
        return binding;
    }
    return null;
}
Also used : AdapterBinding(com.android.ide.common.rendering.api.AdapterBinding) DataBindingItem(com.android.ide.common.rendering.api.DataBindingItem) ResourceReference(com.android.ide.common.rendering.api.ResourceReference) Nullable(com.android.annotations.Nullable)

Example 33 with Nullable

use of com.android.annotations.Nullable in project android by JetBrains.

the class StringEvaluator method evaluate.

/** Evaluates the given expression, with the given set of arguments */
@Nullable
public String evaluate(@NonNull String expression, @NonNull Map<String, Object> inputs) {
    try {
        myCurrentExpression = expression;
        Template inputsTemplate = myFreemarker.getTemplate(expression);
        StringWriter out = new StringWriter();
        Map<String, Object> args = FreemarkerUtils.createParameterMap(inputs);
        inputsTemplate.process(args, out);
        out.flush();
        return out.toString();
    } catch (Exception e) {
        return null;
    }
}
Also used : StringWriter(java.io.StringWriter) IOException(java.io.IOException) Template(freemarker.template.Template) Nullable(com.android.annotations.Nullable)

Example 34 with Nullable

use of com.android.annotations.Nullable in project kotlin by JetBrains.

the class PermissionRequirement method getAnnotationStringValues.

@Nullable
public static String[] getAnnotationStringValues(@Nullable UAnnotation annotation, @NonNull String name) {
    if (annotation != null) {
        UExpression attributeValue = annotation.findDeclaredAttributeValue(name);
        if (attributeValue == null && ATTR_VALUE.equals(name)) {
            attributeValue = annotation.findDeclaredAttributeValue(null);
        }
        if (attributeValue == null) {
            return null;
        }
        if (UastExpressionUtils.isArrayInitializer(attributeValue)) {
            List<UExpression> initializers = ((UCallExpression) attributeValue).getValueArguments();
            List<String> result = Lists.newArrayListWithCapacity(initializers.size());
            ConstantEvaluator constantEvaluator = new ConstantEvaluator(null);
            for (UExpression element : initializers) {
                Object o = constantEvaluator.evaluate(element);
                if (o instanceof String) {
                    result.add((String) o);
                }
            }
            if (result.isEmpty()) {
                return null;
            } else {
                return result.toArray(new String[0]);
            }
        } else {
            // Use constant evaluator since we want to resolve field references as well
            Object o = ConstantEvaluator.evaluate(null, attributeValue);
            if (o instanceof String) {
                return new String[] { (String) o };
            } else if (o instanceof String[]) {
                return (String[]) o;
            } else if (o instanceof Object[]) {
                Object[] array = (Object[]) o;
                List<String> strings = Lists.newArrayListWithCapacity(array.length);
                for (Object element : array) {
                    if (element instanceof String) {
                        strings.add((String) element);
                    }
                }
                return strings.toArray(new String[0]);
            }
        }
    }
    return null;
}
Also used : ConstantEvaluator(com.android.tools.klint.detector.api.ConstantEvaluator) UExpression(org.jetbrains.uast.UExpression) UCallExpression(org.jetbrains.uast.UCallExpression) Nullable(com.android.annotations.Nullable)

Example 35 with Nullable

use of com.android.annotations.Nullable in project kotlin by JetBrains.

the class PrivateResourceDetector method getLibraryName.

/** Pick a suitable name to describe the library defining the private resource */
@Nullable
private static String getLibraryName(@NonNull Context context, @NonNull ResourceType type, @NonNull String name) {
    ResourceVisibilityLookup lookup = context.getProject().getResourceVisibility();
    AndroidLibrary library = lookup.getPrivateIn(type, name);
    if (library != null) {
        String libraryName = library.getProject();
        if (libraryName != null) {
            return libraryName;
        }
        MavenCoordinates coordinates = library.getResolvedCoordinates();
        if (coordinates != null) {
            return coordinates.getGroupId() + ':' + coordinates.getArtifactId();
        }
    }
    return "the library";
}
Also used : MavenCoordinates(com.android.builder.model.MavenCoordinates) AndroidLibrary(com.android.builder.model.AndroidLibrary) ResourceVisibilityLookup(com.android.ide.common.repository.ResourceVisibilityLookup) Nullable(com.android.annotations.Nullable)

Aggregations

Nullable (com.android.annotations.Nullable)83 File (java.io.File)21 IOException (java.io.IOException)9 ResourceType (com.android.resources.ResourceType)8 PsiClass (com.intellij.psi.PsiClass)7 PsiElement (com.intellij.psi.PsiElement)7 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)7 ResourceUrl (com.android.ide.common.resources.ResourceUrl)6 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)6 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)6 PsiExpression (com.intellij.psi.PsiExpression)6 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)6 PsiMethod (com.intellij.psi.PsiMethod)6 PsiStatement (com.intellij.psi.PsiStatement)6 ArrayList (java.util.ArrayList)6 PsiField (com.intellij.psi.PsiField)5 PsiFile (com.intellij.psi.PsiFile)4 Node (lombok.ast.Node)4 UReferenceExpression (org.jetbrains.uast.UReferenceExpression)4 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)3