public static int addImportEdits(
     Tree.Declaration node,
     TextChange fc,
     IDocument doc,
     final Tree.CompilationUnit ncu,
     final Set<String> packages,
     Declaration declaration) {
   Package p = ncu.getUnit().getPackage();
   Map<Declaration, String> imports = getImports(node, p.getNameAsString(), ncu, packages);
   return applyImports(fc, imports, ncu, doc, declaration);
 }
Exemple #2
0
  private void loadModuleFromSource(
      Package pkg,
      Modules modules,
      LinkedList<JCCompilationUnit> moduleTrees,
      List<JCCompilationUnit> parsedTrees) {
    // skip it if we already resolved the package
    if (pkg.getModule() != null) {
      // make sure the default module is always added to the classpath, it will be the only one to
      // have a module
      if (!addedDefaultModuleToClassPath && pkg.getModule().isDefaultModule()) {
        addedDefaultModuleToClassPath = true;
        ceylonEnter.addOutputModuleToClassPath(pkg.getModule());
      }
      return;
    }
    String pkgName = pkg.getQualifiedNameString();
    Module module = null;
    // do we have a module for this package?
    // FIXME: is this true? what if we have a module.ceylon at toplevel?
    if (pkgName.isEmpty()) module = modules.getDefaultModule();
    else {
      for (Module m : modulesLoadedFromSource) {
        if (JvmBackendUtil.isSubPackage(m.getNameAsString(), pkgName)) {
          module = m;
          break;
        }
      }
      if (module == null) {
        module = loadModuleFromSource(pkgName, moduleTrees, parsedTrees);
      } else if (!module.isAvailable()) {
        loadModuleFromSource(pkgName, moduleTrees, parsedTrees);
      }

      if (module == null) {
        // no declaration for it, must be the default module, unless we're bootstrapping the
        // language module,
        // because we have some com.redhat.ceylon packages that must go in the language module
        if (isBootstrap) module = modules.getLanguageModule();
        else module = modules.getDefaultModule();
      }
    }
    // bind module and package together
    pkg.setModule(module);
    if (!module.getPackages().contains(pkg)) {
      module.getPackages().add(pkg);
    }
    // automatically add this module's jar to the classpath if it exists
    ceylonEnter.addOutputModuleToClassPath(module);
  }
  public void generate() throws IOException {
    writeHeader("Overview");
    writeNavBar();

    open("div class='container-fluid'");
    writeDescription();
    writePackagesTable("Packages", tool.getPackages(module));
    writeDependencies();
    close("div");

    for (Package pkg : module.getPackages()) {
      if (tool.isRootPackage(module, pkg) && !pkg.getMembers().isEmpty()) {
        rootPackageDoc = new PackageDoc(tool, writer, pkg);
        rootPackageDoc.generate();
      }
    }

    writeFooter();
  }
Exemple #4
0
 private void importAllMembers(
     Package importedPackage, Set<String> ignoredMembers, ImportList il) {
   for (Declaration dec : importedPackage.getMembers()) {
     if (dec.isShared()
         && isResolvable(dec)
         && !ignoredMembers.contains(dec.getName())
         && !isNonimportable(importedPackage, dec.getName())) {
       addWildcardImport(il, dec);
     }
   }
 }
