Java Code Examples for javax.lang.model.element.ElementKind#isClass()

The following examples show how to use javax.lang.model.element.ElementKind#isClass() . 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: JavacTrees.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 2
Source File: JavacTrees.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 3
Source File: JavacTrees.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 4
Source File: Constitution.java    From immutables with Apache License 2.0 6 votes vote down vote up
@Nullable
private TypeElement findBaseClassElement() {
  Protoclass protoclass = protoclass();
  if (!isApplicableTo(protoclass)) {
    return null;
  }
  for (Element t : protoclass.sourceElement().getEnclosedElements()) {
    ElementKind kind = t.getKind();
    if (kind.isClass() || kind.isInterface()) {
      String simpleName = t.getSimpleName().toString();
      Naming typeInnerClassNaming = naming;

      if (!typeInnerClassNaming.detect(simpleName).isEmpty()) {
        return (TypeElement) t;
      }
    }
  }
  return null;
}
 
Example 5
Source File: JavacTrees.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 6
Source File: JavacTrees.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 7
Source File: JavaI18nSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Finds a main top-level class or a nested class element
 * for {@code sourceDataObject} which should be initialized.
 */
private TypeElement getClass(WorkingCopy workingCopy)
                                                    throws IOException {
    workingCopy.toPhase(Phase.ELEMENTS_RESOLVED);

    final String preferredName = sourceDataObject.getName();
    TypeElement firstPublicNestedClass = null;
    
    List<? extends TypeElement> topClasses = workingCopy.getTopLevelElements();
    for (TypeElement topElement : topClasses) {
        ElementKind elementKind = topElement.getKind();
        if (!elementKind.isClass()) {
            continue;
        }

        if (topElement.getSimpleName().contentEquals(preferredName)) {
            return topElement;
        }

        if ((firstPublicNestedClass == null)
                && topElement.getModifiers().contains(Modifier.PUBLIC)) {
            firstPublicNestedClass = topElement;
        }
    }

    return firstPublicNestedClass;
}
 
Example 8
Source File: ElementHandle.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isSameKind (ElementKind k1, ElementKind k2) {
    if ((k1 == k2) ||
       (k1 == ElementKind.OTHER && (k2.isClass() || k2.isInterface())) ||     
       (k2 == ElementKind.OTHER && (k1.isClass() || k1.isInterface()))) {
        return true;
    }
    return false;
}
 
Example 9
Source File: ElementHandle.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if the handle has this same signature as the parameter.
 * The handles has the same signatures if it is resolved into the same
 * element in the same {@link javax.tools.JavaCompiler} task, but may be resolved into
 * the different {@link Element} in the different {@link javax.tools.JavaCompiler} task.
 * @param element to be checked
 * @return true if this handle resolves into the same {@link Element}
 * in the same {@link javax.tools.JavaCompiler} task.
 */
public boolean signatureEquals (@NonNull final T element) {
    final ElementKind ek = element.getKind();
    final ElementKind thisKind = getKind();
    if ((ek != thisKind) && !(thisKind == ElementKind.OTHER && (ek.isClass() || ek.isInterface()))) {
        return false;
    }
    final ElementHandle<T> handle = create (element);
    return signatureEquals (handle);
}
 
Example 10
Source File: DescriptorFactory.java    From buck with Apache License 2.0 5 votes vote down vote up
@Nullable
public String getDescriptor(Element element) {
  ElementKind kind = element.getKind();
  if (kind.isClass() || kind.isInterface()) {
    return null;
  }
  return getType(element).getDescriptor();
}
 
Example 11
Source File: RenameRefactoringUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public CustomRefactoringPanel getPanel(ChangeListener parent) {
    if (panel == null) {
        String suffix = "";
        if(handle != null && handle.getKind() == Tree.Kind.LABELED_STATEMENT) {
            suffix = getString("LBL_Label");
        } else if (handle != null && handle.getElementHandle() !=null) {
            ElementKind kind = handle.getElementHandle().getKind();
            if (kind!=null && (kind.isClass() || kind.isInterface())) {
                suffix  = kind.isInterface() ? getString("LBL_Interface") : getString("LBL_Class");
            } else if (kind == ElementKind.METHOD) {
                suffix = getString("LBL_Method");
            } else if (kind == ElementKind.FIELD) {
                suffix = getString("LBL_Field");
            } else if (kind == ElementKind.LOCAL_VARIABLE) {
                suffix = getString("LBL_LocalVar");
            } else if (kind == ElementKind.PACKAGE || (handle == null && fromListener)) {
                suffix = pkgRename ? getString("LBL_Package") : getString("LBL_Folder");
            } else if (kind == ElementKind.PARAMETER) {
                suffix = getString("LBL_Parameter");
            }
        }
        suffix = suffix + " " + this.oldName; // NOI18N
        panel = new RenamePanel(handle, newName, parent, NbBundle.getMessage(RenamePanel.class, "LBL_Rename") + " " + suffix, !fromListener, fromListener && !byPassPakageRename);
    }
    return panel;
}
 
Example 12
Source File: RenameRefactoringUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public HelpCtx getHelpCtx() {
    String postfix;
    if (handle==null) {
        postfix = ".JavaPackage";//NOI18N
    } else {
        ElementHandle elHandle = handle.getElementHandle();
        if (elHandle == null) {
            postfix = "";
        } else {
            ElementKind k = elHandle.getKind();

            if (k == null) {
                postfix = "";
            } else if (k.isClass() || k.isInterface()) {
                postfix = ".JavaClass";//NOI18N
            } else if (k == ElementKind.METHOD) {
                postfix = ".Method";//NOI18N
            } else if (k.isField()) {
                postfix = ".Field";//NOI18N
            } else {
                postfix = "";
            }
        }
    }
    
    return new HelpCtx(RenameRefactoringUI.class.getName() + postfix);
}
 
