use of com.sun.tools.javac.code.Scope in project ceylon-compiler by ceylon.
the class ClassDocImpl method searchClass.
private ClassDoc searchClass(String className) {
Names names = tsym.name.table.names;
// search by qualified name first
ClassDoc cd = env.lookupClass(className);
if (cd != null) {
return cd;
}
// search inner classes
//### Add private entry point to avoid creating array?
//### Replicate code in innerClasses here to avoid consing?
ClassDoc[] innerClasses = innerClasses();
for (int i = 0; i < innerClasses.length; i++) {
if (innerClasses[i].name().equals(className) || //### 'name' of the inner class, rather than the true simple name.
innerClasses[i].name().endsWith(className)) {
return innerClasses[i];
} else {
ClassDoc innercd = ((ClassDocImpl) innerClasses[i]).searchClass(className);
if (innercd != null) {
return innercd;
}
}
}
// check in this package
cd = containingPackage().findClass(className);
if (cd != null) {
return cd;
}
// make sure that this symbol has been completed
if (tsym.completer != null) {
tsym.complete();
}
if (tsym.sourcefile != null) {
//### This information is available only for source classes.
Env<AttrContext> compenv = env.enter.getEnv(tsym);
if (compenv == null)
return null;
Scope s = compenv.toplevel.namedImportScope;
for (Scope.Entry e = s.lookup(names.fromString(className)); e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.TYP) {
ClassDoc c = env.getClassDoc((ClassSymbol) e.sym);
return c;
}
}
s = compenv.toplevel.starImportScope;
for (Scope.Entry e = s.lookup(names.fromString(className)); e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.TYP) {
ClassDoc c = env.getClassDoc((ClassSymbol) e.sym);
return c;
}
}
}
// not found
return null;
}
use of com.sun.tools.javac.code.Scope in project ceylon-compiler by ceylon.
the class HashCollisionTest method run.
void run() throws Exception {
// set up basic environment for test
Context context = new Context();
// required by ClassReader which is required by Symtab
JavacFileManager.preRegister(context);
// Name.Table impls tied to an instance of Names
names = Names.instance(context);
symtab = Symtab.instance(context);
// determine hashMask for an empty scope
// any owner will do
Scope emptyScope = new Scope(symtab.unnamedPackage);
Field sHashMask = Scope.class.getDeclaredField("hashMask");
sHashMask.setAccessible(true);
scopeHashMask = sHashMask.getInt(emptyScope);
log("scopeHashMask: " + scopeHashMask);
// 1. determine the Name.hashCode of "Entry", and therefore the index of
// Entry in an empty scope. i.e. name.hashCode() & Scope.hashMask
Name entry = names.fromString("Entry");
// 2. create names of the form *$Entry until we find a name with a
// hashcode which yields the same index as Entry in an empty scope.
// Since Name.hashCode is a function of position (and not content) it
// should work to create successively longer names until one with the
// desired characteristics is found.
Name outerName;
Name innerName;
StringBuilder sb = new StringBuilder("C");
int i = 0;
do {
sb.append(Integer.toString(i % 10));
innerName = names.fromString(sb + "$Entry");
} while (!clash(entry, innerName) && (++i) < MAX_TRIES);
if (clash(entry, innerName)) {
log("Detected expected hash collision for " + entry + " and " + innerName + " after " + i + " tries");
} else {
throw new Exception("No potential collision found after " + i + " tries");
}
outerName = names.fromString(sb.toString());
/*
* Now we can set up the scenario.
*/
// 3. Create a nested class named Entry
ClassSymbol cc = createClass(names.fromString("C"), symtab.unnamedPackage);
ClassSymbol ce = createClass(entry, cc);
// 4. Create a package containing a nested class using the name from 2
PackageSymbol p = new PackageSymbol(names.fromString("p"), symtab.rootPackage);
p.members_field = new Scope(p);
ClassSymbol inner = createClass(innerName, p);
// we'll need this later when we "rename" cn
ClassSymbol outer = createClass(outerName, p);
// 5. Create a star-import scope
log("createStarImportScope");
// if StarImportScope exists, use it, otherwise, for testing legacy code,
// fall back on ImportScope
Scope starImportScope;
Method importAll;
PackageSymbol pkg = new PackageSymbol(names.fromString("pkg"), symtab.rootPackage);
try {
Class<?> c = Class.forName("com.sun.tools.javac.code.Scope$StarImportScope");
Constructor ctor = c.getDeclaredConstructor(new Class[] { Symbol.class });
importAll = c.getDeclaredMethod("importAll", new Class[] { Scope.class });
starImportScope = (Scope) ctor.newInstance(new Object[] { pkg });
} catch (ClassNotFoundException e) {
starImportScope = new ImportScope(pkg);
importAll = null;
}
dump("initial", starImportScope);
// 6. Insert the contents of the package from 4.
Scope p_members = p.members();
if (importAll != null) {
importAll.invoke(starImportScope, p_members);
} else {
Scope fromScope = p_members;
Scope toScope = starImportScope;
// before the use of StarImportScope.importAll.
for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
if (e.sym.kind == TYP && !toScope.includes(e.sym))
toScope.enter(e.sym, fromScope);
}
}
dump("imported p", starImportScope);
// 7. Insert the class from 3.
starImportScope.enter(ce, cc.members_field);
dump("imported ce", starImportScope);
/*
* Set the trap.
*/
// 8. Rename the nested class to Entry. so that there is a bogus entry in the star-import scope
p.members_field.remove(inner);
inner.name = entry;
inner.owner = outer;
outer.members_field.enter(inner);
// 9. Lookup Entry
Scope.Entry e = starImportScope.lookup(entry);
dump("final", starImportScope);
if (e.sym == null)
throw new Exception("symbol not found: " + entry);
}
Aggregations