| Method from com.sun.tools.javac.comp.MemberEnter Detail: |
JCTree DefaultConstructor(TreeMaker make,
ClassSymbol c,
List<Type> typarams,
List<Type> argtypes,
List<Type> thrown,
long flags,
boolean based) {
List< JCVariableDecl > params = make.Params(argtypes, syms.noSymbol);
List< JCStatement > stats = List.nil();
if (c.type != syms.objectType)
stats = stats.prepend(SuperCall(make, typarams, params, based));
if ((c.flags() & ENUM) != 0 &&
(types.supertype(c.type).tsym == syms.enumSym ||
target.compilerBootstrap(c))) {
// constructors of true enums are private
flags = (flags & ~AccessFlags) | PRIVATE | GENERATEDCONSTR;
} else
flags |= (c.flags() & AccessFlags) | GENERATEDCONSTR;
if (c.name.isEmpty()) flags |= ANONCONSTR;
JCTree result = make.MethodDef(
make.Modifiers(flags),
names.init,
null,
make.TypeParams(typarams),
params,
make.Types(thrown),
make.Block(0, stats),
null);
return result;
}
Generate default constructor for given class. For classes different
from java.lang.Object, this is:
c(argtype_0 x_0, ..., argtype_n x_n) throws thrown {
super(x_0, ..., x_n)
}
or, if based == true:
c(argtype_0 x_0, ..., argtype_n x_n) throws thrown {
x_0.super(x_1, ..., x_n)
} |
JCExpressionStatement SuperCall(TreeMaker make,
List<Type> typarams,
List<JCVariableDecl> params,
boolean based) {
JCExpression meth;
if (based) {
meth = make.Select(make.Ident(params.head), names._super);
params = params.tail;
} else {
meth = make.Ident(names._super);
}
List< JCExpression > typeargs = typarams.nonEmpty() ? make.Types(typarams) : null;
return make.Exec(make.Apply(typeargs, meth, make.Idents(params)));
}
Generate call to superclass constructor. This is:
super(id_0, ..., id_n)
or, if based == true
id_0.super(id_1,...,id_n)
where id_0, ..., id_n are the names of the given parameters. |
void annotateDefaultValueLater(JCExpression defaultValue,
Env<AttrContext> localEnv,
MethodSymbol m) {
annotate.later(new Annotate.Annotator() {
public String toString() {
return "annotate " + m.owner + "." +
m + " default " + defaultValue;
}
public void enterAnnotation() {
JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
try {
enterDefaultValue(defaultValue, localEnv, m);
} finally {
log.useSource(prev);
}
}
});
}
Queue processing of an attribute default value. |
void annotateLater(List<JCAnnotation> annotations,
Env<AttrContext> localEnv,
Symbol s) {
if (annotations.isEmpty()) return;
if (s.kind != PCK) s.attributes_field = null; // mark it incomplete for now
annotate.later(new Annotate.Annotator() {
public String toString() {
return "annotate " + annotations + " onto " + s + " in " + s.owner;
}
public void enterAnnotation() {
Assert.check(s.kind == PCK || s.attributes_field == null);
JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
try {
if (s.attributes_field != null &&
s.attributes_field.nonEmpty() &&
annotations.nonEmpty())
log.error(annotations.head.pos,
"already.annotated",
kindName(s), s);
enterAnnotations(annotations, localEnv, s);
} finally {
log.useSource(prev);
}
}
});
}
Queue annotations for later processing. |
Type attribImportType(JCTree tree,
Env<AttrContext> env) {
Assert.check(completionEnabled);
try {
// To prevent deep recursion, suppress completion of some
// types.
completionEnabled = false;
return attr.attribType(tree, env);
} finally {
completionEnabled = true;
}
}
|
public void complete(Symbol sym) throws CompletionFailure {
// Suppress some (recursive) MemberEnter invocations
if (!completionEnabled) {
// Re-install same completer for next time around and return.
Assert.check((sym.flags() & Flags.COMPOUND) == 0);
sym.completer = this;
return;
}
ClassSymbol c = (ClassSymbol)sym;
ClassType ct = (ClassType)c.type;
Env< AttrContext > env = enter.typeEnvs.get(c);
JCClassDecl tree = (JCClassDecl)env.tree;
boolean wasFirst = isFirst;
isFirst = false;
JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
try {
// Save class environment for later member enter (2) processing.
halfcompleted.append(env);
// Mark class as not yet attributed.
c.flags_field |= UNATTRIBUTED;
// If this is a toplevel-class, make sure any preceding import
// clauses have been seen.
if (c.owner.kind == PCK) {
memberEnter(env.toplevel, env.enclosing(JCTree.TOPLEVEL));
todo.append(env);
}
if (c.owner.kind == TYP)
c.owner.complete();
// create an environment for evaluating the base clauses
Env< AttrContext > baseEnv = baseEnv(tree, env);
// Determine supertype.
Type supertype =
(tree.extending != null)
? attr.attribBase(tree.extending, baseEnv, true, false, true)
: ((tree.mods.flags & Flags.ENUM) != 0 && !target.compilerBootstrap(c))
? attr.attribBase(enumBase(tree.pos, c), baseEnv,
true, false, false)
: (c.fullname == names.java_lang_Object)
? Type.noType
: syms.objectType;
ct.supertype_field = modelMissingTypes(supertype, tree.extending, false);
// Determine interfaces.
ListBuffer< Type > interfaces = new ListBuffer< Type >();
ListBuffer< Type > all_interfaces = null; // lazy init
Set< Type > interfaceSet = new HashSet< Type >();
List< JCExpression > interfaceTrees = tree.implementing;
if ((tree.mods.flags & Flags.ENUM) != 0 && target.compilerBootstrap(c)) {
// add interface Comparable< T >
interfaceTrees =
interfaceTrees.prepend(make.Type(new ClassType(syms.comparableType.getEnclosingType(),
List.of(c.type),
syms.comparableType.tsym)));
// add interface Serializable
interfaceTrees =
interfaceTrees.prepend(make.Type(syms.serializableType));
}
for (JCExpression iface : interfaceTrees) {
Type i = attr.attribBase(iface, baseEnv, false, true, true);
if (i.tag == CLASS) {
interfaces.append(i);
if (all_interfaces != null) all_interfaces.append(i);
chk.checkNotRepeated(iface.pos(), types.erasure(i), interfaceSet);
} else {
if (all_interfaces == null)
all_interfaces = new ListBuffer< Type >().appendList(interfaces);
all_interfaces.append(modelMissingTypes(i, iface, true));
}
}
if ((c.flags_field & ANNOTATION) != 0) {
ct.interfaces_field = List.of(syms.annotationType);
ct.all_interfaces_field = ct.interfaces_field;
} else {
ct.interfaces_field = interfaces.toList();
ct.all_interfaces_field = (all_interfaces == null)
? ct.interfaces_field : all_interfaces.toList();
}
if (c.fullname == names.java_lang_Object) {
if (tree.extending != null) {
chk.checkNonCyclic(tree.extending.pos(),
supertype);
ct.supertype_field = Type.noType;
}
else if (tree.implementing.nonEmpty()) {
chk.checkNonCyclic(tree.implementing.head.pos(),
ct.interfaces_field.head);
ct.interfaces_field = List.nil();
}
}
// Annotations.
// In general, we cannot fully process annotations yet, but we
// can attribute the annotation types and then check to see if the
// @Deprecated annotation is present.
attr.attribAnnotationTypes(tree.mods.annotations, baseEnv);
if (hasDeprecatedAnnotation(tree.mods.annotations))
c.flags_field |= DEPRECATED;
annotateLater(tree.mods.annotations, baseEnv, c);
chk.checkNonCyclicDecl(tree);
attr.attribTypeVariables(tree.typarams, baseEnv);
// Add default constructor if needed.
if ((c.flags() & INTERFACE) == 0 &&
!TreeInfo.hasConstructors(tree.defs)) {
List< Type > argtypes = List.nil();
List< Type > typarams = List.nil();
List< Type > thrown = List.nil();
long ctorFlags = 0;
boolean based = false;
if (c.name.isEmpty()) {
JCNewClass nc = (JCNewClass)env.next.tree;
if (nc.constructor != null) {
Type superConstrType = types.memberType(c.type,
nc.constructor);
argtypes = superConstrType.getParameterTypes();
typarams = superConstrType.getTypeArguments();
ctorFlags = nc.constructor.flags() & VARARGS;
if (nc.encl != null) {
argtypes = argtypes.prepend(nc.encl.type);
based = true;
}
thrown = superConstrType.getThrownTypes();
}
}
JCTree constrDef = DefaultConstructor(make.at(tree.pos), c,
typarams, argtypes, thrown,
ctorFlags, based);
tree.defs = tree.defs.prepend(constrDef);
}
// If this is a class, enter symbols for this and super into
// current scope.
if ((c.flags_field & INTERFACE) == 0) {
VarSymbol thisSym =
new VarSymbol(FINAL | HASINIT, names._this, c.type, c);
thisSym.pos = Position.FIRSTPOS;
env.info.scope.enter(thisSym);
if (ct.supertype_field.tag == CLASS) {
VarSymbol superSym =
new VarSymbol(FINAL | HASINIT, names._super,
ct.supertype_field, c);
superSym.pos = Position.FIRSTPOS;
env.info.scope.enter(superSym);
}
}
// check that no package exists with same fully qualified name,
// but admit classes in the unnamed package which have the same
// name as a top-level package.
if (checkClash &&
c.owner.kind == PCK && c.owner != syms.unnamedPackage &&
reader.packageExists(c.fullname))
{
log.error(tree.pos, "clash.with.pkg.of.same.name", Kinds.kindName(sym), c);
}
} catch (CompletionFailure ex) {
chk.completionError(tree.pos(), ex);
} finally {
log.useSource(prev);
}
// Enter all member fields and methods of a set of half completed
// classes in a second phase.
if (wasFirst) {
try {
while (halfcompleted.nonEmpty()) {
finish(halfcompleted.next());
}
} finally {
isFirst = true;
}
// commit pending annotations
annotate.flush();
}
}
Complete entering a class. |
void finishClass(JCClassDecl tree,
Env<AttrContext> env) {
if ((tree.mods.flags & Flags.ENUM) != 0 &&
(types.supertype(tree.sym.type).tsym.flags() & Flags.ENUM) == 0) {
addEnumMembers(tree, env);
}
memberEnter(tree.defs, env);
}
Enter members for a class. |
public Env<AttrContext> getInitEnv(JCVariableDecl tree,
Env<AttrContext> env) {
Env< AttrContext > iEnv = initEnv(tree, env);
return iEnv;
}
|
public Env<AttrContext> getMethodEnv(JCMethodDecl tree,
Env<AttrContext> env) {
Env< AttrContext > mEnv = methodEnv(tree, env);
mEnv.info.lint = mEnv.info.lint.augment(tree.sym.attributes_field, tree.sym.flags());
for (List< JCTypeParameter > l = tree.typarams; l.nonEmpty(); l = l.tail)
mEnv.info.scope.enterIfAbsent(l.head.type.tsym);
for (List< JCVariableDecl > l = tree.params; l.nonEmpty(); l = l.tail)
mEnv.info.scope.enterIfAbsent(l.head.sym);
return mEnv;
}
|
Env<AttrContext> initEnv(JCVariableDecl tree,
Env<AttrContext> env) {
Env< AttrContext > localEnv = env.dupto(new AttrContextEnv(tree, env.info.dup()));
if (tree.sym.owner.kind == TYP) {
localEnv.info.scope = new Scope.DelegatedScope(env.info.scope);
localEnv.info.scope.owner = tree.sym;
}
if ((tree.mods.flags & STATIC) != 0 ||
(env.enclClass.sym.flags() & INTERFACE) != 0)
localEnv.info.staticLevel++;
return localEnv;
}
Create a fresh environment for a variable's initializer.
If the variable is a field, the owner of the environment's scope
is be the variable itself, otherwise the owner is the method
enclosing the variable definition. |
public static MemberEnter instance(Context context) {
MemberEnter instance = context.get(memberEnterKey);
if (instance == null)
instance = new MemberEnter(context);
return instance;
}
|
protected void memberEnter(JCTree tree,
Env<AttrContext> env) {
Env< AttrContext > prevEnv = this.env;
try {
this.env = env;
tree.accept(this);
} catch (CompletionFailure ex) {
chk.completionError(tree.pos(), ex);
} finally {
this.env = prevEnv;
}
}
Enter field and method definitions and process import
clauses, catching any completion failure exceptions. |
void memberEnter(List<JCTree> trees,
Env<AttrContext> env) {
for (List< ? extends JCTree > l = trees; l.nonEmpty(); l = l.tail)
memberEnter(l.head, env);
}
Enter members from a list of trees. |
Env<AttrContext> methodEnv(JCMethodDecl tree,
Env<AttrContext> env) {
Env< AttrContext > localEnv =
env.dup(tree, env.info.dup(env.info.scope.dupUnshared()));
localEnv.enclMethod = tree;
localEnv.info.scope.owner = tree.sym;
if ((tree.mods.flags & STATIC) != 0) localEnv.info.staticLevel++;
return localEnv;
}
Create a fresh environment for method bodies. |
Type modelMissingTypes(Type t,
JCExpression tree,
boolean interfaceExpected) {
if (t.tag != ERROR)
return t;
return new ErrorType(((ErrorType) t).getOriginalType(), t.tsym) {
private Type modelType;
@Override
public Type getModelType() {
if (modelType == null)
modelType = new Synthesizer(getOriginalType(), interfaceExpected).visit(tree);
return modelType;
}
};
}
|
Type signature(List<JCTypeParameter> typarams,
List<JCVariableDecl> params,
JCTree res,
List<JCExpression> thrown,
Env<AttrContext> env) {
// Enter and attribute type parameters.
List< Type > tvars = enter.classEnter(typarams, env);
attr.attribTypeVariables(typarams, env);
// Enter and attribute value parameters.
ListBuffer< Type > argbuf = new ListBuffer< Type >();
for (List< JCVariableDecl > l = params; l.nonEmpty(); l = l.tail) {
memberEnter(l.head, env);
argbuf.append(l.head.vartype.type);
}
// Attribute result type, if one is given.
Type restype = res == null ? syms.voidType : attr.attribType(res, env);
// Attribute thrown exceptions.
ListBuffer< Type > thrownbuf = new ListBuffer< Type >();
for (List< JCExpression > l = thrown; l.nonEmpty(); l = l.tail) {
Type exc = attr.attribType(l.head, env);
if (exc.tag != TYPEVAR)
exc = chk.checkClassType(l.head.pos(), exc);
thrownbuf.append(exc);
}
Type mtype = new MethodType(argbuf.toList(),
restype,
thrownbuf.toList(),
syms.methodClass);
return tvars.isEmpty() ? mtype : new ForAll(tvars, mtype);
}
Construct method type from method signature. |
boolean staticImportAccessible(Symbol sym,
PackageSymbol packge) {
int flags = (int)(sym.flags() & AccessFlags);
switch (flags) {
default:
case PUBLIC:
return true;
case PRIVATE:
return false;
case 0:
case PROTECTED:
return sym.packge() == packge;
}
}
|
public void visitErroneous(JCErroneous tree) {
if (tree.errs != null)
memberEnter(tree.errs, env);
}
|
public void visitImport(JCImport tree) {
JCTree imp = tree.qualid;
Name name = TreeInfo.name(imp);
TypeSymbol p;
// Create a local environment pointing to this tree to disable
// effects of other imports in Resolve.findGlobalType
Env< AttrContext > localEnv = env.dup(tree);
// Attribute qualifying package or class.
JCFieldAccess s = (JCFieldAccess) imp;
p = attr.
attribTree(s.selected,
localEnv,
tree.staticImport ? TYP : (TYP | PCK),
Type.noType).tsym;
if (name == names.asterisk) {
// Import on demand.
chk.checkCanonical(s.selected);
if (tree.staticImport)
importStaticAll(tree.pos, p, env);
else
importAll(tree.pos, p, env);
} else {
// Named type import.
if (tree.staticImport) {
importNamedStatic(tree.pos(), p, name, localEnv);
chk.checkCanonical(s.selected);
} else {
TypeSymbol c = attribImportType(imp, localEnv).tsym;
chk.checkCanonical(imp);
importNamed(tree.pos(), c, env);
}
}
}
|
public void visitMethodDef(JCMethodDecl tree) {
Scope enclScope = enter.enterScope(env);
MethodSymbol m = new MethodSymbol(0, tree.name, null, enclScope.owner);
m.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, m, tree);
tree.sym = m;
Env< AttrContext > localEnv = methodEnv(tree, env);
DeferredLintHandler prevLintHandler =
chk.setDeferredLintHandler(deferredLintHandler.setPos(tree.pos()));
try {
// Compute the method type
m.type = signature(tree.typarams, tree.params,
tree.restype, tree.thrown,
localEnv);
} finally {
chk.setDeferredLintHandler(prevLintHandler);
}
// Set m.params
ListBuffer< VarSymbol > params = new ListBuffer< VarSymbol >();
JCVariableDecl lastParam = null;
for (List< JCVariableDecl > l = tree.params; l.nonEmpty(); l = l.tail) {
JCVariableDecl param = lastParam = l.head;
params.append(Assert.checkNonNull(param.sym));
}
m.params = params.toList();
// mark the method varargs, if necessary
if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0)
m.flags_field |= Flags.VARARGS;
localEnv.info.scope.leave();
if (chk.checkUnique(tree.pos(), m, enclScope)) {
enclScope.enter(m);
}
annotateLater(tree.mods.annotations, localEnv, m);
if (tree.defaultValue != null)
annotateDefaultValueLater(tree.defaultValue, localEnv, m);
}
|
public void visitTopLevel(JCCompilationUnit tree) {
if (tree.starImportScope.elems != null) {
// we must have already processed this toplevel
return;
}
// check that no class exists with same fully qualified name as
// toplevel package
if (checkClash && tree.pid != null) {
Symbol p = tree.packge;
while (p.owner != syms.rootPackage) {
p.owner.complete(); // enter all class members of p
if (syms.classes.get(p.getQualifiedName()) != null) {
log.error(tree.pos,
"pkg.clashes.with.class.of.same.name",
p);
}
p = p.owner;
}
}
// process package annotations
annotateLater(tree.packageAnnotations, env, tree.packge);
// Import-on-demand java.lang.
importAll(tree.pos, reader.enterPackage(names.java_lang), env);
// Process all import clauses.
memberEnter(tree.defs, env);
}
|
public void visitTree(JCTree tree) {
}
Default member enter visitor method: do nothing |
public void visitVarDef(JCVariableDecl tree) {
Env< AttrContext > localEnv = env;
if ((tree.mods.flags & STATIC) != 0 ||
(env.info.scope.owner.flags() & INTERFACE) != 0) {
localEnv = env.dup(tree, env.info.dup());
localEnv.info.staticLevel++;
}
DeferredLintHandler prevLintHandler =
chk.setDeferredLintHandler(deferredLintHandler.setPos(tree.pos()));
try {
attr.attribType(tree.vartype, localEnv);
} finally {
chk.setDeferredLintHandler(prevLintHandler);
}
if ((tree.mods.flags & VARARGS) != 0) {
//if we are entering a varargs parameter, we need to replace its type
//(a plain array type) with the more precise VarargsType --- we need
//to do it this way because varargs is represented in the tree as a modifier
//on the parameter declaration, and not as a distinct type of array node.
ArrayType atype = (ArrayType)tree.vartype.type;
tree.vartype.type = atype.makeVarargs();
}
Scope enclScope = enter.enterScope(env);
VarSymbol v =
new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner);
v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree);
tree.sym = v;
if (tree.init != null) {
v.flags_field |= HASINIT;
if ((v.flags_field & FINAL) != 0 && tree.init.getTag() != JCTree.NEWCLASS) {
Env< AttrContext > initEnv = getInitEnv(tree, env);
initEnv.info.enclVar = v;
v.setLazyConstValue(initEnv(tree, initEnv), attr, tree.init);
}
}
if (chk.checkUnique(tree.pos(), v, enclScope)) {
chk.checkTransparentVar(tree.pos(), v, enclScope);
enclScope.enter(v);
}
annotateLater(tree.mods.annotations, localEnv, v);
v.pos = tree.pos;
}
|