forked from javaparser/javaparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaParserFactory.java
161 lines (153 loc) · 8.48 KB
/
JavaParserFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2020 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver.javaparsermodel;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.*;
import com.github.javaparser.ast.expr.*;
import com.github.javaparser.ast.stmt.*;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.resolution.Context;
import com.github.javaparser.resolution.SymbolDeclarator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.contexts.*;
import com.github.javaparser.symbolsolver.javaparsermodel.declarators.*;
import static com.github.javaparser.resolution.Navigator.demandParentNode;
/**
* @author Federico Tomassetti
*/
public class JavaParserFactory {
public static Context getContext(Node node, TypeSolver typeSolver) {
if (node == null) {
throw new NullPointerException("Node should not be null");
}
// TODO: Is order important here?
if (node instanceof ArrayAccessExpr) {
return new ArrayAccessExprContext((ArrayAccessExpr) node, typeSolver);
} else if (node instanceof AnnotationDeclaration) {
return new AnnotationDeclarationContext((AnnotationDeclaration) node, typeSolver);
} else if (node instanceof BinaryExpr) {
return new BinaryExprContext((BinaryExpr) node, typeSolver);
} else if (node instanceof BlockStmt) {
return new BlockStmtContext((BlockStmt) node, typeSolver);
} else if (node instanceof CompilationUnit) {
return new CompilationUnitContext((CompilationUnit) node, typeSolver);
} else if (node instanceof EnclosedExpr) {
return new EnclosedExprContext((EnclosedExpr) node, typeSolver);
} else if (node instanceof ForEachStmt) {
return new ForEachStatementContext((ForEachStmt) node, typeSolver);
} else if (node instanceof ForStmt) {
return new ForStatementContext((ForStmt) node, typeSolver);
} else if (node instanceof IfStmt) {
return new IfStatementContext((IfStmt) node, typeSolver);
} else if (node instanceof InstanceOfExpr) {
return new InstanceOfExprContext((InstanceOfExpr) node, typeSolver);
} else if (node instanceof LambdaExpr) {
return new LambdaExprContext((LambdaExpr) node, typeSolver);
} else if (node instanceof MethodDeclaration) {
return new MethodContext((MethodDeclaration) node, typeSolver);
} else if (node instanceof ConstructorDeclaration) {
return new ConstructorContext((ConstructorDeclaration) node, typeSolver);
} else if (node instanceof ClassOrInterfaceDeclaration) {
return new ClassOrInterfaceDeclarationContext((ClassOrInterfaceDeclaration) node, typeSolver);
} else if (node instanceof MethodCallExpr) {
return new MethodCallExprContext((MethodCallExpr) node, typeSolver);
} else if (node instanceof MethodReferenceExpr) {
return new MethodReferenceExprContext((MethodReferenceExpr) node, typeSolver);
} else if (node instanceof EnumDeclaration) {
return new EnumDeclarationContext((EnumDeclaration) node, typeSolver);
} else if (node instanceof FieldAccessExpr) {
return new FieldAccessContext((FieldAccessExpr) node, typeSolver);
} else if (node instanceof SwitchEntry) {
return new SwitchEntryContext((SwitchEntry) node, typeSolver);
} else if (node instanceof TryStmt) {
return new TryWithResourceContext((TryStmt) node, typeSolver);
} else if (node instanceof Statement) {
return new StatementContext<>((Statement) node, typeSolver);
} else if (node instanceof CatchClause) {
return new CatchClauseContext((CatchClause) node, typeSolver);
} else if (node instanceof UnaryExpr) {
return new UnaryExprContext((UnaryExpr) node, typeSolver);
} else if (node instanceof VariableDeclarator) {
return new VariableDeclaratorContext((VariableDeclarator) node, typeSolver);
} else if (node instanceof VariableDeclarationExpr) {
return new VariableDeclarationExprContext((VariableDeclarationExpr) node, typeSolver);
} else if (node instanceof ObjectCreationExpr &&
((ObjectCreationExpr) node).getAnonymousClassBody().isPresent()) {
return new AnonymousClassDeclarationContext((ObjectCreationExpr) node, typeSolver);
} else if (node instanceof ObjectCreationExpr) {
return new ObjectCreationContext((ObjectCreationExpr)node, typeSolver);
} else {
if (node instanceof NameExpr) {
// to resolve a name when in a fieldAccess context, we can go up until we get a node other than FieldAccessExpr,
// in order to prevent a infinite loop if the name is the same as the field (ie x.x, x.y.x, or x.y.z.x)
if (node.getParentNode().isPresent() && node.getParentNode().get() instanceof FieldAccessExpr) {
Node ancestor = node.getParentNode().get();
while (ancestor.getParentNode().isPresent()) {
ancestor = ancestor.getParentNode().get();
if (!(ancestor instanceof FieldAccessExpr)) {
break;
}
}
return getContext(ancestor, typeSolver);
}
if (node.getParentNode().isPresent() && node.getParentNode().get() instanceof ObjectCreationExpr && node.getParentNode().get().getParentNode().isPresent()) {
return getContext(node.getParentNode().get().getParentNode().get(), typeSolver);
}
}
final Node parentNode = demandParentNode(node);
if (node instanceof ClassOrInterfaceType && parentNode instanceof ClassOrInterfaceDeclaration) {
ClassOrInterfaceDeclaration parentDeclaration = (ClassOrInterfaceDeclaration) parentNode;
if (parentDeclaration.getImplementedTypes().contains(node) ||
parentDeclaration.getExtendedTypes().contains(node)) {
// When resolving names in implements and extends the body of the declaration
// should not be searched so use limited context.
return new ClassOrInterfaceDeclarationExtendsContext(parentDeclaration, typeSolver);
}
}
return getContext(parentNode, typeSolver);
}
}
public static SymbolDeclarator getSymbolDeclarator(Node node, TypeSolver typeSolver) {
if (node instanceof FieldDeclaration) {
return new FieldSymbolDeclarator((FieldDeclaration) node, typeSolver);
}
if (node instanceof Parameter) {
return new ParameterSymbolDeclarator((Parameter) node, typeSolver);
}
if (node instanceof PatternExpr) {
return new PatternSymbolDeclarator((PatternExpr) node, typeSolver);
}
if (node instanceof ExpressionStmt) {
ExpressionStmt expressionStmt = (ExpressionStmt) node;
if (expressionStmt.getExpression() instanceof VariableDeclarationExpr) {
return new VariableSymbolDeclarator((VariableDeclarationExpr) (expressionStmt.getExpression()),
typeSolver);
}
return new NoSymbolDeclarator<>(expressionStmt, typeSolver);
}
if (node instanceof ForEachStmt) {
ForEachStmt foreachStmt = (ForEachStmt) node;
return new VariableSymbolDeclarator(foreachStmt.getVariable(), typeSolver);
}
return new NoSymbolDeclarator<>(node, typeSolver);
}
}