Java Code Examples for javax.lang.model.element.Name#toString()

The following examples show how to use javax.lang.model.element.Name#toString() . 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: PluginProcessor.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private String calculatePackage(Elements elements, Element element, String packageName) {
    Name name = elements.getPackageOf(element).getQualifiedName();
    if (name == null) {
        return null;
    }
    String pkgName = name.toString();
    if (packageName == null) {
        return pkgName;
    }
    if (pkgName.length() == packageName.length()) {
        return packageName;
    }
    if (pkgName.length() < packageName.length() && packageName.startsWith(pkgName)) {
        return pkgName;
    }

    return commonPrefix(pkgName, packageName);
}
 
Example 2
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static String target2String(TypeElement target) {
    final Name qualifiedName = target.getQualifiedName(); //#130759
    if (qualifiedName == null) {
        Logger.getLogger(Utilities.class.getName()).warning("Target qualified name could not be resolved."); //NOI18N
        return ""; //NOI18N
    } else {
        String qnString = qualifiedName.toString();
        if (qnString.length() == 0) {
            //probably an anonymous class
            qnString = target.asType().toString();
        }

        try {
            qnString = XMLUtil.toElementContent(qnString);
        } catch (CharConversionException ex) {
            Logger.getLogger(Utilities.class.getName()).log(Level.FINE, null, ex);
        }

        return qnString;
    }
}
 
Example 3
Source File: JavaSourceParserUtil.java    From jeddict with Apache License 2.0 5 votes vote down vote up
public static String getAnnotationQualifiedName(AnnotationMirror annotationMirror) {
//     Iterator<Entry<? extends ExecutableElement, ? extends AnnotationValue>> elementValuesItr =  annotationMirror.getElementValues().entrySet().iterator();
        DeclaredType annotationDeclaredType = annotationMirror.getAnnotationType();
        TypeElement annotationTypeElement = (TypeElement) annotationDeclaredType.asElement();
        Name name = annotationTypeElement.getQualifiedName();
        return name.toString();
    }
 
Example 4
Source File: UnusedImports.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void addUnresolvableImport(Name name, ImportTree imp) {
    String key = name.toString();

    Collection<ImportTree> l = simpleName2UnresolvableImports.get(key);

    if (l == null) {
        simpleName2UnresolvableImports.put(key, l = new LinkedList<ImportTree>());
    }

    l.add(imp);
}
 
Example 5
Source File: ReferenceTransformer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void handleIdentifier(Tree node, Name id) {
    TreeMaker mk = copy.getTreeMaker();
    String nn = id.toString();
    if (nn.equals(name)) {
        Element res = copy.getTrees().getElement(getCurrentPath());
        if (res != null && res == shadowed) {
            if (res.getModifiers().contains(Modifier.STATIC)) {
                copy.rewrite(node, 
                        mk.MemberSelect(
                            mk.Identifier(shadowedGate), // NOI18N
                            res.getSimpleName().toString()
                        )
                );
            } else if (shadowedGate == target) {
                copy.rewrite(node, 
                        mk.MemberSelect(
                            mk.MemberSelect(mk.Identifier(target), "super"), // NOI18N
                            res.getSimpleName().toString()
                        )
                );
            } else {
                copy.rewrite(node, 
                        mk.MemberSelect(
                            mk.MemberSelect(mk.Identifier(shadowedGate), "this"), // NOI18N
                            res.getSimpleName().toString()
                        )
                );
            }
        }
    }
}
 
Example 6
Source File: BindingsPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void setScope( WebBeansModel model, Element element )
        throws CdiException
{
    if (getResult() instanceof DependencyInjectionResult.ResolutionResult) {
        String scope = model.getScope(element);
        if (scope == null) {
            return;
        }
        String text = "";
        if (showFqns()) {
            text = "@" + scope; // NOI8N
        }
        else {
            TypeMirror scopeType = model.resolveType(scope);
            if (scopeType != null) {
                Element scopeElement = model.getCompilationController()
                        .getTypes().asElement(scopeType);
                if (scopeElement instanceof TypeElement) {
                    Name name = ((TypeElement) scopeElement)
                            .getSimpleName();
                    text = "@" + name.toString();
                }
            }
        }
        getScopeComponent().setText(text);
    }
}
 
Example 7
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 4 votes vote down vote up
private static String getIdentifierOrNull(Name label) {
  return label == null ? null : label.toString();
}
 
