Skip to content

PackageElement .toString() #3

@regbo

Description

@regbo

I don't know if it's my maven setup, windows, or my JVM, but this block of code leads to errors:

 private String _getPackage(Element pElement)
    {
      Element element = pElement;
      while (element != null)
      {
        if (element.getKind() == ElementKind.PACKAGE)
          return element.toString();
        Element enclosingElement = element.getEnclosingElement();
        if (ENCLOSING_TYPES.contains(enclosingElement.getKind()))
          element = enclosingElement;
        else
        {
          processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Element type not supported", pElement);
          break;
        }
      }
      return "";
    }

In my setup, the element.toString returns "package com.x.y.z" when it sould be "com.x.y.z"

I remedied this with:

private String _getPackage(Element pElement)
    {
      Element element = pElement;
      while (element != null)
      {
        if (element.getKind() == ElementKind.PACKAGE)
          return ((javax.lang.model.element.PackageElement)element).getQualifiedName().toString();
        Element enclosingElement = element.getEnclosingElement();
        if (ENCLOSING_TYPES.contains(enclosingElement.getKind()))
          element = enclosingElement;
        else
        {
          processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Element type not supported", pElement);
          break;
        }
      }
      return "";
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions