private void removeOwnerForRootNodeContext(final SNode node, final ITypeContextOwner owner) { ModelAccess.assertLegalRead(); if (node == null || node.getModel() == null) return; final SNode rootNode = node.getContainingRoot(); // if node is disposed, then context was removed by beforeModelDisposed/beforeRootDeleted // listener synchronized (myLock) { SNodeReference rootNodePointer = new jetbrains.mps.smodel.SNodePointer(rootNode); List<TypecheckingContextHolder> contextWithOwners = myTypeCheckingContexts.get(rootNodePointer); if (contextWithOwners != null) { for (ListIterator<TypecheckingContextHolder> it = contextWithOwners.listIterator(); it.hasNext(); ) { TypecheckingContextHolder contextHolder = it.next(); ITypeContextOwner o = contextHolder.getOwner(); if (o == owner) { if (!owner.reuseTypecheckingContext()) { contextHolder.release(); if (!contextHolder.isActive()) { it.remove(); } } else { contextHolder.release(); if (!contextHolder.isActive()) { it.remove(); } } break; } } if (contextWithOwners.isEmpty()) { myTypeCheckingContexts.remove(rootNodePointer); } } } }
private TypeCheckingContext getOrCreateContext( SNode node, ITypeContextOwner owner, boolean createIfAbsent) { ModelAccess.assertLegalRead(); if (node == null) return null; final SNode rootNode = node.getContainingRoot(); synchronized (myLock) { SNodeReference rootNodePointer = new jetbrains.mps.smodel.SNodePointer(rootNode); List<TypecheckingContextHolder> contextWithOwners = myTypeCheckingContexts.get(rootNodePointer); if (contextWithOwners == null && !createIfAbsent) return null; if (contextWithOwners == null) { contextWithOwners = new ArrayList<TypecheckingContextHolder>(4); myTypeCheckingContexts.put(rootNodePointer, contextWithOwners); } for (ListIterator<TypecheckingContextHolder> it = contextWithOwners.listIterator(); it.hasNext(); ) { TypecheckingContextHolder contextHolder = it.next(); if (contextHolder.getOwner() == owner) { if (!owner.reuseTypecheckingContext()) { assert createIfAbsent; return contextHolder.acquire(node); } else { // reuse the typechecking context if (!createIfAbsent) { return contextHolder.get(node); } final TypeCheckingContext ctx = contextHolder.acquire(node); // Dirty hack if (jetbrains.mps.util.SNodeOperations.isDisposed(ctx.getNode())) { removeContextForNode(rootNodePointer); LOG.error( "Type Checking Context had a disposed node inside. Node: " + node + " model: " + node.getModel()); return getOrCreateContext(node, owner, createIfAbsent); } return ctx; } } } // not found, create new if (!owner.reuseTypecheckingContext()) { assert createIfAbsent; final NonReusableTypecheckingContextHolder contextHolder = new NonReusableTypecheckingContextHolder(owner); contextWithOwners.add(contextHolder); return contextHolder.acquire(node); } else if (!createIfAbsent) { return null; } else { if (contextWithOwners.size() > 100) { if (!myReported) { myReported = true; LOG.warn( "Type checking context for node " + node.getPresentation() + " has too much owners"); } } final CountingTypecheckingContextHolder contextHolder = new CountingTypecheckingContextHolder(owner); contextWithOwners.add(contextHolder); return contextHolder.acquire(node); } } }