forked from pmd/pmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASTMethod.java
144 lines (119 loc) · 4.19 KB
/
ASTMethod.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
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.apex.ast;
import java.util.List;
import java.util.stream.Collectors;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.lang.document.TextFileContent;
import net.sourceforge.pmd.lang.document.TextRegion;
import com.google.summit.ast.SourceLocation;
import com.google.summit.ast.declaration.MethodDeclaration;
public final class ASTMethod extends AbstractApexNode implements ApexQualifiableNode {
// Store the details instead of wrapping a com.google.summit.ast.Node.
// This is to allow synthetic ASTMethod nodes.
// An example is the trigger `invoke` method.
private String name;
private List<String> parameterTypes;
private String returnType;
private SourceLocation sourceLocation;
/**
* Internal name used by constructors.
*/
public static final String CONSTRUCTOR_ID = "<init>";
/**
* Internal name used by static initialization blocks.
*/
public static final String STATIC_INIT_ID = "<clinit>";
public ASTMethod(
String name,
List<String> parameterTypes,
String returnType,
SourceLocation sourceLocation) {
this.name = name;
this.parameterTypes = parameterTypes;
this.returnType = returnType;
this.sourceLocation = sourceLocation;
}
public static ASTMethod fromNode(MethodDeclaration node) {
String name = node.getId().getString();
if (node.isAnonymousInitializationCode()) {
name = STATIC_INIT_ID;
} else if (node.isConstructor()) {
name = CONSTRUCTOR_ID;
}
return new ASTMethod(
name,
node.getParameterDeclarations().stream()
.map(p -> caseNormalizedTypeIfPrimitive(p.getType().asCodeString()))
.collect(Collectors.toList()),
caseNormalizedTypeIfPrimitive(node.getReturnType().asCodeString()),
node.getSourceLocation());
}
@Override
protected <P, R> R acceptApexVisitor(ApexVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data);
}
@Override
void calculateTextRegion(TextFileContent sourceContent) {
if (sourceLocation.isUnknown()) {
return;
}
// Column+1 because Summit columns are 0-based and PMD are 1-based
setRegion(TextRegion.fromBothOffsets(
sourceContent.offsetFromLineColumn(sourceLocation.getStartLine(),
sourceLocation.getStartColumn()+1),
sourceContent.offsetFromLineColumn(sourceLocation.getEndLine(),
sourceLocation.getEndColumn()+1)
));
}
@Override
public boolean hasRealLoc() {
return !sourceLocation.isUnknown();
}
@Override
public String getImage() {
if (isConstructor()) {
ASTUserClass classNode = getFirstParentOfType(ASTUserClass.class);
if (classNode != null) {
return classNode.getSimpleName();
}
}
return getCanonicalName();
}
public String getCanonicalName() {
if (getParent() instanceof ASTProperty) {
return ASTProperty.formatAccessorName((ASTProperty) getParent());
}
return name;
}
@Override
public ApexQualifiedName getQualifiedName() {
return ApexQualifiedName.ofMethod(this);
}
/**
* Returns true if this is a synthetic class initializer, inserted
* by the parser.
*/
public boolean isSynthetic() {
return getImage().matches("<clinit>|<init>|clone");
}
public boolean isConstructor() {
return CONSTRUCTOR_ID.equals(name);
}
public ASTModifierNode getModifiers() {
return getFirstChildOfType(ASTModifierNode.class);
}
/**
* Returns the method return type name.
*
* This includes any type arguments.
* If the type is a primitive, its case will be normalized.
*/
public String getReturnType() {
return returnType;
}
public int getArity() {
return parameterTypes.size();
}
}