Java Code Examples for javax.lang.model.element.PackageElement#isUnnamed()

The following examples show how to use javax.lang.model.element.PackageElement#isUnnamed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HeaderMap.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private String outputDirFromPackage(PackageElement pkg) {
  if (pkg == null || pkg.isUnnamed()) {
    return "";
  }
  String pkgName = ElementUtil.getName(pkg);
  OutputStyleOption style = outputStyle;
  if (isPlatformPackage(pkgName)) {
    // Use package directories for platform classes if they do not have an entry in the header
    // mapping.
    style = OutputStyleOption.PACKAGE;
  }
  switch (style) {
    case PACKAGE:
      return ElementUtil.getName(pkg).replace('.', File.separatorChar) + File.separatorChar;
    default:
      return "";
  }
}
 
Example 2
Source File: PackageIndexWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds list of packages in the index table. Generate link to each package.
 *
 * @param packages Packages to which link is to be generated
 * @param tbody the documentation tree to which the list will be added
 */
protected void addPackagesList(Collection<PackageElement> packages, Content tbody) {
    boolean altColor = true;
    for (PackageElement pkg : packages) {
        if (!pkg.isUnnamed()) {
            if (!(configuration.nodeprecated && utils.isDeprecated(pkg))) {
                Content packageLinkContent = getPackageLink(pkg, getPackageName(pkg));
                Content thPackage = HtmlTree.TH_ROW_SCOPE(HtmlStyle.colFirst, packageLinkContent);
                HtmlTree tdSummary = new HtmlTree(HtmlTag.TD);
                tdSummary.addStyle(HtmlStyle.colLast);
                addSummaryComment(pkg, tdSummary);
                HtmlTree tr = HtmlTree.TR(thPackage);
                tr.addContent(tdSummary);
                tr.addStyle(altColor ? HtmlStyle.altColor : HtmlStyle.rowColor);
                tbody.addContent(tr);
            }
        }
        altColor = !altColor;
    }
}
 
Example 3
Source File: ModulePackageIndexFrameWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns each package name as a separate link.
 *
 * @param pkg PackageElement
 * @param mdle the module being documented
 * @return content for the package link
 */
protected Content getPackage(PackageElement pkg, ModuleElement mdle) {
    Content packageLinkContent;
    Content pkgLabel;
    if (!pkg.isUnnamed()) {
        pkgLabel = getPackageLabel(utils.getPackageName(pkg));
        packageLinkContent = getHyperLink(pathString(pkg,
                 DocPaths.PACKAGE_FRAME), pkgLabel, "",
                "packageFrame");
    } else {
        pkgLabel = new StringContent("<unnamed package>");
        packageLinkContent = getHyperLink(DocPaths.PACKAGE_FRAME,
                pkgLabel, "", "packageFrame");
    }
    Content li = HtmlTree.LI(packageLinkContent);
    return li;
}
 
Example 4
Source File: PrintingProcessor.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public PrintingElementVisitor visitPackage(PackageElement e, Boolean p) {
    defaultAction(e, false);
    if (!e.isUnnamed())
        writer.println("package " + e.getQualifiedName() + ";");
    else
        writer.println("// Unnamed package");
    return this;
}
 
Example 5
Source File: SignatureGenerator.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public String createJniFunctionSignature(ExecutableElement method) {
  // Mangle function name as described in JNI specification.
  // http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html#wp615
  StringBuilder sb = new StringBuilder();
  sb.append("Java_");

  String methodName = ElementUtil.getName(method);
  TypeElement declaringClass = ElementUtil.getDeclaringClass(method);
  PackageElement pkg = ElementUtil.getPackage(declaringClass);
  if (pkg != null && !pkg.isUnnamed()) {
    String pkgName = pkg.getQualifiedName().toString();
    for (String part : pkgName.split("\\.")) {
      sb.append(part);
      sb.append('_');
    }
  }
  jniMangleClass(declaringClass, sb);
  sb.append('_');
  sb.append(jniMangle(methodName));

  // Check whether the method is overloaded.
  int nameCount = 0;
  for (ExecutableElement m : ElementUtil.getExecutables(declaringClass)) {
    if (methodName.equals(ElementUtil.getName(m)) && ElementUtil.isNative(m)) {
      nameCount++;
    }
  }
  if (nameCount >= 2) {
    // Overloaded native methods, append JNI-mangled parameter types.
    sb.append("__");
    for (VariableElement param : method.getParameters()) {
      String type = createTypeSignature(typeUtil.erasure(param.asType()));
      sb.append(jniMangle(type));
    }
  }
  return sb.toString();
}
 
