Search in sources :

Example 41 with HashMap

use of com.intellij.util.containers.HashMap in project android by JetBrains.

the class ResourceTypeClassBase method buildResourceFields.

@NotNull
static PsiField[] buildResourceFields(@NotNull ResourceManager manager, boolean nonFinal, @NotNull String resClassName, @NotNull final PsiClass context) {
    ResourceType resourceType = ResourceType.getEnum(resClassName);
    if (resourceType == null) {
        return PsiField.EMPTY_ARRAY;
    }
    final Map<String, PsiType> fieldNames = new HashMap<String, PsiType>();
    final boolean styleable = ResourceType.STYLEABLE == resourceType;
    final PsiType basicType = styleable ? PsiType.INT.createArrayType() : PsiType.INT;
    for (String resName : manager.getResourceNames(resourceType)) {
        fieldNames.put(resName, basicType);
    }
    if (styleable) {
        for (ResourceEntry entry : manager.getValueResourceEntries(ResourceType.ATTR)) {
            final String resName = entry.getName();
            final String resContext = entry.getContext();
            if (resContext.length() > 0) {
                fieldNames.put(resContext + '_' + resName, PsiType.INT);
            }
        }
    }
    final PsiField[] result = new PsiField[fieldNames.size()];
    final PsiElementFactory factory = JavaPsiFacade.getElementFactory(context.getProject());
    int idIterator = resourceType.ordinal() * 100000;
    int i = 0;
    for (Map.Entry<String, PsiType> entry : fieldNames.entrySet()) {
        final String fieldName = AndroidResourceUtil.getFieldNameByResourceName(entry.getKey());
        final PsiType type = entry.getValue();
        final int id = -(idIterator++);
        final AndroidLightField field = new AndroidLightField(fieldName, context, type, !nonFinal, nonFinal ? null : id);
        field.setInitializer(factory.createExpressionFromText(Integer.toString(id), field));
        result[i++] = field;
    }
    return result;
}
Also used : HashMap(com.intellij.util.containers.HashMap) ResourceType(com.android.resources.ResourceType) ResourceEntry(org.jetbrains.android.util.ResourceEntry) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Example 42 with HashMap

use of com.intellij.util.containers.HashMap in project android by JetBrains.

the class AndroidPrecompileTask method checkArtifacts.

private static boolean checkArtifacts(@NotNull CompileContext context) {
    final Project project = context.getProject();
    final CompileScope scope = context.getCompileScope();
    final Set<Artifact> artifacts = ApplicationManager.getApplication().runReadAction(new Computable<Set<Artifact>>() {

        @Override
        public Set<Artifact> compute() {
            return ArtifactCompileScope.getArtifactsToBuild(project, scope, false);
        }
    });
    if (artifacts == null) {
        return true;
    }
    final Set<Artifact> debugArtifacts = new HashSet<>();
    final Set<Artifact> releaseArtifacts = new HashSet<>();
    final Map<AndroidFacet, List<Artifact>> facet2artifacts = new HashMap<>();
    for (final Artifact artifact : artifacts) {
        final ArtifactProperties<?> properties = artifact.getProperties(AndroidArtifactPropertiesProvider.getInstance());
        if (properties instanceof AndroidApplicationArtifactProperties) {
            final AndroidArtifactSigningMode mode = ((AndroidApplicationArtifactProperties) properties).getSigningMode();
            if (mode == AndroidArtifactSigningMode.DEBUG || mode == AndroidArtifactSigningMode.DEBUG_WITH_CUSTOM_CERTIFICATE) {
                debugArtifacts.add(artifact);
            } else {
                releaseArtifacts.add(artifact);
            }
        }
        final AndroidFacet facet = ApplicationManager.getApplication().runReadAction(new Computable<AndroidFacet>() {

            @Nullable
            @Override
            public AndroidFacet compute() {
                return AndroidArtifactUtil.getPackagedFacet(project, artifact);
            }
        });
        if (facet != null) {
            List<Artifact> list = facet2artifacts.get(facet);
            if (list == null) {
                list = new ArrayList<>();
                facet2artifacts.put(facet, list);
            }
            list.add(artifact);
        }
    }
    boolean success = true;
    if (debugArtifacts.size() > 0 && releaseArtifacts.size() > 0) {
        final String message = "Cannot build debug and release Android artifacts in the same session\n" + "Debug artifacts: " + toString(debugArtifacts) + "\n" + "Release artifacts: " + toString(releaseArtifacts);
        context.addMessage(CompilerMessageCategory.ERROR, message, null, -1, -1);
        success = false;
    }
    if (releaseArtifacts.size() > 0 && CompileStepBeforeRun.getRunConfiguration(context) != null) {
        final String message = "Cannot build release Android artifacts in the 'make before run' session\n" + "Release artifacts: " + toString(releaseArtifacts);
        context.addMessage(CompilerMessageCategory.ERROR, message, null, -1, -1);
        success = false;
    }
    for (Map.Entry<AndroidFacet, List<Artifact>> entry : facet2artifacts.entrySet()) {
        final List<Artifact> list = entry.getValue();
        final String moduleName = entry.getKey().getModule().getName();
        if (list.size() > 1) {
            final Artifact firstArtifact = list.get(0);
            final Object[] firstArtifactProGuardOptions = getProGuardOptions(firstArtifact);
            for (int i = 1; i < list.size(); i++) {
                final Artifact artifact = list.get(i);
                if (!Arrays.equals(getProGuardOptions(artifact), firstArtifactProGuardOptions)) {
                    context.addMessage(CompilerMessageCategory.ERROR, "Artifacts related to the same module '" + moduleName + "' have different ProGuard options: " + firstArtifact.getName() + ", " + artifact.getName(), null, -1, -1);
                    success = false;
                    break;
                }
            }
        }
    }
    return success;
}
Also used : HashSet(com.intellij.util.containers.hash.HashSet) HashMap(com.intellij.util.containers.HashMap) AndroidApplicationArtifactProperties(org.jetbrains.android.compiler.artifact.AndroidApplicationArtifactProperties) HashSet(com.intellij.util.containers.hash.HashSet) Artifact(com.intellij.packaging.artifacts.Artifact) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) ArtifactCompileScope(com.intellij.packaging.impl.compiler.ArtifactCompileScope) AndroidArtifactSigningMode(org.jetbrains.android.compiler.artifact.AndroidArtifactSigningMode) HashMap(com.intellij.util.containers.HashMap) Nullable(org.jetbrains.annotations.Nullable)

