Mark getters, that return null only in case of parsing error

This commit is contained in:
Maxim Shafirov
2011-01-11 01:18:30 +03:00
parent 04fbaa26fe
commit 6cf565dcf5
28 changed files with 67 additions and 32 deletions
@@ -34,6 +34,7 @@ public interface JetNodeTypes {
// TODO: Not sure if we really need separate PSI nodes for the class parameters? // TODO: Not sure if we really need separate PSI nodes for the class parameters?
JetNodeType PRIMARY_CONSTRUCTOR_PARAMETERS_LIST = new JetNodeType("PRIMARY_CONSTRUCTOR_PARAMETERS_LIST", JetParameterList.class); JetNodeType PRIMARY_CONSTRUCTOR_PARAMETERS_LIST = new JetNodeType("PRIMARY_CONSTRUCTOR_PARAMETERS_LIST", JetParameterList.class);
JetNodeType PRIMARY_CONSTRUCTOR_PARAMETER = new JetNodeType("PRIMARY_CONSTRUCTOR_PARAMETER", JetParameter.class); JetNodeType PRIMARY_CONSTRUCTOR_PARAMETER = new JetNodeType("PRIMARY_CONSTRUCTOR_PARAMETER", JetParameter.class);
JetNodeType CLASS_BODY = new JetNodeType("CLASS_BODY", JetClassBody.class); JetNodeType CLASS_BODY = new JetNodeType("CLASS_BODY", JetClassBody.class);
JetNodeType IMPORT_DIRECTIVE = new JetNodeType("IMPORT_DIRECTIVE", JetImportDirective.class); JetNodeType IMPORT_DIRECTIVE = new JetNodeType("IMPORT_DIRECTIVE", JetImportDirective.class);
JetNodeType IMPORTED = new JetNodeType("IMPORTED"); // TODO: ??? JetNodeType IMPORTED = new JetNodeType("IMPORTED"); // TODO: ???
@@ -17,11 +17,12 @@ public class JetArgument extends JetElement {
visitor.visitArgument(this); visitor.visitArgument(this);
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetExpression getArgumentExpression() { public JetExpression getArgumentExpression() {
return findChildByClass(JetExpression.class); return findChildByClass(JetExpression.class);
} }
@Nullable
public String getArgumentName() { public String getArgumentName() {
ASTNode firstChildNode = getNode().getFirstChildNode(); ASTNode firstChildNode = getNode().getFirstChildNode();
return firstChildNode.getElementType() == JetTokens.IDENTIFIER ? firstChildNode.getText() : null; return firstChildNode.getElementType() == JetTokens.IDENTIFIER ? firstChildNode.getText() : null;
@@ -21,7 +21,7 @@ public class JetAttribute extends JetElement {
visitor.visitAttribute(this); visitor.visitAttribute(this);
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetTypeReference getTypeReference() { public JetTypeReference getTypeReference() {
return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE); return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
} }
@@ -27,7 +27,7 @@ public class JetBinaryExpression extends JetExpression {
return left; return left;
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetExpression getRight() { public JetExpression getRight() {
ASTNode node = getOperationTokenNode(); ASTNode node = getOperationTokenNode();
while (node != null) { while (node != null) {
@@ -48,7 +48,7 @@ public class JetBinaryExpression extends JetExpression {
return operationNode; return operationNode;
} }
@Nullable @NotNull
public JetToken getOperationSign() { public JetToken getOperationSign() {
return (JetToken) getOperationTokenNode().getElementType(); return (JetToken) getOperationTokenNode().getElementType();
} }
@@ -27,7 +27,7 @@ public class JetBinaryExpressionWithTypeRHS extends JetExpression {
return left; return left;
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetTypeReference getRight() { public JetTypeReference getRight() {
ASTNode node = getOperationTokenNode(); ASTNode node = getOperationTokenNode();
while (node != null) { while (node != null) {
@@ -48,7 +48,7 @@ public class JetBinaryExpressionWithTypeRHS extends JetExpression {
return operationNode; return operationNode;
} }
@Nullable @NotNull
public JetToken getOperationSign() { public JetToken getOperationSign() {
return (JetToken) getOperationTokenNode().getElementType(); return (JetToken) getOperationTokenNode().getElementType();
} }
@@ -20,12 +20,12 @@ public class JetCatchSection extends JetElement {
visitor.visitCatchSection(this); visitor.visitCatchSection(this);
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetParameterList getParameterList() { public JetParameterList getParameterList() {
return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST); return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST);
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetParameter getCatchParameter() { public JetParameter getCatchParameter() {
JetParameterList list = getParameterList(); JetParameterList list = getParameterList();
if (list == null) return null; if (list == null) return null;
@@ -34,6 +34,7 @@ public class JetCatchSection extends JetElement {
} }
@Nullable(IF_NOT_PARSED)
public JetExpression getCatchBody() { public JetExpression getCatchBody() {
return findChildByClass(JetExpression.class); return findChildByClass(JetExpression.class);
} }
@@ -2,7 +2,6 @@ package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** /**
* @author max * @author max
@@ -17,8 +16,10 @@ public class JetClassInitializer extends JetDeclaration {
visitor.visitAnonymousInitializer(this); visitor.visitAnonymousInitializer(this);
} }
@Nullable @NotNull
public JetExpression getBody() { public JetExpression getBody() {
return findChildByClass(JetExpression.class); JetExpression body = findChildByClass(JetExpression.class);
assert body != null;
return body;
} }
} }
@@ -21,7 +21,7 @@ public class JetConstructor extends JetDeclaration {
visitor.visitConstructor(this); visitor.visitConstructor(this);
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetParameterList getParameterList() { public JetParameterList getParameterList() {
return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST); return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST);
} }
@@ -21,7 +21,7 @@ public class JetDecomposer extends JetNamedDeclaration {
visitor.visitDecomposer(this); visitor.visitDecomposer(this);
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetDecomposerPropertyList getPropertyList() { public JetDecomposerPropertyList getPropertyList() {
return (JetDecomposerPropertyList) findChildByType(JetNodeTypes.DECOMPOSER_PROPERTY_LIST); return (JetDecomposerPropertyList) findChildByType(JetNodeTypes.DECOMPOSER_PROPERTY_LIST);
} }
@@ -18,11 +18,12 @@ public class JetDoWhileExpression extends JetExpression {
visitor.visitDoWhileExpression(this); visitor.visitDoWhileExpression(this);
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetExpression getCondition() { public JetExpression getCondition() {
return findExpressionUnder(JetNodeTypes.CONDITION); return findExpressionUnder(JetNodeTypes.CONDITION);
} }
@Nullable(IF_NOT_PARSED)
public JetExpression getBody() { public JetExpression getBody() {
return findExpressionUnder(JetNodeTypes.BODY); return findExpressionUnder(JetNodeTypes.BODY);
} }
@@ -15,6 +15,8 @@ public class JetElement extends ASTWrapperPsiElement {
super(node); super(node);
} }
protected static final String IF_NOT_PARSED = "Returns null if has parsing error";
@NotNull @NotNull
@Override @Override
public Language getLanguage() { public Language getLanguage() {
@@ -21,6 +21,7 @@ public class JetExtension extends JetTypeParameterListOwner {
visitor.visitExtension(this); visitor.visitExtension(this);
} }
@NotNull
public List<JetDeclaration> getDeclarations() { public List<JetDeclaration> getDeclarations() {
JetClassBody body = (JetClassBody) findChildByType(JetNodeTypes.CLASS_BODY); JetClassBody body = (JetClassBody) findChildByType(JetNodeTypes.CLASS_BODY);
if (body == null) return Collections.emptyList(); if (body == null) return Collections.emptyList();
@@ -28,7 +29,7 @@ public class JetExtension extends JetTypeParameterListOwner {
return body.getDeclarations(); return body.getDeclarations();
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetTypeReference getTargetTypeRef() { public JetTypeReference getTargetTypeRef() {
return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE); return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
} }
@@ -24,7 +24,7 @@ public class JetFunction extends JetTypeParameterListOwner {
visitor.visitFunction(this); visitor.visitFunction(this);
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetParameterList getParameterList() { public JetParameterList getParameterList() {
return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST); return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST);
} }
@@ -18,15 +18,17 @@ public class JetIfExpression extends JetExpression {
visitor.visitIfExpression(this); visitor.visitIfExpression(this);
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetExpression getCondition() { public JetExpression getCondition() {
return findExpressionUnder(JetNodeTypes.CONDITION); return findExpressionUnder(JetNodeTypes.CONDITION);
} }
@Nullable(IF_NOT_PARSED)
public JetExpression getThen() { public JetExpression getThen() {
return findExpressionUnder(JetNodeTypes.THEN); return findExpressionUnder(JetNodeTypes.THEN);
} }
@Nullable
public JetExpression getElse() { public JetExpression getElse() {
return findExpressionUnder(JetNodeTypes.ELSE); return findExpressionUnder(JetNodeTypes.ELSE);
} }
@@ -19,6 +19,7 @@ public class JetImportDirective extends JetElement {
visitor.visitImportDirective(this); visitor.visitImportDirective(this);
} }
@NotNull
public String getImportedName() { public String getImportedName() {
StringBuilder answer = new StringBuilder(); StringBuilder answer = new StringBuilder();
ASTNode childNode = getNode().getFirstChildNode(); ASTNode childNode = getNode().getFirstChildNode();
@@ -25,7 +25,7 @@ public class JetMatchExpression extends JetExpression {
return left; return left;
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetMatchBlock getMatchBlock() { public JetMatchBlock getMatchBlock() {
return (JetMatchBlock) findChildByType(JetNodeTypes.MATCH_BLOCK); return (JetMatchBlock) findChildByType(JetNodeTypes.MATCH_BLOCK);
} }
@@ -21,6 +21,7 @@ public class JetNewExpression extends JetExpression {
visitor.visitNewExpression(this); visitor.visitNewExpression(this);
} }
@Nullable(IF_NOT_PARSED)
public JetTypeReference getTypeReference() { public JetTypeReference getTypeReference() {
return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE); return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
} }
@@ -35,6 +35,7 @@ public class JetObjectLiteralExpression extends JetExpression implements JetClas
} }
@Override @Override
@NotNull
public List<JetDeclaration> getDeclarations() { public List<JetDeclaration> getDeclarations() {
JetClassBody body = (JetClassBody) findChildByType(JetNodeTypes.CLASS_BODY); JetClassBody body = (JetClassBody) findChildByType(JetNodeTypes.CLASS_BODY);
if (body == null) return Collections.emptyList(); if (body == null) return Collections.emptyList();
@@ -19,8 +19,11 @@ public class JetPostfixExpression extends JetExpression {
visitor.visitPostfixExpression(this); visitor.visitPostfixExpression(this);
} }
@NotNull
public JetExpression getBaseExpression() { public JetExpression getBaseExpression() {
return findChildByClass(JetExpression.class); JetExpression answer = findChildByClass(JetExpression.class);
assert answer != null;
return answer;
} }
@NotNull @NotNull
@@ -2,7 +2,6 @@ package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lexer.JetToken; import org.jetbrains.jet.lexer.JetToken;
import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.lexer.JetTokens;
@@ -19,8 +18,11 @@ public class JetPrefixExpression extends JetExpression {
visitor.visitPrefixExpression(this); visitor.visitPrefixExpression(this);
} }
@NotNull
public JetExpression getBaseExpression() { public JetExpression getBaseExpression() {
return findChildByClass(JetExpression.class); JetExpression answer = findChildByClass(JetExpression.class);
assert answer != null;
return answer;
} }
@NotNull @NotNull
@@ -30,7 +32,7 @@ public class JetPrefixExpression extends JetExpression {
return operationNode; return operationNode;
} }
@Nullable @NotNull
public JetToken getOperationSign() { public JetToken getOperationSign() {
return (JetToken) getOperationTokenNode().getElementType(); return (JetToken) getOperationTokenNode().getElementType();
} }
@@ -71,6 +71,7 @@ public class JetProperty extends JetNamedDeclaration {
return null; return null;
} }
@Nullable
public JetPropertyAccessor getSetter() { public JetPropertyAccessor getSetter() {
for (JetPropertyAccessor accessor : getAccessors()) { for (JetPropertyAccessor accessor : getAccessors()) {
if (accessor.isSetter()) return accessor; if (accessor.isSetter()) return accessor;
@@ -22,7 +22,7 @@ public abstract class JetQualifiedExpression extends JetExpression {
return left; return left;
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetExpression getSelectorExpression() { public JetExpression getSelectorExpression() {
ASTNode node = getOperationTokenNode(); ASTNode node = getOperationTokenNode();
while (node != null) { while (node != null) {
@@ -42,7 +42,7 @@ public abstract class JetQualifiedExpression extends JetExpression {
return operationNode; return operationNode;
} }
@Nullable @NotNull
public JetToken getOperationSign() { public JetToken getOperationSign() {
return (JetToken) getOperationTokenNode().getElementType(); return (JetToken) getOperationTokenNode().getElementType();
} }
@@ -17,7 +17,7 @@ public class JetThrowExpression extends JetExpression {
visitor.visitThrowExpression(this); visitor.visitThrowExpression(this);
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetExpression getThrownExpression() { public JetExpression getThrownExpression() {
return findChildByClass(JetExpression.class); return findChildByClass(JetExpression.class);
} }
@@ -25,7 +25,7 @@ public class JetTypeConstraint extends JetElement {
findChildByType(JetTokens.OBJECT_KEYWORD) != null; findChildByType(JetTokens.OBJECT_KEYWORD) != null;
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetTypeReference getSubjectTypeReference() { public JetTypeReference getSubjectTypeReference() {
ASTNode node = getNode().getFirstChildNode(); ASTNode node = getNode().getFirstChildNode();
while (node != null) { while (node != null) {
@@ -38,7 +38,7 @@ public class JetTypeConstraint extends JetElement {
return null; return null;
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetTypeReference getExtendsTypeReference() { public JetTypeReference getExtendsTypeReference() {
boolean passedColon = false; boolean passedColon = false;
ASTNode node = getNode().getFirstChildNode(); ASTNode node = getNode().getFirstChildNode();
@@ -21,6 +21,7 @@ public class JetTypeParameterListOwner extends JetNamedDeclaration {
return (JetTypeParameterList) findChildByType(JetNodeTypes.TYPE_PARAMETER_LIST); return (JetTypeParameterList) findChildByType(JetNodeTypes.TYPE_PARAMETER_LIST);
} }
@NotNull
public List<JetTypeParameter> getTypeParameters() { public List<JetTypeParameter> getTypeParameters() {
JetTypeParameterList list = getTypeParameterList(); JetTypeParameterList list = getTypeParameterList();
if (list == null) return Collections.emptyList(); if (list == null) return Collections.emptyList();
@@ -18,7 +18,7 @@ public class JetTypedef extends JetTypeParameterListOwner {
visitor.visitTypedef(this); visitor.visitTypedef(this);
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetTypeReference getTypeReference() { public JetTypeReference getTypeReference() {
return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE); return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
} }
@@ -18,11 +18,12 @@ public class JetWhileExpression extends JetExpression {
visitor.visitWhileExpression(this); visitor.visitWhileExpression(this);
} }
@Nullable @Nullable(IF_NOT_PARSED)
public JetExpression getCondition() { public JetExpression getCondition() {
return findExpressionUnder(JetNodeTypes.CONDITION); return findExpressionUnder(JetNodeTypes.CONDITION);
} }
@Nullable(IF_NOT_PARSED)
public JetExpression getBody() { public JetExpression getBody() {
return findExpressionUnder(JetNodeTypes.BODY); return findExpressionUnder(JetNodeTypes.BODY);
} }
@@ -4,22 +4,24 @@
package org.jetbrains.jet.parsing; package org.jetbrains.jet.parsing;
import com.intellij.openapi.application.PathManager; import com.intellij.openapi.application.PathManager;
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.testFramework.ParsingTestCase; import com.intellij.testFramework.ParsingTestCase;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NonNls;
import org.jetbrains.jet.lang.psi.JetBinaryExpression;
import org.jetbrains.jet.lang.psi.JetElement; import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetVisitor; import org.jetbrains.jet.lang.psi.JetVisitor;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
public class JetParsingTest extends ParsingTestCase { public class JetParsingTest extends ParsingTestCase {
@@ -49,7 +51,6 @@ public class JetParsingTest extends ParsingTestCase {
@Override @Override
protected void checkResult(@NonNls String targetDataName, PsiFile file) throws IOException { protected void checkResult(@NonNls String targetDataName, PsiFile file) throws IOException {
super.checkResult(targetDataName, file);
file.acceptChildren(new JetVisitor() { file.acceptChildren(new JetVisitor() {
@Override @Override
public void visitJetElement(JetElement elem) { public void visitJetElement(JetElement elem) {
@@ -60,7 +61,20 @@ public class JetParsingTest extends ParsingTestCase {
throw new RuntimeException(throwable); throw new RuntimeException(throwable);
} }
} }
@Override
public void visitBinaryExpression(JetBinaryExpression expression) {
super.visitBinaryExpression(expression);
JetExpression right = expression.getRight();
JetExpression left = expression.getLeft();
assertNotSame(left, right);
if (right == null) {
assertNotNull("Imcomplete binary operation in parsed OK test", PsiTreeUtil.findChildOfType(expression, PsiErrorElement.class) != null);
}
}
}); });
super.checkResult(targetDataName, file);
} }
private void checkPsiGetters(JetElement elem) throws Throwable { private void checkPsiGetters(JetElement elem) throws Throwable {