Example 6
Source File: TypeHelper.java    From AnnotatedAdapter with Apache License 2.0 5 votes vote down vote up
public String getPackageName(TypeElement type) {
  PackageElement pkg = elements.getPackageOf(type);
  if (!pkg.isUnnamed()) {
    return pkg.getQualifiedName().toString();
  } else {
    return ""; // Default package
  }
}
 
Example 7
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parse the package name.  We only want to display package name up to
 * 2 levels.
 */
public String parsePackageName(PackageElement p) {
    String pkgname = p.isUnnamed() ? "" : getPackageName(p);
    int index = -1;
    for (int j = 0; j < MAX_CONSTANT_VALUE_INDEX_LENGTH; j++) {
        index = pkgname.indexOf(".", index + 1);
    }
    if (index != -1) {
        pkgname = pkgname.substring(0, index);
    }
    return pkgname;
}
 
Example 8
Source File: Group.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Group the packages according the grouping information provided on the
 * command line. Given a list of packages, search each package name in
 * regular expression map as well as package name map to get the
 * corresponding group name. Create another map with mapping of group name
 * to the package list, which will fall under the specified group. If any
 * package doesn't belong to any specified group on the command line, then
 * a new group named "Other Packages" will be created for it. If there are
 * no groups found, in other words if "-group" option is not at all used,
 * then all the packages will be grouped under group "Packages".
 *
 * @param packages Packages specified on the command line.
 * @return map of group names and set of package elements
 */
public Map<String, SortedSet<PackageElement>> groupPackages(Set<PackageElement> packages) {
    Map<String, SortedSet<PackageElement>> groupPackageMap = new HashMap<>();
    String defaultGroupName =
        (elementNameGroupMap.isEmpty() && regExpGroupMap.isEmpty())?
            configuration.getResources().getText("doclet.Packages") :
            configuration.getResources().getText("doclet.Other_Packages");
    // if the user has not used the default group name, add it
    if (!groupList.contains(defaultGroupName)) {
        groupList.add(defaultGroupName);
    }
    for (PackageElement pkg : packages) {
        String pkgName = pkg.isUnnamed() ? null : configuration.utils.getPackageName(pkg);
        String groupName = pkg.isUnnamed() ? null : elementNameGroupMap.get(pkgName);
        // if this package is not explicitly assigned to a group,
        // try matching it to group specified by regular expression
        if (groupName == null) {
            groupName = regExpGroupName(pkgName);
        }
        // if it is in neither group map, put it in the default
        // group
        if (groupName == null) {
            groupName = defaultGroupName;
        }
        getPkgList(groupPackageMap, groupName).add(pkg);
    }
    return groupPackageMap;
}
 
Example 9
Source File: PackageUseWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add the package use information.
 *
 * @param pkg the package that used the given package
 * @param contentTree the content tree to which the information will be added
 */
protected void addPackageUse(PackageElement pkg, Content contentTree) {
    Content thFirst = HtmlTree.TH_ROW_SCOPE(HtmlStyle.colFirst,
            getHyperLink(utils.getPackageName(pkg),
            new StringContent(utils.getPackageName(pkg))));
    contentTree.addContent(thFirst);
    HtmlTree tdLast = new HtmlTree(HtmlTag.TD);
    tdLast.addStyle(HtmlStyle.colLast);
    if (pkg != null && !pkg.isUnnamed()) {
        addSummaryComment(pkg, tdLast);
    } else {
        tdLast.addContent(Contents.SPACE);
    }
    contentTree.addContent(tdLast);
}
 
