Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/JavadocMemberEnter.java
41161 views
1
/*
2
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package jdk.javadoc.internal.tool;
27
28
import com.sun.source.util.TreePath;
29
import com.sun.tools.javac.code.Flags;
30
import com.sun.tools.javac.code.Symbol.*;
31
import com.sun.tools.javac.comp.MemberEnter;
32
import com.sun.tools.javac.tree.JCTree;
33
import com.sun.tools.javac.tree.JCTree.*;
34
import com.sun.tools.javac.tree.TreeInfo;
35
import com.sun.tools.javac.util.Context;
36
import com.sun.tools.javac.util.List;
37
38
import static com.sun.tools.javac.code.Flags.*;
39
import static com.sun.tools.javac.code.Kinds.Kind.*;
40
41
/**
42
* Javadoc's own memberEnter phase does a few things above and beyond that
43
* done by javac.
44
*
45
* <p><b>This is NOT part of any supported API.
46
* If you write code that depends on this, you do so at your own risk.
47
* This code and its internal interfaces are subject to change or
48
* deletion without notice.</b>
49
*/
50
public class JavadocMemberEnter extends MemberEnter {
51
public static JavadocMemberEnter instance0(Context context) {
52
MemberEnter instance = context.get(memberEnterKey);
53
if (instance == null)
54
instance = new JavadocMemberEnter(context);
55
return (JavadocMemberEnter)instance;
56
}
57
58
public static void preRegister(Context context) {
59
context.put(memberEnterKey, (Context.Factory<MemberEnter>)JavadocMemberEnter::new);
60
}
61
62
final ToolEnvironment toolEnv;
63
64
65
protected JavadocMemberEnter(Context context) {
66
super(context);
67
toolEnv = ToolEnvironment.instance(context);
68
}
69
70
@Override
71
public void visitMethodDef(JCMethodDecl tree) {
72
super.visitMethodDef(tree);
73
MethodSymbol meth = tree.sym;
74
if (meth == null || meth.kind != MTH) return;
75
TreePath treePath = toolEnv.getTreePath(env.toplevel, env.enclClass, tree);
76
// do not add those methods that may be mandated by the spec,
77
// or those that are synthesized, thus if it does not exist in
78
// tree best to let other logic determine the TreePath.
79
if (env.enclClass.defs.contains(tree)) {
80
toolEnv.setElementToTreePath(meth, treePath);
81
}
82
// release resources
83
// handle constructors for record types specially, because of downstream checks
84
if ((env.enclClass.mods.flags & Flags.RECORD) != 0 && TreeInfo.isConstructor(tree)) {
85
tree.body.stats = List.nil();
86
} else {
87
tree.body = null;
88
}
89
}
90
91
@Override
92
public void visitVarDef(JCVariableDecl tree) {
93
if (tree.init != null) {
94
boolean isFinal = (tree.mods.flags & FINAL) != 0
95
|| (env.enclClass.mods.flags & INTERFACE) != 0;
96
if (!isFinal || containsNonConstantExpression(tree.init)) {
97
// Avoid unnecessary analysis and release resources.
98
// In particular, remove non-constant expressions
99
// which may trigger Attr.attribClass, since
100
// method bodies are also removed, in visitMethodDef.
101
tree.init = null;
102
}
103
}
104
super.visitVarDef(tree);
105
if (tree.sym != null && tree.sym.kind == VAR && !isParameter(tree.sym)) {
106
toolEnv.setElementToTreePath(tree.sym, toolEnv.getTreePath(env.toplevel, env.enclClass, tree));
107
}
108
}
109
110
private static boolean isParameter(VarSymbol var) {
111
return (var.flags() & Flags.PARAMETER) != 0;
112
}
113
114
/**
115
* Simple analysis of an expression tree to see if it contains tree nodes
116
* for any non-constant expression. This does not include checking references
117
* to other fields which may or may not be constant.
118
*/
119
private static boolean containsNonConstantExpression(JCExpression tree) {
120
return new MaybeConstantExpressionScanner().containsNonConstantExpression(tree);
121
}
122
123
/**
124
* See JLS 15.18, Constant Expression
125
*/
126
private static class MaybeConstantExpressionScanner extends JCTree.Visitor {
127
boolean maybeConstantExpr = true;
128
129
public boolean containsNonConstantExpression(JCExpression tree) {
130
scan(tree);
131
return !maybeConstantExpr;
132
}
133
134
public void scan(JCTree tree) {
135
// short circuit scan when end result is definitely false
136
if (maybeConstantExpr && tree != null)
137
tree.accept(this);
138
}
139
140
@Override
141
/** default for any non-overridden visit method. */
142
public void visitTree(JCTree tree) {
143
maybeConstantExpr = false;
144
}
145
146
@Override
147
public void visitBinary(JCBinary tree) {
148
switch (tree.getTag()) {
149
case MUL: case DIV: case MOD:
150
case PLUS: case MINUS:
151
case SL: case SR: case USR:
152
case LT: case LE: case GT: case GE:
153
case EQ: case NE:
154
case BITAND: case BITXOR: case BITOR:
155
case AND: case OR:
156
break;
157
default:
158
maybeConstantExpr = false;
159
}
160
}
161
162
@Override
163
public void visitConditional(JCConditional tree) {
164
scan(tree.cond);
165
scan(tree.truepart);
166
scan(tree.falsepart);
167
}
168
169
@Override
170
public void visitIdent(JCIdent tree) { }
171
172
@Override
173
public void visitLiteral(JCLiteral tree) { }
174
175
@Override
176
public void visitParens(JCParens tree) {
177
scan(tree.expr);
178
}
179
180
@Override
181
public void visitSelect(JCTree.JCFieldAccess tree) {
182
scan(tree.selected);
183
}
184
185
@Override
186
public void visitTypeCast(JCTypeCast tree) {
187
scan(tree.clazz);
188
scan(tree.expr);
189
}
190
191
@Override
192
public void visitTypeIdent(JCPrimitiveTypeTree tree) { }
193
194
@Override
195
public void visitUnary(JCUnary tree) {
196
switch (tree.getTag()) {
197
case POS: case NEG: case COMPL: case NOT:
198
break;
199
default:
200
maybeConstantExpr = false;
201
}
202
}
203
}
204
}
205
206