Example 8
Source File: BasicAnnotationProcessor.java    From auto with Apache License 2.0 4 votes vote down vote up
private ElementName(Kind kind, Name name) {
  this.kind = checkNotNull(kind);
  this.name = name.toString();
}
 
Example 9
Source File: ElementUtil.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public static String getName(Element element) {
  // Always return qualified package names.
  Name name = element.getKind() == ElementKind.PACKAGE
      ? ((PackageElement) element).getQualifiedName() : element.getSimpleName();
  return name.toString();
}
 
Example 10
Source File: WebServiceAp.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String getOperationName(Name messageName) {
    return messageName != null ? messageName.toString() : null;
}
 
Example 11
Source File: TreeAnalyzer.java    From meghanada-server with GNU General Public License v3.0 4 votes vote down vote up
private static void analyzeTopLevelClass(SourceContext context, JCTree.JCClassDecl classDecl)
    throws IOException {
  Source src = context.source;
  EndPosTable endPosTable = context.endPosTable;

  Tree.Kind classDeclKind = classDecl.getKind();

  boolean isInterface = classDeclKind.equals(Tree.Kind.INTERFACE);
  boolean isEnum = classDeclKind.equals(Tree.Kind.ENUM);

  int startPos = classDecl.getPreferredPosition();
  int endPos = classDecl.getEndPosition(endPosTable);
  JCTree.JCModifiers modifiers = classDecl.getModifiers();
  List<JCTree.JCAnnotation> annotations = modifiers.getAnnotations();

  final String classModifiers = parseModifiers(context, modifiers);

  analyzeParsedTree(context, classDecl.getExtendsClause());

  analyzeSimpleExpressions(context, classDecl.getImplementsClause());

  Name simpleName = classDecl.getSimpleName();
  Range range = Range.create(src, startPos + 1, endPos);

  int nameStart = startPos + 6;
  if (isInterface) {
    nameStart = startPos + 10;
  } else if (isEnum) {
    nameStart = startPos + 5;
  }
  Range nameRange = Range.create(src, nameStart, nameStart + simpleName.length());

  String fqcn;
  if (src.getPackageName().isEmpty()) {
    fqcn = simpleName.toString();
  } else {
    fqcn = src.getPackageName() + '.' + simpleName.toString();
  }
  ClassScope classScope = new ClassScope(fqcn, nameRange, startPos, range);
  classScope.isEnum = isEnum;
  classScope.isInterface = isInterface;
  log.trace("class={}", classScope);
  analyzeAnnotations(context, annotations, classScope);

  src.startClass(classScope);

  for (JCTree tree : classDecl.getMembers()) {
    analyzeParsedTree(context, tree);
  }
  addClassNameIndex(
      src, classScope.range.begin.line, classScope.range.begin.column, classScope.getFQCN());
  Optional<ClassScope> endClass = src.endClass();
  log.trace("class={}", endClass);
}
 
Example 12
Source File: WebServiceAp.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String getOperationName(Name messageName) {
    return messageName != null ? messageName.toString() : null;
}
 
Example 13
Source File: WebServiceAp.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String getOperationName(Name messageName) {
    return messageName != null ? messageName.toString() : null;
}
 
Example 14
Source File: WebServiceAp.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String getOperationName(Name messageName) {
    return messageName != null ? messageName.toString() : null;
}
 