Example 10
Source File: TypeElementCatalog.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return all of the classes specified on the command-line that belong to the given package.
 *
 * @param packageName the name of the package specified on the command-line.
 */
public SortedSet<TypeElement> allUnnamedClasses() {
    for (PackageElement pkg : allClasses.keySet()) {
        if (pkg.isUnnamed()) {
            return allClasses.get(pkg);
        }
    }
    return new TreeSet<>(comparator);
}
 
Example 11
Source File: TreeWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add the links to all the package tree files.
 *
 * @param contentTree the content tree to which the links will be added
 */
protected void addPackageTreeLinks(Content contentTree) {
    //Do nothing if only unnamed package is used
    if (isUnnamedPackage()) {
        return;
    }
    if (!classesOnly) {
        Content span = HtmlTree.SPAN(HtmlStyle.packageHierarchyLabel,
                contents.packageHierarchies);
        contentTree.addContent(span);
        HtmlTree ul = new HtmlTree(HtmlTag.UL);
        ul.addStyle(HtmlStyle.horizontal);
        int i = 0;
        for (PackageElement pkg : packages) {
            // If the package name length is 0 or if -nodeprecated option
            // is set and the package is marked as deprecated, do not include
            // the page in the list of package hierarchies.
            if (pkg.isUnnamed() ||
                    (configuration.nodeprecated && utils.isDeprecated(pkg))) {
                i++;
                continue;
            }
            DocPath link = pathString(pkg, DocPaths.PACKAGE_TREE);
            Content li = HtmlTree.LI(getHyperLink(link,
                    new StringContent(utils.getPackageName(pkg))));
            if (i < packages.size() - 1) {
                li.addContent(", ");
            }
            ul.addContent(li);
            i++;
        }
        contentTree.addContent(ul);
    }
}
 
Example 12
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * package name, an unnamed package is returned as &lt;Unnamed&gt;
 * @param pkg
 * @return
 */
public String getPackageName(PackageElement pkg) {
    if (pkg == null || pkg.isUnnamed()) {
        return DocletConstants.DEFAULT_PACKAGE_NAME;
    }
    return pkg.getQualifiedName().toString();
}
 
Example 13
Source File: BindDataSourceBuilder.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Generate dataSource name.
 *
 * @param schema
 *            the schema
 * @return associated class name
 */
public static ClassName generateDataSourceName(SQLiteDatabaseSchema schema) {
	String dataSourceName = schema.getName();
	dataSourceName = PREFIX + dataSourceName;

	PackageElement pkg = BaseProcessor.elementUtils.getPackageOf(schema.getElement());
	String packageName = pkg.isUnnamed() ? "" : pkg.getQualifiedName().toString();

	return ClassName.get(packageName, dataSourceName);
}
 
Example 14
Source File: DocPath.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the inverse path for a package.
 * For example, if the package is java.lang,
 * the inverse path is ../...
 */
public static DocPath forRoot(PackageElement pkgElement) {
    String name = (pkgElement == null || pkgElement.isUnnamed())
            ? ""
            : pkgElement.getQualifiedName().toString();
    return new DocPath(name.replace('.', '/').replaceAll("[^/]+", ".."));
}
 
Example 15
Source File: HtmlDocletWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public Content getPackageLink(PackageElement packageElement) {
    StringContent content =  packageElement.isUnnamed()
            ? new StringContent()
            : new StringContent(utils.getPackageName(packageElement));
    return getPackageLink(packageElement, content);
}
 