Exemple #5
0
 private JavaFileObject genCodeUnlessError(Env<AttrContext> env, JCClassDecl cdef)
     throws IOException {
   CeylonFileObject sourcefile = (CeylonFileObject) env.toplevel.sourcefile;
   try {
     // do not look at the global number of errors but only those for this file
     if (super.gen.genClass(env, cdef)) {
       String packageName = cdef.sym.packge().getQualifiedName().toString();
       Package pkg = modelLoader.findPackage(packageName);
       if (pkg == null) throw new RuntimeException("Failed to find package: " + packageName);
       Module module = pkg.getModule();
       if (!module.isDefaultModule()) {
         String moduleName = module.getNameAsString();
         CeylonFileObject moduleFileObject = moduleNamesToFileObjects.get(moduleName);
         // if there's no module source file object it means the module descriptor had parse errors
         if (moduleFileObject == null || moduleFileObject.hasError()) {
           // we do not produce any class files for modules with errors
           if (options.get(Option.VERBOSE) != null) {
             log.printRawLines(
                 WriterKind.NOTICE,
                 "[Not writing class "
                     + cdef.sym.className()
                     + " because its module has errors: "
                     + moduleName
                     + "]");
           }
           return null;
         }
       }
       return writer.writeClass(cdef.sym);
     }
   } catch (ClassWriter.PoolOverflow ex) {
     log.error(cdef.pos(), "limit.pool");
   } catch (ClassWriter.StringOverflow ex) {
     log.error(cdef.pos(), "limit.string.overflow", ex.value.substring(0, 20));
   } catch (CompletionFailure ex) {
     chk.completionError(cdef.pos(), ex);
   } catch (AssertionError e) {
     throw new RuntimeException("Error generating bytecode for " + sourcefile.getName(), e);
   }
   return null;
 }
  public static Object getPackageOrDeclaration(IProject project, String qualifiedName) {
    Object result = null;

    String pkgName = null;
    int pkgSepIndex = qualifiedName.indexOf("::");
    if (pkgSepIndex == -1) {
      pkgName = qualifiedName;
    } else {
      pkgName = qualifiedName.substring(0, pkgSepIndex);
    }

    Package pkg = getPackage(project, pkgName);
    if (pkg != null && pkgSepIndex != -1) {
      Declaration d;
      int memberSepIndex = qualifiedName.indexOf(".", pkgSepIndex);
      if (memberSepIndex != -1) {
        String baseName = qualifiedName.substring(pkgSepIndex + 2, memberSepIndex);
        String methodName = qualifiedName.substring(memberSepIndex + 1);
        d = pkg.getMember(baseName, null, false);
        d = extractAnonymousClassIfRequired(d);
        if (d != null) {
          Declaration m = d.getMember(methodName, null, false);
          if (m instanceof Function && d instanceof Class) {
            result = new MethodWithContainer((Class) d, (Function) m);
          }
        }
      } else {
        String baseName = qualifiedName.substring(pkgSepIndex + 2);
        d = pkg.getMember(baseName, null, false);
        result = extractAnonymousClassIfRequired(d);
      }
    } else {
      result = pkg;
    }

    return result;
  }
Exemple #7
0
 private String importMember(
     Tree.ImportMemberOrType member, Package importedPackage, ImportList il) {
   Tree.Identifier id = member.getIdentifier();
   if (id == null) {
     return null;
   }
   Import i = new Import();
   member.setImportModel(i);
   Tree.Alias alias = member.getAlias();
   String name = name(id);
   if (alias == null) {
     i.setAlias(name);
   } else {
     i.setAlias(name(alias.getIdentifier()));
   }
   if (isNonimportable(importedPackage, name)) {
     id.addError("root type may not be imported");
     return name;
   }
   Declaration d = importedPackage.getMember(name, null, false);
   if (d == null) {
     String correction = correct(importedPackage, unit, name);
     String message = correction == null ? "" : " (did you mean '" + correction + "'?)";
     id.addError("imported declaration not found: '" + name + "'" + message, 100);
     unit.getUnresolvedReferences().add(id);
   } else {
     if (!declaredInPackage(d, unit)) {
       if (!d.isShared()) {
         id.addError("imported declaration is not shared: '" + name + "'", 400);
       } else if (d.isPackageVisibility()) {
         id.addError("imported package private declaration is not visible: '" + name + "'");
       } else if (d.isProtectedVisibility()) {
         id.addError("imported protected declaration is not visible: '" + name + "'");
       }
     }
     i.setDeclaration(d);
     member.setDeclarationModel(d);
     if (il.hasImport(d)) {
       id.addError("already imported: '" + name + "'");
     } else if (!checkForHiddenToplevel(id, i, alias)) {
       addImport(member, il, i);
     }
     checkAliasCase(alias, d);
   }
   if (d != null) {
     importMembers(member, d);
   }
   return name;
 }
