diff --git a/java/java-impl/src/com/intellij/psi/impl/source/tree/java/PsiLocalVariableImpl.java b/java/java-impl/src/com/intellij/psi/impl/source/tree/java/PsiLocalVariableImpl.java index 12347227ffc1..d3c2bc6b1caf 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/tree/java/PsiLocalVariableImpl.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/tree/java/PsiLocalVariableImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,6 +37,7 @@ import com.intellij.psi.search.LocalSearchScope; import com.intellij.psi.search.SearchScope; import com.intellij.psi.tree.ChildRoleBase; import com.intellij.psi.tree.IElementType; +import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtilBase; import com.intellij.ui.RowIcon; import com.intellij.util.CharTable; @@ -47,6 +48,7 @@ import gnu.trove.THashSet; import org.jetbrains.annotations.NotNull; import javax.swing.*; +import java.util.Arrays; import java.util.Set; public class PsiLocalVariableImpl extends CompositePsiElement implements PsiLocalVariable, PsiVariableEx, Constants { @@ -100,13 +102,16 @@ public class PsiLocalVariableImpl extends CompositePsiElement implements PsiLoca @NotNull public PsiTypeElement getTypeElement() { - final CompositeElement parent = getTreeParent(); - assert parent != null : this + "; [" + getText() + "]"; - final ASTNode first = parent.findChildByType(LOCAL_VARIABLE); - assert first != null : this + "; [" + getText() + "]"; - final ASTNode type = first.findChildByType(TYPE); - assert type != null : this + "; [" + getText() + "]"; - return SourceTreeToPsiMap.treeToPsiNotNull(type); + PsiTypeElement typeElement = PsiTreeUtil.getChildOfType(this, PsiTypeElement.class); + if (typeElement != null) return typeElement; + + final PsiElement parent = getParent(); + assert parent != null : "no parent; " + this + "; [" + getText() + "]"; + final PsiLocalVariable localVariable = PsiTreeUtil.getChildOfType(parent, PsiLocalVariable.class); + assert localVariable != null : "no local variable in " + Arrays.toString(parent.getChildren()); + typeElement = PsiTreeUtil.getChildOfType(localVariable, PsiTypeElement.class); + assert typeElement != null : "no type element in " + Arrays.toString(localVariable.getChildren()); + return typeElement; } public PsiModifierList getModifierList() { @@ -177,13 +182,13 @@ public class PsiLocalVariableImpl extends CompositePsiElement implements PsiLoca CompositeElement statement1 = Factory.createCompositeElement(DECLARATION_STATEMENT, charTableByTree, getManager()); statement1.addChild(variable, null); - ASTNode space = Factory.createSingleLeafElement(JavaTokenType.WHITE_SPACE, " ", 0, 1, treeCharTab, getManager()); + ASTNode space = Factory.createSingleLeafElement(TokenType.WHITE_SPACE, " ", 0, 1, treeCharTab, getManager()); variable.addChild(space, variable.getFirstChildNode()); variable.addChild(typeCopy, variable.getFirstChildNode()); if (modifierListCopy.getTextLength() > 0) { - space = Factory.createSingleLeafElement(JavaTokenType.WHITE_SPACE, " ", 0, 1, treeCharTab, getManager()); + space = Factory.createSingleLeafElement(TokenType.WHITE_SPACE, " ", 0, 1, treeCharTab, getManager()); variable.addChild(space, variable.getFirstChildNode()); } diff --git a/platform/core-api/src/com/intellij/lang/ASTNode.java b/platform/core-api/src/com/intellij/lang/ASTNode.java index b6629e5b4d74..11918df3b20c 100644 --- a/platform/core-api/src/com/intellij/lang/ASTNode.java +++ b/platform/core-api/src/com/intellij/lang/ASTNode.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -142,9 +142,9 @@ public interface ASTNode extends UserDataHolder { * Adds the specified child node at the specified position in the child list. * * @param child the child node to add. - * @param anchorBefore the node before which the child node is inserted. + * @param anchorBefore the node before which the child node is inserted (null to add a child as a last node). */ - void addChild(@NotNull ASTNode child, ASTNode anchorBefore); + void addChild(@NotNull ASTNode child, @Nullable ASTNode anchorBefore); /** * Add leaf element with specified type and text in the child list. diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/tree/CompositeElement.java b/platform/lang-impl/src/com/intellij/psi/impl/source/tree/CompositeElement.java index 6a1df7da140e..cee41867efd1 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/tree/CompositeElement.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/tree/CompositeElement.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -612,7 +612,7 @@ public class CompositeElement extends TreeElement { this.lastChild = lastChild; } - public void addChild(@NotNull ASTNode child, final ASTNode anchorBefore) { + public void addChild(@NotNull ASTNode child, @Nullable final ASTNode anchorBefore) { LOG.assertTrue(anchorBefore == null || ((TreeElement)anchorBefore).getTreeParent() == this, "anchorBefore == null || anchorBefore.getTreeParent() == parent"); TreeUtil.ensureParsed(getFirstChildNode()); TreeUtil.ensureParsed(child);