GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — processing4/java/src/processing/mode/java/ASTUtils.java at main · D-I-R-M/processing4

Expand file tree

/

ASTUtils.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
184 lines (155 loc) · 5.61 KB
/

ASTUtils.java

Copy path

File metadata and controls

184 lines (155 loc) · 5.61 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package processing.mode.java;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.NodeFinder;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import processing.app.Messages;
public class ASTUtils {
public static ASTNode getASTNodeAt(ASTNode root, int startJavaOffset, int stopJavaOffset) {
Messages.log("* getASTNodeAt");
int length = stopJavaOffset - startJavaOffset;
NodeFinder f = new NodeFinder(root, startJavaOffset, length);
ASTNode node = f.getCoveredNode();
if (node == null) {
node = f.getCoveringNode();
}
if (node == null) {
Messages.log("no node found");
} else {
Messages.log("found " + node.getClass().getSimpleName());
}
return node;
}
public static SimpleName getSimpleNameAt(ASTNode root, int startJavaOffset, int stopJavaOffset) {
Messages.log("* getSimpleNameAt");
// Find node at offset
ASTNode node = getASTNodeAt(root, startJavaOffset, stopJavaOffset);
SimpleName result = null;
if (node != null) {
if (node.getNodeType() == ASTNode.SIMPLE_NAME) {
result = (SimpleName) node;
} else {
// Return SimpleName with highest coverage
List<SimpleName> simpleNames = getSimpleNameChildren(node);
if (!simpleNames.isEmpty()) {
// Compute coverage <selection x node>
int[] coverages = simpleNames.stream()
.mapToInt(name -> {
int start = name.getStartPosition();
int stop = start + name.getLength();
return Math.min(stop, stopJavaOffset) -
Math.max(startJavaOffset, start);
})
.toArray();
// Select node with highest coverage
int maxIndex = IntStream.range(0, simpleNames.size())
.filter(i -> coverages[i] >= 0)
.reduce((i, j) -> coverages[i] > coverages[j] ? i : j)
.orElse(-1);
if (maxIndex == -1) return null;
result = simpleNames.get(maxIndex);
}
}
}
if (result == null) {
Messages.log("no simple name found");
} else {
Messages.log("found " + node);
}
return result;
}
public static List<SimpleName> getSimpleNameChildren(ASTNode node) {
List<SimpleName> simpleNames = new ArrayList<>();
node.accept(new ASTVisitor() {
@Override
public boolean visit(SimpleName simpleName) {
simpleNames.add(simpleName);
return super.visit(simpleName);
}
});
return simpleNames;
}
public static IBinding resolveBinding(SimpleName node) {
IBinding binding = node.resolveBinding();
if (binding == null) return null;
// Fix constructor call/declaration being resolved as type
if (binding.getKind() == IBinding.TYPE) {
ASTNode context = node;
// Go up until we find non Name or Type node
// stop if context is type argument (parent is also Name/Type, but unrelated)
while (isNameOrType(context) &&
!context.getLocationInParent().getId().equals("typeArguments")) {
context = context.getParent();
}
switch (context.getNodeType()) {
case ASTNode.METHOD_DECLARATION:
MethodDeclaration decl = (MethodDeclaration) context;
if (decl.isConstructor()) {
binding = decl.resolveBinding();
}
break;
case ASTNode.CLASS_INSTANCE_CREATION:
ClassInstanceCreation cic = (ClassInstanceCreation) context;
binding = cic.resolveConstructorBinding();
break;
}
}
if (binding == null) return null;
// Normalize parametrized and raw bindings into generic bindings
switch (binding.getKind()) {
case IBinding.TYPE:
ITypeBinding type = (ITypeBinding) binding;
if (type.isParameterizedType() || type.isRawType()) {
binding = type.getErasure();
}
break;
case IBinding.METHOD:
IMethodBinding method = (IMethodBinding) binding;
ITypeBinding declaringClass = method.getDeclaringClass();
if (declaringClass.isParameterizedType() ||
declaringClass.isRawType()) {
IMethodBinding[] methods = declaringClass.getErasure().getDeclaredMethods();
IMethodBinding generic = Arrays.stream(methods)
.filter(method::overrides)
.findAny().orElse(null);
if (generic != null) method = generic;
}
if (method.isParameterizedMethod() || method.isRawMethod()) {
method = method.getMethodDeclaration();
}
binding = method;
break;
}
return binding;
}
public static boolean isNameOrType(ASTNode node) {
return node instanceof Name || node instanceof Type;
}
public static List<SimpleName> findAllOccurrences(ASTNode root,
String bindingKey
) {
List<SimpleName> occurrences = new ArrayList<>();
root.getRoot().accept(new ASTVisitor() {
@Override
public boolean visit(SimpleName name) {
IBinding binding = resolveBinding(name);
if (binding != null && bindingKey.equals(binding.getKey())) {
occurrences.add(name);
}
return super.visit(name);
}
});
return occurrences;
}
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.