Java Code Examples for javax.lang.model.util.Elements#getAllMembers()

The following examples show how to use javax.lang.model.util.Elements#getAllMembers() . 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: VarUsageVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean isMethodAvailable(TypeElement subTypeElement, 
        ExecutableElement execElem, TypeElement superTypeElement) {
    Elements elements = workingCopy.getElements();
    List<? extends Element> memberElements = elements.getAllMembers(superTypeElement);
    for (Element elem : memberElements) {
        if(ElementKind.METHOD.equals(elem.getKind())){
            if(execElem.getModifiers().contains(Modifier.STATIC) && elements.hides(execElem, elem)){
                return true;
            }else{
                if(execElem.equals(elem) || elements.overrides(execElem, (ExecutableElement)elem, 
                        subTypeElement)){
                    return true;
                }
            }
        }
    }

    return false;
}
 
Example 2
Source File: RenameRefactoringPlugin.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Element hides(Element field, String name, CompilationInfo info) {
        Elements elements = info.getElements();
        ElementUtilities utils = info.getElementUtilities();
        TypeElement jc = utils.enclosingTypeElement(field);
        for (Element el:elements.getAllMembers(jc)) {
//TODO:
//            if (utils.willHide(el, field, name)) {
//                return el;
//            }
            if (el.getKind().isField()) {
                if (el.getSimpleName().toString().equals(name)) {
                    if (!el.getEnclosingElement().equals(field.getEnclosingElement())) {
                        return el;
                    }
                }
            }
        }
        return null;
    }
 
Example 3
Source File: Utils.java    From paperparcel with Apache License 2.0 6 votes vote down vote up
/** If {@code subject} has a {@code Parcelable.Creator} field instance, return it. */
@Nullable static VariableElement findCreator(
    Elements elements, Types types, TypeElement subject) {

  TypeMirror creatorType = types.getDeclaredType(
      elements.getTypeElement(PARCELABLE_CREATOR_CLASS_NAME),
      types.getWildcardType(null, null));

  List<? extends Element> members = elements.getAllMembers(subject);
  for (VariableElement field : ElementFilter.fieldsIn(members)) {
    if (field.getSimpleName().contentEquals("CREATOR")
        && types.isAssignable(field.asType(), creatorType)
        && field.getModifiers().contains(Modifier.STATIC)
        && field.getModifiers().contains(Modifier.PUBLIC)) {
      return field;
    }
  }

  return null;
}
 
Example 4
Source File: TestMethodNameGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Collects names of accessible no-argument methods that are present
 * in the given class and its superclasses. Methods inherited from the
 * class's superclasses are taken into account, too.
 * 
 * @param  clazz  class whose methods' names should be collected
 * @param  reservedMethodNames  collection to which the method names
 *                              should be added
 */
private void collectExistingMethodNames(TypeElement clazz,
                                        Collection<String> reservedMethodNames) {
    final Elements elements = workingCopy.getElements();
    List<? extends Element> allMembers = elements.getAllMembers(clazz);
    List<? extends ExecutableElement> methods = ElementFilter.methodsIn(allMembers);
    if (!methods.isEmpty()) {
        for (ExecutableElement method : methods) {
            if (method.getParameters().isEmpty()) {
                reservedMethodNames.add(method.getSimpleName().toString());
            }
        }
    }
}
 
Example 5
Source File: TestMethodNameGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Collects names of accessible no-argument methods that are present
 * in the given class and its superclasses. Methods inherited from the
 * class's superclasses are taken into account, too.
 * 
 * @param  clazz  class whose methods' names should be collected
 * @param  reservedMethodNames  collection to which the method names
 *                              should be added
 */
private void collectExistingMethodNames(TypeElement clazz,
                                        Collection<String> reservedMethodNames) {
    final Elements elements = workingCopy.getElements();
    List<? extends Element> allMembers = elements.getAllMembers(clazz);
    List<? extends ExecutableElement> methods = ElementFilter.methodsIn(allMembers);
    if (!methods.isEmpty()) {
        for (ExecutableElement method : methods) {
            if (method.getParameters().isEmpty()) {
                reservedMethodNames.add(method.getSimpleName().toString());
            }
        }
    }
}
 
Example 6
Source File: UseSuperTypeRefactoringPlugin.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean hidesSupTypeMember(Element methElement, TypeElement superTypeElement) {
    Elements elements = workingCopy.getElements();
    List<? extends Element> containedElements = elements.getAllMembers(superTypeElement);
    for (Element elem : containedElements) {
        boolean isPresentInSuperType = methElement.equals(elem) || 
                elements.hides(methElement, elem);
        if ((elem != null) && isStatic(elem) && isPresentInSuperType) {
            return true;
        }
    }
    return false;
}
 