Example 15
Source File: ButterKnifeProcessor.java    From butterknife with Apache License 2.0 4 votes vote down vote up
private void parseBindView(Element element, Map<TypeElement, BindingSet.Builder> builderMap,
    Set<TypeElement> erasedTargetNames) {
  TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();

  // Start by verifying common generated code restrictions.
  boolean hasError = isInaccessibleViaGeneratedCode(BindView.class, "fields", element)
      || isBindingInWrongPackage(BindView.class, element);

  // Verify that the target type extends from View.
  TypeMirror elementType = element.asType();
  if (elementType.getKind() == TypeKind.TYPEVAR) {
    TypeVariable typeVariable = (TypeVariable) elementType;
    elementType = typeVariable.getUpperBound();
  }
  Name qualifiedName = enclosingElement.getQualifiedName();
  Name simpleName = element.getSimpleName();
  if (!isSubtypeOfType(elementType, VIEW_TYPE) && !isInterface(elementType)) {
    if (elementType.getKind() == TypeKind.ERROR) {
      note(element, "@%s field with unresolved type (%s) "
              + "must elsewhere be generated as a View or interface. (%s.%s)",
          BindView.class.getSimpleName(), elementType, qualifiedName, simpleName);
    } else {
      error(element, "@%s fields must extend from View or be an interface. (%s.%s)",
          BindView.class.getSimpleName(), qualifiedName, simpleName);
      hasError = true;
    }
  }

  if (hasError) {
    return;
  }

  // Assemble information on the field.
  int id = element.getAnnotation(BindView.class).value();
  BindingSet.Builder builder = builderMap.get(enclosingElement);
  Id resourceId = elementToId(element, BindView.class, id);
  if (builder != null) {
    String existingBindingName = builder.findExistingBindingName(resourceId);
    if (existingBindingName != null) {
      error(element, "Attempt to use @%s for an already bound ID %d on '%s'. (%s.%s)",
          BindView.class.getSimpleName(), id, existingBindingName,
          enclosingElement.getQualifiedName(), element.getSimpleName());
      return;
    }
  } else {
    builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
  }

  String name = simpleName.toString();
  TypeName type = TypeName.get(elementType);
  boolean required = isFieldRequired(element);

  builder.addField(resourceId, new FieldViewBinding(name, type, required));

  // Add the type-erased version to the valid binding targets set.
  erasedTargetNames.add(enclosingElement);
}
 
Example 16
Source File: WebServiceAp.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String getOperationName(Name messageName) {
    return messageName != null ? messageName.toString() : null;
}
 
Example 17
Source File: JpaControllerUtil.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static String getAnnotationQualifiedName(AnnotationMirror annotationMirror) {
    DeclaredType annotationDeclaredType = annotationMirror.getAnnotationType();
    TypeElement annotationTypeElement = (TypeElement) annotationDeclaredType.asElement();
    Name name = annotationTypeElement.getQualifiedName();
    return name.toString();
}
 
Example 18
Source File: WebServiceAp.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String getOperationName(Name messageName) {
    return messageName != null ? messageName.toString() : null;
}
 
Example 19
Source File: PluginAnnotationProcessor.java    From Velocity with MIT License 4 votes vote down vote up
@Override
public synchronized boolean process(Set<? extends TypeElement> annotations,
    RoundEnvironment roundEnv) {
  if (roundEnv.processingOver()) {
    return false;
  }

  for (Element element : roundEnv.getElementsAnnotatedWith(Plugin.class)) {
    if (element.getKind() != ElementKind.CLASS) {
      environment.getMessager()
          .printMessage(Diagnostic.Kind.ERROR, "Only classes can be annotated with "
              + Plugin.class.getCanonicalName());
      return false;
    }

    Name qualifiedName = ((TypeElement) element).getQualifiedName();

    if (Objects.equals(pluginClassFound, qualifiedName.toString())) {
      if (!warnedAboutMultiplePlugins) {
        environment.getMessager()
            .printMessage(Diagnostic.Kind.WARNING, "Velocity does not yet currently support "
                + "multiple plugins. We are using " + pluginClassFound
                + " for your plugin's main class.");
        warnedAboutMultiplePlugins = true;
      }
      return false;
    }

    Plugin plugin = element.getAnnotation(Plugin.class);
    if (!SerializedPluginDescription.ID_PATTERN.matcher(plugin.id()).matches()) {
      environment.getMessager().printMessage(Diagnostic.Kind.ERROR, "Invalid ID for plugin "
          + qualifiedName
          + ". IDs must start alphabetically, have alphanumeric characters, and can "
          + "contain dashes or underscores.");
      return false;
    }

    // All good, generate the velocity-plugin.json.
    SerializedPluginDescription description = SerializedPluginDescription
        .from(plugin, qualifiedName.toString());
    try {
      FileObject object = environment.getFiler()
          .createResource(StandardLocation.CLASS_OUTPUT, "", "velocity-plugin.json");
      try (Writer writer = new BufferedWriter(object.openWriter())) {
        new Gson().toJson(description, writer);
      }
      pluginClassFound = qualifiedName.toString();
    } catch (IOException e) {
      environment.getMessager()
          .printMessage(Diagnostic.Kind.ERROR, "Unable to generate plugin file");
    }
  }

  return false;
}
 
Example 20
Source File: WebServiceAp.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String getOperationName(Name messageName) {
    return messageName != null ? messageName.toString() : null;
}