@Override public TypeCheckingContext acquire(SNode node) { if (myContexts.size() >= 10) { LOG.warn("too many non-reusable typechecking contexts"); return null; } for (TypeCheckingContext ctx : myContexts) { if (ctx.getNode() == node) { LOG.warn("double typechecking context acquiring"); return null; } } final TypeCheckingContext ctx = myOwner.createTypecheckingContext(node, TypeContextManager.this); addModelListener(node); myContexts.add(ctx); return ctx; }
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); } } }