Example 43 with HashMap

use of com.intellij.util.containers.HashMap in project android by JetBrains.

the class AndroidMavenExecutor method generateResources.

public static Map<CompilerMessageCategory, List<String>> generateResources(final Module module) {
    MavenProjectsManager projectsManager = MavenProjectsManager.getInstance(module.getProject());
    final MavenRunnerParameters parameters = new MavenRunnerParameters(true, projectsManager.findProject(module).getDirectory(), Collections.singletonList("process-resources"), projectsManager.getExplicitProfiles());
    final Map<CompilerMessageCategory, List<String>> result = new HashMap<CompilerMessageCategory, List<String>>();
    result.put(CompilerMessageCategory.ERROR, new ArrayList<String>());
    try {
        JavaParameters javaParams = ApplicationManager.getApplication().runReadAction(new Computable<JavaParameters>() {

            @Nullable
            @Override
            public JavaParameters compute() {
                try {
                    return MavenExternalParameters.createJavaParameters(module.getProject(), parameters);
                } catch (ExecutionException e) {
                    LOG.info(e);
                    result.get(CompilerMessageCategory.ERROR).add(e.getMessage());
                    return null;
                }
            }
        });
        if (javaParams == null) {
            return result;
        }
        GeneralCommandLine commandLine = javaParams.toCommandLine();
        final StringBuildingOutputProcessor processor = new StringBuildingOutputProcessor();
        boolean success = AndroidUtils.executeCommand(commandLine, processor, WaitingStrategies.WaitForever.getInstance()) == ExecutionStatus.SUCCESS;
        String message = processor.getMessage();
        if (!success) {
            LOG.info(message);
            String lcmessage = message.toLowerCase();
            int buildErrorIndex = lcmessage.indexOf(BUILD_ERROR_INDICATOR);
            if (buildErrorIndex >= 0) {
                result.get(CompilerMessageCategory.ERROR).add(message.substring(buildErrorIndex));
            }
        }
    } catch (ExecutionException e) {
        LOG.info(e);
        result.get(CompilerMessageCategory.ERROR).add(e.getMessage());
    }
    return result;
}
Also used : CompilerMessageCategory(com.intellij.openapi.compiler.CompilerMessageCategory) MavenProjectsManager(org.jetbrains.idea.maven.project.MavenProjectsManager) HashMap(com.intellij.util.containers.HashMap) StringBuildingOutputProcessor(org.jetbrains.android.util.StringBuildingOutputProcessor) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) JavaParameters(com.intellij.execution.configurations.JavaParameters) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(com.intellij.execution.ExecutionException) Nullable(org.jetbrains.annotations.Nullable) MavenRunnerParameters(org.jetbrains.idea.maven.execution.MavenRunnerParameters)

Example 44 with HashMap

use of com.intellij.util.containers.HashMap in project android by JetBrains.