Example 13
Source File: RouteProcessor.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private TypeData genTypeData(TypeElement element) {
    ClassName className = ClassName.get(element);
    ElementKind kind = element.getKind();
    if (kind.isClass()) {
        return new TypeData(element, className);
    } else {
        // Element is not a class, miss it
        messager.printMessage(Diagnostic.Kind.WARNING, className + " is not a class");
        return null;
    }
}
 
Example 14
Source File: TraverseProc.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void addPublicTypes(List<TypeElement> list, Element e) {
    ElementKind kind = e.getKind();
    if ((kind.isClass() || kind.isInterface())
            && e.getModifiers().contains(Modifier.PUBLIC)) {
        list.add((TypeElement)e);
        for (Element enc : e.getEnclosedElements()) {
            addPublicTypes(list, enc);
        }
    }
}
 
Example 15
Source File: InstanceRefFinder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private TypeElement findType(Tree selector) {
    TypeMirror tm = ci.getTrees().getTypeMirror(new TreePath(getCurrentPath(), selector));
    if (tm != null && tm.getKind() == TypeKind.DECLARED) {
        TypeElement t = (TypeElement)ci.getTypes().asElement(tm);
        ElementKind ek = t.getKind();
        if (!(ek.isClass() || ek.isInterface())) {
            // PENDING: an error, log
            return null;
        }
        // the referenced type must be in the same CU, cannot be a superclass.
        return t;
    }
    return null;
}
 
Example 16
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public TypeElement getEnclosingTypeElement(Element e) {
    if (e.getKind() == ElementKind.PACKAGE)
        return null;
    Element encl = e.getEnclosingElement();
    ElementKind kind = encl.getKind();
    if (kind == ElementKind.PACKAGE)
        return null;
    while (!(kind.isClass() || kind.isInterface())) {
        encl = encl.getEnclosingElement();
    }
    return (TypeElement)encl;
}
 
Example 17
Source File: AdapterValidator.java    From paperparcel with Apache License 2.0 4 votes vote down vote up
ValidationReport<TypeElement> validate(TypeElement element) {
  ValidationReport.Builder<TypeElement> builder = ValidationReport.about(element);
  if (element.getKind() != ElementKind.CLASS) {
    builder.addError(ErrorMessages.ADAPTER_MUST_BE_CLASS);
  }
  if (element.getModifiers().contains(Modifier.ABSTRACT)) {
    builder.addError(ErrorMessages.ADAPTER_IS_ABSTRACT);
  }
  if (Visibility.ofElement(element) != Visibility.PUBLIC) {
    builder.addError(ErrorMessages.ADAPTER_MUST_BE_PUBLIC);
  } else if (Visibility.effectiveVisibilityOfElement(element) != Visibility.PUBLIC) {
    builder.addError(ErrorMessages.ADAPTER_VISIBILITY_RESTRICTED);
  }
  ElementKind enclosingKind = element.getEnclosingElement().getKind();
  if (enclosingKind.isClass() || enclosingKind.isInterface()) {
    if (!element.getModifiers().contains(Modifier.STATIC)) {
      builder.addError(ErrorMessages.NESTED_ADAPTER_MUST_BE_STATIC);
    }
  }
  TypeMirror adaptedType = Utils.getAdaptedType(elements, types, asDeclared(element.asType()));
  if (Utils.isRawType(adaptedType)) {
    builder.addError(ErrorMessages.ADAPTER_TYPE_ARGUMENT_HAS_RAW_TYPE);
  } else if (Utils.containsWildcards(adaptedType)) {
    builder.addError(ErrorMessages.ADAPTER_TYPE_ARGUMENT_HAS_WILDCARDS);
  } else {
    List<TypeParameterElement> missingParameters = findMissingParameters(element, adaptedType);
    for (TypeParameterElement missingParameter : missingParameters) {
      builder.addError(
          String.format(
              ErrorMessages.ADAPTER_TYPE_ARGUMENT_MISSING_PARAMETER,
              missingParameter.getSimpleName().toString()),
          missingParameter);
    }
  }

  ExecutableElement mainConstructor = Utils.findLargestPublicConstructor(element);
  if (mainConstructor != null) {
    builder.addSubreport(validateConstructor(mainConstructor));
  } else if (adaptedType != null) {
    if (!Utils.isSingletonAdapter(elements, types, element, adaptedType)) {
      builder.addError(ErrorMessages.ADAPTER_MUST_HAVE_PUBLIC_CONSTRUCTOR);
    }
  }
  return builder.build();
}
 
Example 18
Source File: TopClassFinder.java    From netbeans with Apache License 2.0 4 votes vote down vote up
static boolean isTestable(TypeElement typeDeclElement) {
    ElementKind elemKind = typeDeclElement.getKind();
    return (elemKind != ElementKind.ANNOTATION_TYPE)
           && (elemKind.isClass()|| elemKind.isInterface());
}
 
Example 19
Source File: TopClassFinder.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public boolean passes(TypeElement topClass,
                      CompilationInfo compInfo) {
    ElementKind elemKind = topClass.getKind();
    return (elemKind != ElementKind.ANNOTATION_TYPE)
           && (elemKind.isClass()|| elemKind.isInterface());
}
 
Example 20
Source File: SourceNames.java    From immutables with Apache License 2.0 4 votes vote down vote up
private static boolean isTypeElement(Element element) {
  ElementKind kind = element.getKind();
  return kind.isClass()
      || kind.isInterface();
}