Example 16
Source File: ExternalDomainMetaFactory.java    From doma with Apache License 2.0 4 votes vote down vote up
private void doDomainType(
    TypeElement converterElement, TypeMirror domainType, ExternalDomainMeta meta) {
  meta.setType(domainType);

  ArrayType arrayType = ctx.getMoreTypes().toArrayType(domainType);
  if (arrayType != null) {
    TypeMirror componentType = arrayType.getComponentType();
    if (componentType.getKind() == TypeKind.ARRAY) {
      throw new AptException(Message.DOMA4447, converterElement, new Object[] {});
    }
    TypeElement componentElement = ctx.getMoreTypes().toTypeElement(componentType);
    if (componentElement == null) {
      throw new AptIllegalStateException(componentType.toString());
    }
    if (!componentElement.getTypeParameters().isEmpty()) {
      throw new AptException(Message.DOMA4448, converterElement, new Object[] {});
    }
    return;
  }

  TypeElement domainElement = ctx.getMoreTypes().toTypeElement(domainType);
  if (domainElement == null) {
    throw new AptIllegalStateException(domainType.toString());
  }
  if (domainElement.getNestingKind().isNested()) {
    validateEnclosingElement(domainElement);
  }
  PackageElement pkgElement = ctx.getMoreElements().getPackageOf(domainElement);
  if (pkgElement.isUnnamed()) {
    throw new AptException(
        Message.DOMA4197, converterElement, new Object[] {domainElement.getQualifiedName()});
  }
  DeclaredType declaredType = ctx.getMoreTypes().toDeclaredType(domainType);
  if (declaredType == null) {
    throw new AptIllegalStateException(domainType.toString());
  }
  for (TypeMirror typeArg : declaredType.getTypeArguments()) {
    if (typeArg.getKind() != TypeKind.WILDCARD) {
      throw new AptException(
          Message.DOMA4203, converterElement, new Object[] {domainElement.getQualifiedName()});
    }
  }
  meta.setTypeElement(domainElement);
  TypeParametersDef typeParametersDef = ctx.getMoreElements().getTypeParametersDef(domainElement);
  meta.setTypeParametersDef(typeParametersDef);
}
 
Example 17
Source File: ClassWriterImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Content getHeader(String header) {
    HtmlTree bodyTree = getBody(true, getWindowTitle(utils.getSimpleName(typeElement)));
    HtmlTree htmlTree = (configuration.allowTag(HtmlTag.HEADER))
            ? HtmlTree.HEADER()
            : bodyTree;
    addTop(htmlTree);
    addNavLinks(true, htmlTree);
    if (configuration.allowTag(HtmlTag.HEADER)) {
        bodyTree.addContent(htmlTree);
    }
    bodyTree.addContent(HtmlConstants.START_OF_CLASS_DATA);
    HtmlTree div = new HtmlTree(HtmlTag.DIV);
    div.addStyle(HtmlStyle.header);
    if (configuration.showModules) {
        ModuleElement mdle = configuration.docEnv.getElementUtils().getModuleOf(typeElement);
        Content classModuleLabel = HtmlTree.SPAN(HtmlStyle.moduleLabelInType, contents.moduleLabel);
        Content moduleNameDiv = HtmlTree.DIV(HtmlStyle.subTitle, classModuleLabel);
        moduleNameDiv.addContent(Contents.SPACE);
        moduleNameDiv.addContent(getModuleLink(mdle,
                new StringContent(mdle.getQualifiedName().toString())));
        div.addContent(moduleNameDiv);
    }
    PackageElement pkg = utils.containingPackage(typeElement);
    if (!pkg.isUnnamed()) {
        Content classPackageLabel = HtmlTree.SPAN(HtmlStyle.packageLabelInType, contents.packageLabel);
        Content pkgNameDiv = HtmlTree.DIV(HtmlStyle.subTitle, classPackageLabel);
        pkgNameDiv.addContent(Contents.SPACE);
        Content pkgNameContent = getPackageLink(pkg,
                new StringContent(utils.getPackageName(pkg)));
        pkgNameDiv.addContent(pkgNameContent);
        div.addContent(pkgNameDiv);
    }
    LinkInfoImpl linkInfo = new LinkInfoImpl(configuration,
            LinkInfoImpl.Kind.CLASS_HEADER, typeElement);
    //Let's not link to ourselves in the header.
    linkInfo.linkToSelf = false;
    Content headerContent = new StringContent(header);
    Content heading = HtmlTree.HEADING(HtmlConstants.CLASS_PAGE_HEADING, true,
            HtmlStyle.title, headerContent);
    heading.addContent(getTypeParameterLinks(linkInfo));
    div.addContent(heading);
    if (configuration.allowTag(HtmlTag.MAIN)) {
        mainTree.addContent(div);
    } else {
        bodyTree.addContent(div);
    }
    return bodyTree;
}
 
