more useful parse of broken try-with-resources resource expression (IDEA-157118)

This commit is contained in:
Bas Leijdekkers
2016-06-10 19:39:18 +03:00
parent ad12f0764b
commit fe0824b7e2
7 changed files with 65 additions and 3 deletions
@@ -1784,7 +1784,8 @@ public class HighlightUtil extends HighlightUtilBase {
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(text).create();
}
return null;
String text = JavaErrorMessages.message("declaration.or.variable.expected");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(text).create();
}
@Nullable
@@ -49,7 +49,8 @@ public class DeclarationParser {
private static final TokenSet TYPE_START = TokenSet.orSet(
ElementType.PRIMITIVE_TYPE_BIT_SET, TokenSet.create(JavaTokenType.IDENTIFIER, JavaTokenType.AT));
private static final TokenSet RESOURCE_EXPRESSIONS = TokenSet.create(
JavaElementType.REFERENCE_EXPRESSION, JavaElementType.THIS_EXPRESSION);
JavaElementType.REFERENCE_EXPRESSION, JavaElementType.THIS_EXPRESSION,
JavaElementType.METHOD_CALL_EXPRESSION, JavaElementType.NEW_EXPRESSION);
private static final String WHITESPACES = "\n\r \t";
private static final String LINE_ENDS = "\n\r";
@@ -145,6 +145,7 @@ variable.must.be.final=Variable ''{0}'' is accessed from within inner class, nee
variable.must.be.final.or.effectively.final=Variable ''{0}'' is accessed from within inner class, needs to be final or effectively final
lambda.variable.must.be.final=Variable used in lambda expression should be final or effectively final
resource.variable.must.be.final=Variable used as a try-with-resources resource should be final or effectively final
declaration.or.variable.expected=Declaration, final or effectively final variable expected
initializer.must.be.able.to.complete.normally=Initializer must be able to complete normally
weaker.privileges={0}; attempting to assign weaker access privileges (''{1}''); was ''{2}''
incompatible.return.type=attempting to use incompatible return type
@@ -42,4 +42,16 @@ class TryWithResources {
System.out.println(r);
}
}
static class Resource implements AutoCloseable {
static Resource create() {
return new Resource();
}
@Override public void close() { }
}
void testMoreUsefulParseForIncorrectExpression() {
try (<error descr="Declaration, final or effectively final variable expected">new Resource()</error>) {}
try (<error descr="Declaration, final or effectively final variable expected">Resource.create()</error>) {}
}
}
@@ -0,0 +1,22 @@
PsiJavaFile:TryNormal11.java
PsiTryStatement
PsiKeyword:try('try')
PsiResourceList:(new R())
PsiJavaToken:LPARENTH('(')
PsiResourceExpression
PsiNewExpression:new R()
PsiKeyword:new('new')
PsiReferenceParameterList
<empty list>
PsiWhiteSpace(' ')
PsiJavaCodeReferenceElement:R
PsiIdentifier:R('R')
PsiReferenceParameterList
<empty list>
PsiExpressionList
PsiJavaToken:LPARENTH('(')
PsiJavaToken:RPARENTH(')')
PsiJavaToken:RPARENTH(')')
PsiCodeBlock
PsiJavaToken:LBRACE('{')
PsiJavaToken:RBRACE('}')
@@ -0,0 +1,23 @@
PsiJavaFile:TryNormal12.java
PsiTryStatement
PsiKeyword:try('try')
PsiResourceList:(R.create())
PsiJavaToken:LPARENTH('(')
PsiResourceExpression
PsiMethodCallExpression:R.create()
PsiReferenceExpression:R.create
PsiReferenceExpression:R
PsiReferenceParameterList
<empty list>
PsiIdentifier:R('R')
PsiJavaToken:DOT('.')
PsiReferenceParameterList
<empty list>
PsiIdentifier:create('create')
PsiExpressionList
PsiJavaToken:LPARENTH('(')
PsiJavaToken:RPARENTH(')')
PsiJavaToken:RPARENTH(')')
PsiCodeBlock
PsiJavaToken:LBRACE('{')
PsiJavaToken:RBRACE('}')
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -145,6 +145,8 @@ public class StatementParserTest extends JavaParsingTestCase {
public void testTryIncomplete16() { doParserTest("try(R r =){}"); }
public void testTryIncomplete17() { doParserTest("try(R r = 0;;){}"); }
public void testTryIncomplete18() { doParserTest("try(R<T> r){}"); }
public void testTryNormal11() { doParserTest("try(new R()){}"); }
public void testTryNormal12() { doParserTest("try(R.create()){}"); }
public void testWhileNormal() { doParserTest("while (true) foo();"); }
public void testWhileIncomplete0() { doParserTest("while"); }