Exemple #8
0
 private boolean isNonimportable(Package pkg, String name) {
   String pname = pkg.getQualifiedNameString();
   return pname.equals("java.lang")
           && ("Object".equals(name) || "Throwable".equals(name) || "Exception".equals(name))
       || pname.equals("java.lang.annotation") && "Annotation".equals(name);
 }
Exemple #9
0
 private Module loadModuleFromSource(
     String pkgName,
     LinkedList<JCCompilationUnit> moduleTrees,
     List<JCCompilationUnit> parsedTrees) {
   if (pkgName.isEmpty()) return null;
   String moduleClassName = pkgName + ".module";
   JavaFileObject fileObject;
   try {
     if (options.get(Option.VERBOSE) != null) {
       log.printRawLines(
           WriterKind.NOTICE, "[Trying to load source for module " + moduleClassName + "]");
     }
     fileObject =
         fileManager.getJavaFileForInput(
             StandardLocation.SOURCE_PATH, moduleClassName, Kind.SOURCE);
     if (options.get(Option.VERBOSE) != null) {
       log.printRawLines(WriterKind.NOTICE, "[Got file object: " + fileObject + "]");
     }
   } catch (IOException e) {
     e.printStackTrace();
     return loadModuleFromSource(getParentPackage(pkgName), moduleTrees, parsedTrees);
   }
   if (fileObject != null) {
     // first make sure we're not already compiling it: this can happen if we have several versions
     // of the
     // same module already loaded: we will get one which isn't the one we compile, but that's not
     // the one
     // we really want to compile.
     for (JCCompilationUnit parsedTree : parsedTrees) {
       if (parsedTree.sourcefile.equals(fileObject)
           && parsedTree instanceof CeylonCompilationUnit) {
         // same file! we already parsed it, let's return this one's module
         PhasedUnit phasedUnit = ((CeylonCompilationUnit) parsedTree).phasedUnit;
         // the module visitor does load the module but does not set the unit's package module
         if (phasedUnit.getPackage().getModule() == null) {
           // so find the module it created
           for (Module mod : ceylonContext.getModules().getListOfModules()) {
             // we recognise it with the unit
             if (mod.getUnit() == phasedUnit.getUnit()) {
               // set the package's module
               Package pkg = phasedUnit.getPackage();
               pkg.setModule(mod);
               mod.getPackages().add(pkg);
               modulesLoadedFromSource.add(mod);
               break;
             }
           }
         }
         // now return it
         return phasedUnit.getPackage().getModule();
       }
     }
     JCCompilationUnit javaCompilationUnit = parse(fileObject);
     Module module;
     if (javaCompilationUnit instanceof CeylonCompilationUnit) {
       CeylonCompilationUnit ceylonCompilationUnit = (CeylonCompilationUnit) javaCompilationUnit;
       moduleTrees.add(ceylonCompilationUnit);
       // parse the module info from there
       module = ceylonCompilationUnit.phasedUnit.visitSrcModulePhase();
       ceylonCompilationUnit.phasedUnit.visitRemainingModulePhase();
       // now set the module
       if (module != null) {
         ceylonCompilationUnit.phasedUnit.getPackage().setModule(module);
       }
     } else {
       // there was a syntax error in the module descriptor, make a pretend module so that we can
       // correctly mark all declarations as part of that module, but we won't generate any code
       // for it
       ModuleManager moduleManager = phasedUnits.getModuleManager();
       module = moduleManager.getOrCreateModule(Arrays.asList(pkgName.split("\\.")), "bogus");
     }
     // now remember it
     if (module != null) {
       modulesLoadedFromSource.add(module);
       return module;
     }
   }
   return loadModuleFromSource(getParentPackage(pkgName), moduleTrees, parsedTrees);
 }