Search in sources :

Example 26 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 27 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)

Example 28 with Nullable

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

the class ApiLookup method get.

/**
     * Returns an instance of the API database
     *
     * @param client the client to associate with this database - used only for
     *            logging. The database object may be shared among repeated invocations,
     *            and in that case client used will be the one originally passed in.
     *            In other words, this parameter may be ignored if the client created
     *            is not new.
     * @return a (possibly shared) instance of the API database, or null
     *         if its data can't be found
     */
@Nullable
public static ApiLookup get(@NonNull LintClient client) {
    synchronized (ApiLookup.class) {
        ApiLookup db = sInstance.get();
        if (db == null) {
            File file = client.findResource(XML_FILE_PATH);
            if (file == null) {
                // AOSP build environment?
                //$NON-NLS-1$
                String build = System.getenv("ANDROID_BUILD_TOP");
                if (build != null) {
                    file = new File(build, //$NON-NLS-1$
                    "development/sdk/api-versions.xml".replace('/', File.separatorChar));
                }
            }
            if (file == null || !file.exists()) {
                return null;
            } else {
                db = get(client, file);
            }
            sInstance = new WeakReference<ApiLookup>(db);
        }
        return db;
    }
}
Also used : File(java.io.File) Nullable(com.android.annotations.Nullable)

Example 29 with Nullable

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

the class LintIdeViewTypeDetector method getViewTags.

@Nullable
@Override
protected Collection<String> getViewTags(@NonNull Context context, @NonNull ResourceItem item) {
    AbstractResourceRepository projectResources = context.getClient().getProjectResources(context.getMainProject(), true);
    assert projectResources instanceof LocalResourceRepository : projectResources;
    LocalResourceRepository repository = (LocalResourceRepository) projectResources;
    String viewTag = repository.getViewTag(item);
    if (viewTag != null) {
        return Collections.singleton(viewTag);
    }
    return super.getViewTags(context, item);
}
Also used : AbstractResourceRepository(com.android.ide.common.res2.AbstractResourceRepository) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) Nullable(com.android.annotations.Nullable)

Example 30 with Nullable

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

the class LintIdeJavaParser method getType.

@Nullable
@Override
public TypeDescriptor getType(@NonNull JavaContext context, @NonNull Node node) {
    final PsiElement element = getPsiElement(node);
    if (element == null) {
        return null;
    }
    Application application = ApplicationManager.getApplication();
    if (application.isReadAccessAllowed()) {
        return getTypeDescriptor(element);
    }
    return application.runReadAction(new Computable<TypeDescriptor>() {

        @Nullable
        @Override
        public TypeDescriptor compute() {
            return getTypeDescriptor(element);
        }
    });
}
Also used : Application(com.intellij.openapi.application.Application) Nullable(com.android.annotations.Nullable) 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