Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Intellij Community
Commits
a41f4826
Commit
a41f4826
authored
6 years ago
by
Bas Leijdekkers
1
Browse files
Options
Download
Email Patches
Plain Diff
SSR: fix and improve new array expression matching (IDEA-209438)
parent
cd4d4eda
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java
+30
-10
...ij/structuralsearch/impl/matcher/JavaMatchingVisitor.java
platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralSearchTest.java
+7
-0
...e/com/intellij/structuralsearch/StructuralSearchTest.java
with
37 additions
and
10 deletions
+37
-10
java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java
+
30
-
10
View file @
a41f4826
...
...
@@ -969,10 +969,21 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
}
private
void
matchArrayOrArguments
(
final
PsiNewExpression
new1
,
final
PsiNewExpression
new2
)
{
final
PsiType
type1
=
new1
.
getType
();
final
PsiType
type2
=
new2
.
getType
();
if
(!
myMatchingVisitor
.
setResult
(
type1
!=
null
&&
type2
!=
null
&&
type1
.
getArrayDimensions
()
==
type2
.
getArrayDimensions
()))
return
;
final
PsiArrayInitializerExpression
initializer1
=
new1
.
getArrayInitializer
();
final
PsiArrayInitializerExpression
initializer2
=
new2
.
getArrayInitializer
();
if
(
initializer1
!=
null
)
{
if
(!
myMatchingVisitor
.
setResult
(
myMatchingVisitor
.
matchSons
(
initializer1
,
initializer2
)))
return
;
}
else
if
(
initializer2
!=
null
)
{
myMatchingVisitor
.
setResult
(
areZeroLiterals
(
new1
.
getArrayDimensions
())
&&
initializer2
.
getInitializers
().
length
==
0
);
return
;
}
final
PsiExpression
[]
dimensions1
=
new1
.
getArrayDimensions
();
final
PsiExpression
[]
dimensions2
=
new2
.
getArrayDimensions
();
if
(!
myMatchingVisitor
.
setResult
(
myMatchingVisitor
.
matchSons
(
new1
.
getArrayInitializer
(),
new2
.
getArrayInitializer
())))
return
;
if
(!
myMatchingVisitor
.
setResult
(
dimensions1
.
length
==
dimensions2
.
length
))
return
;
if
(
dimensions1
.
length
!=
0
)
{
for
(
int
i
=
0
;
i
<
dimensions1
.
length
;
++
i
)
{
...
...
@@ -980,14 +991,18 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
}
}
else
{
final
PsiType
type1
=
new1
.
getType
();
final
PsiType
type2
=
new2
.
getType
();
myMatchingVisitor
.
setResult
(
type1
!=
null
&&
type2
!=
null
&&
type1
.
getArrayDimensions
()
==
type2
.
getArrayDimensions
()
&&
myMatchingVisitor
.
matchSons
(
new1
.
getArgumentList
(),
new2
.
getArgumentList
())
&&
myMatchingVisitor
.
setResult
(
myMatchingVisitor
.
matchSons
(
new1
.
getArgumentList
(),
new2
.
getArgumentList
())
&&
myMatchingVisitor
.
setResult
(
matchTypeParameters
(
new1
,
new2
)));
}
}
private
static
boolean
areZeroLiterals
(
PsiExpression
[]
expressions
)
{
for
(
PsiExpression
expression
:
expressions
)
{
if
(!(
expression
instanceof
PsiLiteralExpression
)
||
!
expression
.
getText
().
equals
(
"0"
))
return
false
;
}
return
true
;
}
private
static
boolean
matchImplicitQualifier
(
PsiExpression
qualifier
,
PsiElement
reference
,
MatchContext
context
)
{
final
PsiElement
target
=
reference
instanceof
PsiMethodCallExpression
?
((
PsiMethodCallExpression
)
reference
).
resolveMethod
()
...
...
@@ -1431,9 +1446,8 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
final
PsiJavaCodeReferenceElement
classReference
=
new1
.
getClassReference
();
if
(
other
instanceof
PsiArrayInitializerExpression
&&
other
.
getParent
()
instanceof
PsiVariable
&&
new1
.
getArrayDimensions
().
length
==
0
&&
new1
.
getArrayInitializer
()
!=
null
)
{
areZeroLiterals
(
new1
.
getArrayDimensions
())
)
{
final
MatchContext
matchContext
=
myMatchingVisitor
.
getMatchContext
();
final
CompiledPattern
pattern
=
matchContext
.
getPattern
();
final
boolean
isTypedVar
=
pattern
.
isTypedVar
(
classReference
);
...
...
@@ -1456,7 +1470,13 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
myMatchingVisitor
.
setResult
(
type
!=
null
&&
type
.
equals
(
otherType
));
}
if
(
myMatchingVisitor
.
getResult
())
{
myMatchingVisitor
.
matchSons
(
new1
.
getArrayInitializer
(),
other
);
PsiArrayInitializerExpression
initializer
=
new1
.
getArrayInitializer
();
if
(
initializer
!=
null
)
{
myMatchingVisitor
.
matchSons
(
initializer
,
other
);
}
else
{
myMatchingVisitor
.
setResult
(((
PsiArrayInitializerExpression
)
other
).
getInitializers
().
length
==
0
);
}
}
return
;
}
...
...
This diff is collapsed.
Click to expand it.
platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralSearchTest.java
+
7
-
0
View file @
a41f4826
...
...
@@ -248,6 +248,13 @@ public class StructuralSearchTest extends StructuralSearchTestCase {
"}}"
;
assertEquals
(
"Find 2 dimensional array"
,
1
,
findMatchesCount
(
multiDimensional
,
"new String[][]{}"
));
assertEquals
(
"Find 1 dimensional arrays"
,
2
,
findMatchesCount
(
multiDimensional
,
"new String[]{}"
));
assertEquals
(
"Find empty 1 dimensional arrays"
,
2
,
findMatchesCount
(
multiDimensional
,
"new String[0]"
));
String
singleDimensional
=
"class X {{"
+
" String[] ss1 = new String[0];"
+
" String[][] ss2 = new String[0][];"
+
"}}"
;
assertEquals
(
"Find empty array"
,
1
,
findMatchesCount
(
singleDimensional
,
"new String[0]"
));
}
public
void
testLiteral
()
{
...
...
This diff is collapsed.
Click to expand it.
小 白蛋
@baidan
mentioned in commit
2a28ff2c
·
2 years ago
mentioned in commit
2a28ff2c
mentioned in commit 2a28ff2c76b0b88d1b8e0e8e0f202d4d69e9fb5f
Toggle commit list
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help