Example 18
Source File: PreferenceKeyField.java    From PreferenceRoom with Apache License 2.0 4 votes vote down vote up
public PreferenceKeyField(
    @NonNull VariableElement variableElement, @NonNull Elements elementUtils)
    throws IllegalAccessException {
  KeyName annotation_keyName = variableElement.getAnnotation(KeyName.class);
  this.variableElement = variableElement;
  PackageElement packageElement = elementUtils.getPackageOf(variableElement);
  this.packageName =
      packageElement.isUnnamed() ? null : packageElement.getQualifiedName().toString();
  this.typeName = TypeName.get(variableElement.asType());
  this.clazzName = variableElement.getSimpleName().toString();
  this.value = variableElement.getConstantValue();
  setTypeStringName();

  if (annotation_keyName != null)
    this.keyName =
        Strings.isNullOrEmpty(annotation_keyName.value())
            ? StringUtils.toUpperCamel(this.clazzName)
            : annotation_keyName.value();
  else this.keyName = StringUtils.toUpperCamel(this.clazzName);

  if (this.isObjectField) {
    variableElement.getAnnotationMirrors().stream()
        .filter(
            annotationMirror ->
                TypeName.get(annotationMirror.getAnnotationType())
                    .equals(TypeName.get(TypeConverter.class)))
        .forEach(
            annotationMirror ->
                annotationMirror
                    .getElementValues()
                    .forEach(
                        (type, value) -> {
                          String[] split = value.getValue().toString().split("\\.");
                          StringBuilder builder = new StringBuilder();
                          for (int i = 0; i < split.length - 1; i++)
                            builder.append(split[i] + ".");
                          this.converterPackage =
                              builder.toString().substring(0, builder.toString().length() - 1);
                          this.converter = split[split.length - 1];
                        }));
  }

  if (variableElement.getModifiers().contains(Modifier.PRIVATE)) {
    throw new IllegalAccessException(
        String.format("Field \'%s\' should not be private.", variableElement.getSimpleName()));
  } else if (!this.isObjectField && !variableElement.getModifiers().contains(Modifier.FINAL)) {
    throw new IllegalAccessException(
        String.format("Field \'%s\' should be final.", variableElement.getSimpleName()));
  }
}
 
Example 19
Source File: HtmlDocWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Get the enclosed name of the package
 *
 * @param te  TypeElement
 * @return the name
 */
public String getEnclosingPackageName(TypeElement te) {

    PackageElement encl = configuration.utils.containingPackage(te);
    return (encl.isUnnamed()) ? "" : (encl.getQualifiedName() + ".");
}
 
Example 20
Source File: ObjectMappableProcessor.java    From sqlbrite-dao with Apache License 2.0 2 votes vote down vote up
/**
 * Get the package name of a certain clazz
 *
 * @param clazz The class you want the packagename for
 * @return The package name
 */
private String getPackageName(ObjectMappableAnnotatedClass clazz) {
  PackageElement pkg = elements.getPackageOf(clazz.getElement());
  return pkg.isUnnamed() ? "" : pkg.getQualifiedName().toString();
}