public Instruction[] getControlFlow() { assert isValid(); CachedValue<Instruction[]> controlFlow = getUserData(CONTROL_FLOW); if (controlFlow == null) { controlFlow = CachedValuesManager.getManager(getProject()) .createCachedValue( new CachedValueProvider<Instruction[]>() { @Override public Result<Instruction[]> compute() { try { ResolveProfiler.start(); final Instruction[] flow = new ControlFlowBuilder(getProject()).buildControlFlow(GrBlockImpl.this); return Result.create( flow, getContainingFile(), PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT); } finally { final long time = ResolveProfiler.finish(); ResolveProfiler.write("flow " + GrBlockImpl.this.toString() + " : " + time); } } }, false); controlFlow = putUserDataIfAbsent(CONTROL_FLOW, controlFlow); } return ControlFlowBuilder.assertValidPsi(controlFlow.getValue()); }
@Nullable public BundleManifest getManifest(@NotNull final VirtualFile libRoot) { CachedValue<BundleManifest> value = myCache.get(libRoot); if (value == null) { value = myManager.createCachedValue( new CachedValueProvider<BundleManifest>() { @Nullable @Override public Result<BundleManifest> compute() { VirtualFile manifestFile = libRoot.findFileByRelativePath(JarFile.MANIFEST_NAME); PsiFile psiFile = manifestFile != null ? PsiManager.getInstance(myProject).findFile(manifestFile) : null; BundleManifest manifest = manifestFile != null ? readManifest(psiFile) : null; return Result.createSingleDependency(manifest, libRoot); } }, false); myCache.put(libRoot, value); } return value.getValue(); }
public XmlNSDescriptor getNSDescriptor(final String namespace, boolean strict) { final XmlTag parentTag = getParentTag(); if (parentTag == null && namespace.equals(XmlUtil.XHTML_URI)) { final XmlNSDescriptor descriptor = getDtdDescriptor(XmlUtil.getContainingFile(this)); if (descriptor != null) { return descriptor; } } Map<String, CachedValue<XmlNSDescriptor>> map = initNSDescriptorsMap(); final CachedValue<XmlNSDescriptor> descriptor = map.get(namespace); if (descriptor != null) { final XmlNSDescriptor value = descriptor.getValue(); if (value != null) { return value; } } if (parentTag == null) { final XmlDocument parentOfType = PsiTreeUtil.getParentOfType(this, XmlDocument.class); if (parentOfType == null) { return null; } return parentOfType.getDefaultNSDescriptor(namespace, strict); } return parentTag.getNSDescriptor(namespace, strict); }
private synchronized void save() { myDir.mkdirs(); Properties props = new Properties(); props.setProperty(KIND_KEY, myKind.toString()); props.setProperty(ID_KEY, myId.getValue()); props.setProperty(PATH_OR_URL_KEY, myRepositoryPathOrUrl); props.setProperty(INDEX_VERSION_KEY, CURRENT_VERSION); if (myUpdateTimestamp != null) props.setProperty(TIMESTAMP_KEY, String.valueOf(myUpdateTimestamp)); if (myDataDirName != null) props.setProperty(DATA_DIR_NAME_KEY, myDataDirName); if (myFailureMessage != null) props.setProperty(FAILURE_MESSAGE_KEY, myFailureMessage); try { FileOutputStream s = new FileOutputStream(new File(myDir, INDEX_INFO_FILE)); try { props.store(s, null); } finally { s.close(); } } catch (IOException e) { MavenLog.LOG.warn(e); } }
@Override public XmlNSDescriptor getDefaultNSDescriptor(final String namespace, final boolean strict) { long curExtResourcesModCount = ExternalResourceManagerEx.getInstanceEx().getModificationCount(getProject()); if (myExtResourcesModCount != curExtResourcesModCount) { myDefaultDescriptorsCacheNotStrict.clear(); myDefaultDescriptorsCacheStrict.clear(); myExtResourcesModCount = curExtResourcesModCount; } final ConcurrentMap<String, CachedValue<XmlNSDescriptor>> defaultDescriptorsCache; if (strict) { defaultDescriptorsCache = myDefaultDescriptorsCacheStrict; } else { defaultDescriptorsCache = myDefaultDescriptorsCacheNotStrict; } CachedValue<XmlNSDescriptor> cachedValue = defaultDescriptorsCache.get(namespace); if (cachedValue == null) { defaultDescriptorsCache.put( namespace, cachedValue = new PsiCachedValueImpl<XmlNSDescriptor>( getManager(), new CachedValueProvider<XmlNSDescriptor>() { @Override public Result<XmlNSDescriptor> compute() { final XmlNSDescriptor defaultNSDescriptorInner = getDefaultNSDescriptorInner(namespace, strict); if (isGeneratedFromDtd(defaultNSDescriptorInner)) { return new Result<XmlNSDescriptor>( defaultNSDescriptorInner, XmlDocumentImpl.this, ExternalResourceManager.getInstance()); } return new Result<XmlNSDescriptor>( defaultNSDescriptorInner, defaultNSDescriptorInner != null ? defaultNSDescriptorInner.getDependences() : ExternalResourceManager.getInstance()); } })); } return cachedValue.getValue(); }
private PsiJavaFileStub getStub() { CachedValue<PsiJavaFileStub> answer = file.getUserData(JAVA_API_STUB); if (answer == null) { answer = CachedValuesManager.getManager(getProject()) .createCachedValue( new CachedValueProvider<PsiJavaFileStub>() { @Override public Result<PsiJavaFileStub> compute() { return Result.create( calcStub(), PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT); } }, false); file.putUserData(JAVA_API_STUB, answer); } return answer.getValue(); }
@Override @Nullable public PsiModifierList getAnnotationList() { if (myAnnotationList == null) { myAnnotationList = CachedValuesManager.getManager(myManager.getProject()) .createCachedValue(new PackageAnnotationValueProvider()); } return myAnnotationList.getValue(); }
/** * Analyze project with string cache for given file. Given file will be fully analyzed. * * @param file * @param declarationProvider * @return */ public static AnalyzeExhaust analyzeFileWithCache( @NotNull final JetFile file, @NotNull final Function<JetFile, Collection<JetFile>> declarationProvider) { // Need lock for getValue(), because parallel threads can start evaluation of compute() // simultaneously synchronized (lock) { CachedValue<AnalyzeExhaust> bindingContextCachedValue = file.getUserData(BINDING_CONTEXT); if (bindingContextCachedValue == null) { bindingContextCachedValue = CachedValuesManager.getManager(file.getProject()) .createCachedValue( new CachedValueProvider<AnalyzeExhaust>() { @Override public Result<AnalyzeExhaust> compute() { try { AnalyzeExhaust bindingContext = analyzeFilesWithJavaIntegration( file.getProject(), declarationProvider.fun(file), Predicates.<PsiFile>equalTo(file), JetControlFlowDataTraceFactory.EMPTY); return new Result<AnalyzeExhaust>( bindingContext, PsiModificationTracker.MODIFICATION_COUNT); } catch (ProcessCanceledException e) { throw e; } catch (Throwable e) { DiagnosticUtils.throwIfRunningOnServer(e); LOG.error(e); BindingTraceContext bindingTraceContext = new BindingTraceContext(); bindingTraceContext.report(Errors.EXCEPTION_WHILE_ANALYZING.on(file, e)); AnalyzeExhaust analyzeExhaust = new AnalyzeExhaust(bindingTraceContext.getBindingContext(), null); return new Result<AnalyzeExhaust>( analyzeExhaust, PsiModificationTracker.MODIFICATION_COUNT); } } }, false); file.putUserData(BINDING_CONTEXT, bindingContextCachedValue); } return bindingContextCachedValue.getValue(); } }
@Nullable protected TypeDescriptor findTypeDescriptorImpl( XmlTag rootTag, final String name, String namespace, Set<XmlTag> visited) { XmlNSDescriptorImpl responsibleDescriptor = this; if (namespace != null && namespace.length() != 0 && !namespace.equals(getDefaultNamespace())) { final XmlNSDescriptor nsDescriptor = rootTag.getNSDescriptor(namespace, true); if (nsDescriptor instanceof XmlNSDescriptorImpl) { responsibleDescriptor = (XmlNSDescriptorImpl) nsDescriptor; } } if (responsibleDescriptor != this) { return responsibleDescriptor.findTypeDescriptor(XmlUtil.findLocalNameByQualifiedName(name)); } if (rootTag == null) return null; if (visited != null) { if (visited.contains(rootTag)) return null; visited.add(rootTag); } final Pair<QNameKey, XmlTag> pair = new Pair<QNameKey, XmlTag>(new QNameKey(name, namespace), rootTag); final CachedValue<TypeDescriptor> descriptor = myTypesMap.get(pair); if (descriptor != null) { TypeDescriptor value = descriptor.getValue(); if (value == null || (value instanceof ComplexTypeDescriptor && ((ComplexTypeDescriptor) value).getDeclaration().isValid())) return value; } XmlTag[] tags = rootTag.getSubTags(); if (visited == null) { visited = new HashSet<XmlTag>(1); visited.add(rootTag); } return doFindIn(tags, name, namespace, pair, rootTag, visited); }
@Nullable private static PsiType getLeastUpperBoundByVar(final GrVariable resolved) { CachedValue<PsiType> data = resolved.getUserData(LEAST_UPPER_BOUND_TYPE); if (data == null) { data = CachedValuesManager.getManager(resolved.getProject()) .createCachedValue( new CachedValueProvider<PsiType>() { @Override public Result<PsiType> compute() { return Result.create( getLeastUpperBoundByVarImpl(resolved), PsiModificationTracker.MODIFICATION_COUNT, ProjectRootManager.getInstance(resolved.getProject())); } }, false); } return data.getValue(); }
/** * Analyze project with string cache for the whole project. All given files will be analyzed only * for descriptors. */ public static AnalyzeExhaust analyzeProjectWithCache( @NotNull final Project project, @NotNull final Collection<JetFile> files) { // Need lock for getValue(), because parallel threads can start evaluation of compute() // simultaneously synchronized (lock) { CachedValue<AnalyzeExhaust> bindingContextCachedValue = project.getUserData(BINDING_CONTEXT); if (bindingContextCachedValue == null) { bindingContextCachedValue = CachedValuesManager.getManager(project) .createCachedValue( new CachedValueProvider<AnalyzeExhaust>() { @Override public Result<AnalyzeExhaust> compute() { try { AnalyzeExhaust analyzeExhaust = analyzeFilesWithJavaIntegration( project, files, Predicates.<PsiFile>alwaysFalse(), JetControlFlowDataTraceFactory.EMPTY); return new Result<AnalyzeExhaust>( analyzeExhaust, PsiModificationTracker.MODIFICATION_COUNT); } catch (ProcessCanceledException e) { throw e; } catch (Throwable e) { DiagnosticUtils.throwIfRunningOnServer(e); LOG.error(e); BindingTraceContext bindingTraceContext = new BindingTraceContext(); AnalyzeExhaust analyzeExhaust = new AnalyzeExhaust(bindingTraceContext.getBindingContext(), null); return new Result<AnalyzeExhaust>( analyzeExhaust, PsiModificationTracker.MODIFICATION_COUNT); } } }, false); project.putUserData(BINDING_CONTEXT, bindingContextCachedValue); } return bindingContextCachedValue.getValue(); } }
/** Returns the same as {@link #getGlobalDartSdk()} but much faster */ @Nullable public static DartSdk getDartSdk(@NotNull final Project project) { CachedValue<DartSdk> cachedValue = project.getUserData(CACHED_DART_SDK_KEY); if (cachedValue == null) { cachedValue = CachedValuesManager.getManager(project) .createCachedValue( () -> { final DartSdk sdk = getGlobalDartSdk(); if (sdk == null) { return new CachedValueProvider.Result<DartSdk>( null, DartProjectComponent.getProjectRootsModificationTracker(project)); } List<Object> dependencies = new ArrayList<Object>(3); dependencies.add( DartProjectComponent.getProjectRootsModificationTracker(project)); ContainerUtil.addIfNotNull( dependencies, LocalFileSystem.getInstance() .findFileByPath(sdk.getHomePath() + "/version")); ContainerUtil.addIfNotNull( dependencies, LocalFileSystem.getInstance() .findFileByPath(sdk.getHomePath() + "/lib/core/core.dart")); return new CachedValueProvider.Result<DartSdk>( sdk, ArrayUtil.toObjectArray(dependencies)); }, false); project.putUserData(CACHED_DART_SDK_KEY, cachedValue); } return cachedValue.getValue(); }
@Nullable public XmlElementDescriptor getElementDescriptor( String localName, String namespace, Set<XmlNSDescriptorImpl> visited, boolean reference) { if (visited.contains(this)) return null; final QNameKey pair = new QNameKey(namespace, localName); final CachedValue<XmlElementDescriptor> descriptor = myDescriptorsMap.get(pair); if (descriptor != null) { final XmlElementDescriptor value = descriptor.getValue(); if (value == null || value.getDeclaration().isValid()) return value; } final XmlTag rootTag = myTag; if (rootTag == null) return null; XmlTag[] tags = rootTag.getSubTags(); visited.add(this); for (final XmlTag tag : tags) { if (equalsToSchemaName(tag, ELEMENT_TAG_NAME)) { String name = tag.getAttributeValue("name"); if (name != null) { if (checkElementNameEquivalence(localName, namespace, name, tag)) { final CachedValue<XmlElementDescriptor> cachedValue = CachedValuesManager.getManager(tag.getProject()) .createCachedValue( new CachedValueProvider<XmlElementDescriptor>() { public Result<XmlElementDescriptor> compute() { final String name = tag.getAttributeValue("name"); if (name != null && !name.equals(pair.second)) { myDescriptorsMap.remove(pair); return new Result<XmlElementDescriptor>(null); } final XmlElementDescriptor xmlElementDescriptor = createElementDescriptor(tag); return new Result<XmlElementDescriptor>( xmlElementDescriptor, xmlElementDescriptor.getDependences()); } }, false); myDescriptorsMap.put(pair, cachedValue); return cachedValue.getValue(); } } } else if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || (reference && equalsToSchemaName(tag, IMPORT_TAG_NAME) && (namespace.equals(tag.getAttributeValue("namespace")) || namespace.length() == 0 && tag.getAttributeValue("namespace") == null))) { final XmlAttribute schemaLocation = tag.getAttribute("schemaLocation", null); if (schemaLocation != null) { final XmlFile xmlFile = XmlUtil.findNamespaceByLocation( rootTag.getContainingFile(), schemaLocation.getValue()); if (xmlFile != null) { final XmlDocument includedDocument = xmlFile.getDocument(); if (includedDocument != null) { final PsiMetaData data = includedDocument.getMetaData(); if (data instanceof XmlNSDescriptorImpl) { final XmlElementDescriptor elementDescriptor = ((XmlNSDescriptorImpl) data) .getElementDescriptor(localName, namespace, visited, reference); if (elementDescriptor != null) { // final CachedValue<XmlElementDescriptor> value = // includedDocument.getManager().getCachedValuesManager() // .createCachedValue(new CachedValueProvider<XmlElementDescriptor>() { // public Result<XmlElementDescriptor> compute() { // return new Result<XmlElementDescriptor>(elementDescriptor, // elementDescriptor.getDependences()); // } // }, false); // return value.getValue(); return elementDescriptor; } } } } } } else if (equalsToSchemaName(tag, REDEFINE_TAG_NAME)) { final XmlNSDescriptorImpl nsDescriptor = getRedefinedElementDescriptor(tag); if (nsDescriptor != null) { final XmlElementDescriptor xmlElementDescriptor = nsDescriptor.getElementDescriptor(localName, namespace, visited, reference); if (xmlElementDescriptor != null) return xmlElementDescriptor; } } } return null; }
@Nullable public BundleManifest getManifest(@NotNull final Module module) { final OsmorcFacet facet = OsmorcFacet.getInstance(module); if (facet == null) return null; CachedValue<BundleManifest> value = myCache.get(facet); if (value == null) { value = myManager.createCachedValue( new CachedValueProvider<BundleManifest>() { @Nullable @Override public Result<BundleManifest> compute() { OsmorcFacetConfiguration configuration = facet.getConfiguration(); BundleManifest manifest = null; List<Object> dependencies = ContainerUtil.<Object>newSmartList(configuration); switch (configuration.getManifestGenerationMode()) { case Manually: { PsiFile manifestFile = findInModuleRoots( facet.getModule(), configuration.getManifestLocation()); if (manifestFile != null) { manifest = readManifest(manifestFile); dependencies.add(manifestFile); } else { dependencies.add(PsiModificationTracker.MODIFICATION_COUNT); } break; } case OsmorcControlled: { Map<String, String> map = ContainerUtil.newHashMap(configuration.getAdditionalPropertiesAsMap()); map.put( Constants.BUNDLE_SYMBOLICNAME, configuration.getBundleSymbolicName()); map.put(Constants.BUNDLE_VERSION, configuration.getBundleVersion()); map.put(Constants.BUNDLE_ACTIVATOR, configuration.getBundleActivator()); manifest = new BundleManifest(map); break; } case Bnd: { PsiFile bndFile = findInModuleRoots( facet.getModule(), configuration.getBndFileLocation()); if (bndFile != null) { manifest = readProperties(bndFile); dependencies.add(bndFile); } else { dependencies.add(PsiModificationTracker.MODIFICATION_COUNT); } break; } case Bundlor: break; // not supported } return Result.create(manifest, dependencies); } }, false); myCache.put(facet, value); } return value.getValue(); }
private TypeDescriptor doFindIn( final XmlTag[] tags, final String name, final String namespace, final Pair<QNameKey, XmlTag> pair, final XmlTag rootTag, final Set<XmlTag> visited) { for (final XmlTag tag : tags) { if (equalsToSchemaName(tag, "complexType")) { if (name == null) { CachedValue<TypeDescriptor> value = createAndPutTypesCachedValue(tag, pair); return value.getValue(); } String nameAttribute = tag.getAttributeValue("name"); if (isSameName(name, namespace, nameAttribute)) { CachedValue<TypeDescriptor> cachedValue = createAndPutTypesCachedValue(tag, pair); return cachedValue.getValue(); } } else if (equalsToSchemaName(tag, "simpleType")) { if (name == null) { CachedValue<TypeDescriptor> value = createAndPutTypesCachedValueSimpleType(tag, pair); return value.getValue(); } String nameAttribute = tag.getAttributeValue("name"); if (isSameName(name, namespace, nameAttribute)) { CachedValue<TypeDescriptor> cachedValue = createAndPutTypesCachedValue(tag, pair); return cachedValue.getValue(); } } else if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || (equalsToSchemaName(tag, IMPORT_TAG_NAME) && (namespace == null || !namespace.equals(getDefaultNamespace())))) { final String schemaLocation = tag.getAttributeValue("schemaLocation"); if (schemaLocation != null) { final XmlFile xmlFile = XmlUtil.findNamespaceByLocation(rootTag.getContainingFile(), schemaLocation); if (xmlFile != null) { final XmlDocument document = xmlFile.getDocument(); if (document != null) { final CachedValue<TypeDescriptor> value = CachedValuesManager.getManager(tag.getProject()) .createCachedValue( new CachedValueProvider<TypeDescriptor>() { public Result<TypeDescriptor> compute() { final String currentName = tag.getAttributeValue("name"); if ((currentName != null && !currentName.equals( XmlUtil.findLocalNameByQualifiedName(name))) || !xmlFile.isValid() || xmlFile.getDocument() == null) { myTypesMap.remove(pair); return new Result<TypeDescriptor>(null); } final XmlDocument document = xmlFile.getDocument(); final XmlNSDescriptorImpl nsDescriptor = findNSDescriptor(tag, document); if (nsDescriptor == null) { myTypesMap.remove(pair); return new Result<TypeDescriptor>(null); } final XmlTag rTag = document.getRootTag(); final TypeDescriptor complexTypeDescriptor = nsDescriptor.findTypeDescriptorImpl( rTag, name, namespace, visited); return new Result<TypeDescriptor>(complexTypeDescriptor, rTag); } }, false); if (value.getValue() != null) { myTypesMap.put(pair, value); return value.getValue(); } } } } } else if (equalsToSchemaName(tag, REDEFINE_TAG_NAME)) { final XmlTag[] subTags = tag.getSubTags(); TypeDescriptor descriptor = doFindIn(subTags, name, namespace, pair, rootTag, visited); if (descriptor != null) return descriptor; final XmlNSDescriptorImpl nsDescriptor = getRedefinedElementDescriptor(tag); if (nsDescriptor != null) { final XmlTag redefinedRootTag = ((XmlDocument) nsDescriptor.getDeclaration()).getRootTag(); descriptor = doFindIn( redefinedRootTag.getSubTags(), name, namespace, pair, redefinedRootTag, visited); if (descriptor != null) return descriptor; } } } return null; }
@Nullable private XmlAttributeDescriptor getAttributeImpl( String localName, String namespace, Set<XmlTag> visited) { if (myTag == null) return null; XmlNSDescriptorImpl nsDescriptor = (XmlNSDescriptorImpl) myTag.getNSDescriptor(namespace, true); if (nsDescriptor != this && nsDescriptor != null) { return nsDescriptor.getAttributeImpl(localName, namespace, visited); } if (visited == null) visited = new HashSet<XmlTag>(1); else if (visited.contains(myTag)) return null; visited.add(myTag); XmlTag[] tags = myTag.getSubTags(); for (XmlTag tag : tags) { if (equalsToSchemaName(tag, ATTRIBUTE_TAG_NAME)) { String name = tag.getAttributeValue("name"); if (name != null) { if (checkElementNameEquivalence(localName, namespace, name, tag)) { return createAttributeDescriptor(tag); } } } else if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || (equalsToSchemaName(tag, IMPORT_TAG_NAME) && namespace.equals(tag.getAttributeValue("namespace")))) { final XmlAttribute schemaLocation = tag.getAttribute("schemaLocation", null); if (schemaLocation != null) { final XmlFile xmlFile = XmlUtil.findNamespaceByLocation(myTag.getContainingFile(), schemaLocation.getValue()); if (xmlFile != null) { final XmlDocument includedDocument = xmlFile.getDocument(); if (includedDocument != null) { final PsiMetaData data = includedDocument.getMetaData(); if (data instanceof XmlNSDescriptorImpl) { final XmlAttributeDescriptor attributeDescriptor = ((XmlNSDescriptorImpl) data).getAttributeImpl(localName, namespace, visited); if (attributeDescriptor != null) { final CachedValue<XmlAttributeDescriptor> value = CachedValuesManager.getManager(includedDocument.getProject()) .createCachedValue( new CachedValueProvider<XmlAttributeDescriptor>() { public Result<XmlAttributeDescriptor> compute() { return new Result<XmlAttributeDescriptor>( attributeDescriptor, attributeDescriptor.getDependences()); } }, false); return value.getValue(); } } } } } } } return null; }
private int createContext(File contextDir, String suffix) throws MavenServerIndexerException { String indexId = myDir.getName() + "-" + suffix; return myIndexer.createIndex( indexId, myId.getValue(), getRepositoryFile(), getRepositoryUrl(), contextDir); }
public String getRepositoryId() { return myId.getValue(); }