private static List<TypeElement> staticTypesIn(Iterable<? extends Element> elements) { List<TypeElement> list = new ArrayList<>(); for (Element element : elements) { if (TYPE_KINDS.contains(element.getKind()) && element.getModifiers().contains(Modifier.STATIC)) { list.add(TypeElement.class.cast(element)); } } return list; }
public static PackageElement getPackage(Element type) { while (type.getKind() != ElementKind.PACKAGE) { type = type.getEnclosingElement(); } return (PackageElement) type; }
private String getListName(Element element, Element param) { String listName = param.getSimpleName().toString(); if (element.getKind() == ElementKind.PARAMETER) { listName = element.getEnclosingElement().getSimpleName().toString() + "_" + listName; } else if (element.getKind() == ElementKind.METHOD) { listName = element.getSimpleName().toString() + "_" + listName; } return listName; }
private String computePath(ExecutableElement element) { String path = element.getSimpleName().toString(); for (Element e = element.getEnclosingElement(); // e.getKind().isClass() || e.getKind().isInterface(); // e = e.getEnclosingElement()) { path = e.getSimpleName() + "." + path; } if (element.getModifiers().contains(Modifier.ABSTRACT)) { return path; } return path + "()"; }
public boolean isInterfaceDefaultMethod() { return element.getEnclosingElement().getKind() == ElementKind.INTERFACE && !element.getModifiers().contains(Modifier.ABSTRACT); }
public void hasNotMultipleAnnotatedMethodWithSameName(Element element, ElementValidation valid, Class<? extends Annotation> annotation) { Set<String> actionNames = new TreeSet<>(); List<? extends Element> enclosedElements = element.getEnclosedElements(); for (Element enclosedElement : enclosedElements) { if (enclosedElement.getKind() != ElementKind.METHOD || !annotationHelper.hasOneOfClassAnnotations(enclosedElement, annotation)) { continue; } String enclosedElementName = enclosedElement.getSimpleName().toString(); if (actionNames.contains(enclosedElementName)) { valid.addError(enclosedElement, "The " + TargetAnnotationHelper.annotationName(annotation) + " annotated method must have unique name even if the signature is not the same"); } else { actionNames.add(enclosedElementName); } } }
@Override public boolean apply(Element input) { if (input.getKind() != ElementKind.METHOD) { return false; } if (isJavaLangObject((TypeElement) input.getEnclosingElement())) { return false; } ExecutableElement element = (ExecutableElement) input; boolean parameterless = element.getParameters().isEmpty(); boolean nonstatic = !element.getModifiers().contains(Modifier.STATIC); return parameterless && nonstatic; } }
public Set<String> getNonAttributeAbstractMethodSignatures() { if (element.getKind().isClass() || element.getKind().isInterface()) { Set<String> signatures = new LinkedHashSet<>(); List<? extends Element> members = constitution.protoclass() .environment() .processing() .getElementUtils() .getAllMembers(CachingElements.getDelegate((TypeElement) element)); for (ExecutableElement m : ElementFilter.methodsIn(members)) { if (!m.getParameters().isEmpty() || m.getSimpleName().contentEquals(AccessorAttributesCollector.HASH_CODE_METHOD) || m.getSimpleName().contentEquals(AccessorAttributesCollector.TO_STRING_METHOD)) { if (m.getModifiers().contains(Modifier.ABSTRACT)) { TypeMirror returnType = m.getReturnType(); if (!AccessorAttributesCollector.isEclipseImplementation(m)) { returnType = AccessorAttributesCollector.asInheritedMemberReturnType( constitution.protoclass().processing(), CachingElements.getDelegate((TypeElement) element), m); } signatures.add(toSignature(m, returnType)); } } } return signatures; } return Collections.emptySet(); }
/** * Returns true if generated code can invoke {@code constructor}. That is, if * the constructor is non-private and its enclosing class is either a * top-level class or a static nested class. */ public static boolean isCallableConstructor(ExecutableElement constructor) { if (constructor.getModifiers().contains(Modifier.PRIVATE)) { return false; } TypeElement type = (TypeElement) constructor.getEnclosingElement(); return type.getEnclosingElement().getKind() == ElementKind.PACKAGE || type.getModifiers().contains(Modifier.STATIC); }
private boolean validateInjectable(Element injectable) { Element injectableType = injectable.getEnclosingElement(); if (injectable.getKind() == ElementKind.CLASS) { error("@Inject is not valid on a class: " + elementToString(injectable), injectable); return false; if (injectable.getKind() == ElementKind.METHOD) { error("Method injection is not supported: " + elementToString(injectable), injectable); return false; if (injectable.getKind() == ElementKind.FIELD && injectable.getModifiers().contains(FINAL)) { error("Can't inject a final field: " + elementToString(injectable), injectable); return false; if (injectable.getKind() == ElementKind.FIELD && injectable.getModifiers().contains(PRIVATE)) { error("Can't inject a private field: " + elementToString(injectable), injectable); return false; if (injectable.getKind() == ElementKind.CONSTRUCTOR && injectable.getModifiers().contains(PRIVATE)) { error("Can't inject a private constructor: " + elementToString(injectable), injectable); return false; ElementKind elementKind = injectableType.getEnclosingElement().getKind(); boolean isClassOrInterface = elementKind.isClass() || elementKind.isInterface(); boolean isStatic = injectableType.getModifiers().contains(STATIC);
@Override public boolean accept(Element e, TypeMirror t) { return (e.getKind().isClass() || e.getKind().isInterface()) && e.getSimpleName().contentEquals(simpleName) && (!isStatic || e.getModifiers().contains(STATIC)) && trees.isAccessible(scope, e, (DeclaredType) t); } };
Element element = compilationInfo.getTrees().getElement(errorPath); if ( element == null || element.getSimpleName() == null || errorPath.getLeaf().getKind() != Kind.IDENTIFIER ) if ( parentPath.getLeaf().getKind() != Kind.ANNOTATION ){ return Collections.<Fix>emptyList(); Element annotation = compilationInfo.getTrees().getElement(parentPath); TreePath path = parentPath; while (path != null ){ Tree leaf = path.getLeaf(); Kind leafKind = leaf.getKind(); if ( TreeUtilities.CLASS_TREE_KINDS.contains(leafKind) ){ Element clazz = compilationInfo.getTrees().getElement(path); if ( clazz != null && clazz.getKind() == ElementKind.CLASS ) return null; Element parent = var.getEnclosingElement(); if ( var.getKind() == ElementKind.FIELD && (parent instanceof TypeElement)) Element method = compilationInfo.getTrees().getElement(path); if ( method != null && method.getKind() == ElementKind.METHOD){ return analyzeMethodParameter( compilationInfo , (ExecutableElement)method , annotation );
private @NonNull VerifyResult fullVerifyElements(TreePath node, TreePath p) { Element nodeEl = info.getTrees().getElement(node); Element pEl = info.getTrees().getElement(p); if (!nodeEl.getModifiers().contains(Modifier.STATIC)) { if ((nodeEl.getKind().isClass() || nodeEl.getKind().isInterface())) { if (p.getLeaf().getKind() == Tree.Kind.MEMBER_SELECT && node.getLeaf().getKind() == Tree.Kind.MEMBER_SELECT) { Tree selector = ((MemberSelectTree)p.getLeaf()).getExpression(); if (getWildcardTreeName(selector) != null) { Element nodeSelector = info.getTrees().getElement(new TreePath(node, ((MemberSelectTree)node.getLeaf()).getExpression())); if (nodeSelector != null && (nodeSelector.getKind().isClass() || nodeSelector.getKind().isInterface())) { matchingResult = VerifyResult.NO_MATCH; } else { return VerifyResult.NO_MATCH; if (nodeEl.getKind() == pEl.getKind() && nodeEl.getKind() == ElementKind.FIELD && CLASS.contentEquals(((VariableElement)nodeEl).getSimpleName()) && CLASS.contentEquals(((VariableElement)pEl).getSimpleName())) { if (nodeEl.getKind() == pEl.getKind() && nodeEl.getKind() == ElementKind.METHOD) { if (info.getElements().overrides((ExecutableElement) nodeEl, (ExecutableElement) pEl, (TypeElement) nodeEl.getEnclosingElement())) { return VerifyResult.MATCH_CHECK_DEEPER;
public void run(WorkingCopy workingCopy) throws Exception { workingCopy.toPhase(Phase.RESOLVED); CompilationUnitTree cut = workingCopy.getCompilationUnit(); gu = GenerationUtils.newInstance(workingCopy); for (Tree typeDecl : cut.getTypeDecls()) { if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) { Element e = workingCopy.getTrees().getElement(new TreePath(new TreePath(workingCopy.getCompilationUnit()), typeDecl)); if (e != null && e.getKind().isClass()) { TypeElement te = (TypeElement) e; ClassTree ct = (ClassTree) typeDecl; workingCopy.rewrite(ct, generateInterfaces(workingCopy, te, ct, gu)); } } } } };
@Override public Void visitIdentifier(IdentifierTree node, Void p) { if (!map.containsKey(node.getName())) { Element element = trees.getElement(getCurrentPath()); if (element != null && (element.getKind().isClass() || element.getKind().isInterface()) && element.asType().getKind() != TypeKind.ERROR) { map.put(node.getName(), (TypeElement) element); } } return super.visitIdentifier(node, p); }
Accessor(Element element) { this.element = element; this.name = element.getSimpleName().toString(); this.callable = element.getKind() == ElementKind.METHOD; }
public void run(CompilationController compilationController) throws Exception { compilationController.toPhase(Phase.ELEMENTS_RESOLVED); Trees trees = compilationController.getTrees(); CompilationUnitTree compilationUnitTree = compilationController.getCompilationUnit(); List<?extends Tree> typeDecls = compilationUnitTree.getTypeDecls(); for (Tree tree : typeDecls) { Element element = trees.getElement(trees.getPath(compilationUnitTree, tree)); if (element != null && element.getKind() == ElementKind.CLASS && element.getSimpleName().contentEquals(fo.getName())){ long pos = trees.getSourcePositions().getStartPosition(compilationUnitTree, tree); line[0] = compilationUnitTree.getLineMap().getLineNumber(pos); break; } } } }, true);
CachingElement(Element delegate) { this.delegate = delegate; this.kind = delegate.getKind(); this.simpleName = delegate.getSimpleName(); this.modifiers = delegate.getModifiers(); }
private static boolean isTypeElement(Element element) { ElementKind kind = element.getKind(); return kind.isClass() || kind.isInterface(); }
private String getPackageName(CompilationInfo cinfo) { Element e = cinfo.getTrees().getElement(new TreePath(cinfo.getCompilationUnit())); if (e != null && e.getKind() == ElementKind.PACKAGE) { return ((PackageElement) e).getQualifiedName().toString(); } return null; }