Example 7
Source File: VarUsageVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean isHidingMember(TypeElement subTypeElement, Element variableElement, 
        TypeElement superTypeElement) {
    //TODO: We do not handle nested types yet (includes enums)
    Elements elements = workingCopy.getElements();
    List<? extends Element> memberElements = elements.getAllMembers(superTypeElement);
    for (Element elem : memberElements) {
        if(variableElement.equals(elem) || elements.hides(variableElement, elem)){
            return true;
        }
    }
    return false;
}
 
Example 8
Source File: TurbineElementsGetAllMembersTest.java    From turbine with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
  JavacTask javacTask =
      IntegrationTestSupport.runJavacAnalysis(
          input.sources, ImmutableList.of(), ImmutableList.of());
  Elements javacElements = javacTask.getElements();
  List<? extends Element> javacMembers =
      javacElements.getAllMembers(requireNonNull(javacElements.getTypeElement("Test")));

  ImmutableList<CompUnit> units =
      input.sources.entrySet().stream()
          .map(e -> new SourceFile(e.getKey(), e.getValue()))
          .map(Parser::parse)
          .collect(toImmutableList());

  Binder.BindingResult bound =
      Binder.bind(
          units,
          ClassPathBinder.bindClasspath(ImmutableList.of()),
          TestClassPaths.TURBINE_BOOTCLASSPATH,
          Optional.empty());

  Env<ClassSymbol, TypeBoundClass> env =
      CompoundEnv.<ClassSymbol, TypeBoundClass>of(bound.classPathEnv())
          .append(new SimpleEnv<>(bound.units()));
  ModelFactory factory = new ModelFactory(env, ClassLoader.getSystemClassLoader(), bound.tli());
  TurbineTypes turbineTypes = new TurbineTypes(factory);
  TurbineElements turbineElements = new TurbineElements(factory, turbineTypes);
  List<? extends Element> turbineMembers =
      turbineElements.getAllMembers(factory.typeElement(new ClassSymbol("Test")));

  assertThat(formatElements(turbineMembers))
      .containsExactlyElementsIn(formatElements(javacMembers));
}
 
Example 9
Source File: SqlBuilderHelper.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Iterate over methods.
 *
 * @param typeElement
 *            the type element
 * @param listener
 *            the listener
 */
public static void forEachMethods(TypeElement typeElement, MethodFoundListener listener) {
	Elements elementUtils = BaseProcessor.elementUtils;
	List<? extends Element> list = elementUtils.getAllMembers(typeElement);

	for (Element item : list) {
		if (item.getKind() == ElementKind.METHOD) {
			listener.onMethod((ExecutableElement) item);
		}
	}
}
 
Example 10
Source File: Helper.java    From vertx-codegen with Apache License 2.0 4 votes vote down vote up
/**
 * Resolves a documentation signature, null can be returned if no element can be resolved.
 *
 * @param elementUtils the element utils
 * @param typeUtils the type utils
 * @param declaringElt the declaring element, may be null
 * @param signature the signature to resolve
 * @return the resolved element
 */
public static Element resolveSignature(
    Elements elementUtils,
    Types typeUtils,
    TypeElement declaringElt,
    String signature) {
  Matcher signatureMatcher = SIGNATURE_PATTERN.matcher(signature);
  if (signatureMatcher.find()) {
    String memberName = signatureMatcher.group(1);
    String typeName = signature.substring(0, signatureMatcher.start());
    TypeElement typeElt = resolveTypeElement(elementUtils, declaringElt, typeName);
    if (typeElt != null) {
      Predicate<? super Element> memberMatcher;
      if (signatureMatcher.group(2) != null) {
        String t = signatureMatcher.group(2).trim();
        Predicate<ExecutableElement> parametersMatcher;
        if (t.length() == 0) {
          parametersMatcher = exeElt -> exeElt.getParameters().isEmpty();
        } else {
          parametersMatcher = parametersMatcher(typeUtils, t.split("\\s*,\\s*"));
        }
        memberMatcher = elt -> matchesConstructor(elt, memberName, parametersMatcher) || matchesMethod(elt, memberName, parametersMatcher);
      } else {
        memberMatcher = elt -> matchesConstructor(elt, memberName, exeElt -> true) ||
            matchesMethod(elt, memberName, exeElt -> true) ||
            matchesField(elt, memberName);
      }
      // The order of kinds is important
      for (ElementKind kind : Arrays.asList(ElementKind.FIELD, ElementKind.CONSTRUCTOR, ElementKind.METHOD)) {
        for (Element memberElt : elementUtils.getAllMembers(typeElt)) {
          if(memberElt.getKind() == kind && memberMatcher.test(memberElt)) {
             return memberElt;
          }
        }
      }
    }
    return null;
  } else {
    return resolveTypeElement(elementUtils, declaringElt, signature);
  }
}