the class AndroidFindStyleApplicationsProcessor method isPossibleApplicationOfStyle.

private boolean isPossibleApplicationOfStyle(XmlTag candidate) {
    final DomElement domCandidate = DomManager.getDomManager(myProject).getDomElement(candidate);
    if (!(domCandidate instanceof LayoutViewElement)) {
        return false;
    }
    final LayoutViewElement candidateView = (LayoutViewElement) domCandidate;
    final Map<Pair<String, String>, String> attrsInCandidateMap = new HashMap<Pair<String, String>, String>();
    final List<XmlAttribute> attrsInCandidate = AndroidExtractStyleAction.getExtractableAttributes(candidate);
    if (attrsInCandidate.size() < myAttrMap.size()) {
        return false;
    }
    for (XmlAttribute attribute : attrsInCandidate) {
        final String attrValue = attribute.getValue();
        if (attrValue != null) {
            attrsInCandidateMap.put(Pair.create(attribute.getNamespace(), attribute.getLocalName()), attrValue);
        }
    }
    for (Map.Entry<AndroidAttributeInfo, String> entry : myAttrMap.entrySet()) {
        final String ns = entry.getKey().getNamespace();
        final String name = entry.getKey().getName();
        final String value = entry.getValue();
        final String valueInCandidate = attrsInCandidateMap.get(Pair.create(ns, name));
        if (valueInCandidate == null || !valueInCandidate.equals(value)) {
            return false;
        }
    }
    if (candidateView.getStyle().getStringValue() != null) {
        if (myParentStyleNameAttrValue == null) {
            return false;
        }
        final PsiElement styleNameAttrValueForTag = getStyleNameAttrValueForTag(candidateView);
        if (styleNameAttrValueForTag == null || !myParentStyleNameAttrValue.equals(styleNameAttrValueForTag)) {
            return false;
        }
    } else if (myParentStyleNameAttrValue != null) {
        return false;
    }
    return true;
}
Also used : DomElement(com.intellij.util.xml.DomElement) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) HashMap(com.intellij.util.containers.HashMap) HashMap(com.intellij.util.containers.HashMap) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 45 with HashMap

use of com.intellij.util.containers.HashMap in project android by JetBrains.

the class AndroidRefactoringUtil method computeAttributeMap.

@Nullable
static Map<AndroidAttributeInfo, String> computeAttributeMap(@NotNull Style style, @NotNull ErrorReporter errorReporter, @NotNull String errorReportTitle) {
    final Map<AndroidAttributeInfo, String> attributeValues = new HashMap<AndroidAttributeInfo, String>();
    for (StyleItem item : style.getItems()) {
        final String attributeName = item.getName().getStringValue();
        String attributeValue = item.getStringValue();
        if (attributeName == null || attributeName.length() <= 0 || attributeValue == null) {
            continue;
        }
        final int idx = attributeName.indexOf(':');
        final String localName = idx >= 0 ? attributeName.substring(idx + 1) : attributeName;
        final String nsPrefix = idx >= 0 ? attributeName.substring(0, idx) : null;
        if (nsPrefix != null) {
            if (!AndroidUtils.SYSTEM_RESOURCE_PACKAGE.equals(nsPrefix)) {
                errorReporter.report(RefactoringBundle.getCannotRefactorMessage("Unknown XML attribute prefix '" + nsPrefix + ":'"), errorReportTitle);
                return null;
            }
        } else {
            errorReporter.report(RefactoringBundle.getCannotRefactorMessage("The style contains attribute without 'android' prefix."), errorReportTitle);
            return null;
        }
        attributeValues.put(new AndroidAttributeInfo(localName, nsPrefix), attributeValue);
    }
    return attributeValues;
}
Also used : StyleItem(org.jetbrains.android.dom.resources.StyleItem) HashMap(com.intellij.util.containers.HashMap) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

HashMap (com.intellij.util.containers.HashMap)118 NotNull (org.jetbrains.annotations.NotNull)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 File (java.io.File)22 Nullable (org.jetbrains.annotations.Nullable)18 Map (java.util.Map)17 Module (com.intellij.openapi.module.Module)15 Project (com.intellij.openapi.project.Project)15 ArrayList (java.util.ArrayList)14 PsiElement (com.intellij.psi.PsiElement)11 HashSet (com.intellij.util.containers.HashSet)10 List (java.util.List)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 IOException (java.io.IOException)8 Pair (com.intellij.openapi.util.Pair)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)6 SearchScope (com.intellij.psi.search.SearchScope)6 Logger (com.intellij.openapi.diagnostic.Logger)5 PsiFile (com.intellij.psi.PsiFile)5 UsageInfo (com.intellij.usageView.UsageInfo)5