@Nullable private static Tree suppressibleNode(TreePath path) { return StreamSupport.stream(path.spliterator(), false) .filter( tree -> tree instanceof MethodTree || (tree instanceof ClassTree && ((ClassTree) tree).getSimpleName().length() != 0) || tree instanceof VariableTree) .findFirst() .orElse(null); }
/** * Adapted from {@link com.google.errorprone.fixes.SuggestedFixes}. * * <p>TODO: actually use {@link * com.google.errorprone.fixes.SuggestedFixes#addSuppressWarnings(VisitorState, String)} instead */ @Nullable private static Tree suppressibleNode(@Nullable TreePath path) { if (path == null) { return null; } return StreamSupport.stream(path.spliterator(), false) .filter( tree -> tree instanceof MethodTree || (tree instanceof ClassTree && ((ClassTree) tree).getSimpleName().length() != 0) || tree instanceof VariableTree) .findFirst() .orElse(null); }
@Override public Description matchClass(ClassTree tree, VisitorState state) { ImmutableAnalysis analysis = createImmutableAnalysis(state); if (tree.getSimpleName().length() == 0) {
private boolean isEmpty(Name name) { return name == null || name.length() == 0; } }
@Override public boolean isUnnamed() { return qualifiedName.length() == 0; }
@Override public boolean isExcluded(TypeElement typeElement) { Name name = typeElement.getQualifiedName(); return name.length() != 0 && isFullyQualifiedNameExcluded( name ); }
private static String toNameId(Name simpleName) { assert simpleName != null; StringBuilder buf = new StringBuilder(); for (int i = 0, n = simpleName.length(); i < n; i++) { char c = simpleName.charAt(i); if (c != '_') { buf.append(Character.toLowerCase(c)); } } return buf.toString(); }
protected TypeElement asImmutableElement(TypeElement typeElement) { Element enclosingElement = typeElement.getEnclosingElement(); StringBuilder builderQualifiedName = new StringBuilder( typeElement.getQualifiedName().length() + 17 ); if ( enclosingElement.getKind() == ElementKind.PACKAGE ) { builderQualifiedName.append( ( (PackageElement) enclosingElement ).getQualifiedName().toString() ); } else { builderQualifiedName.append( ( (TypeElement) enclosingElement ).getQualifiedName().toString() ); } if ( builderQualifiedName.length() > 0 ) { builderQualifiedName.append( "." ); } builderQualifiedName.append( "Immutable" ).append( typeElement.getSimpleName() ); return elementUtils.getTypeElement( builderQualifiedName ); } }
@Nullable private static Tree suppressibleNode(TreePath path) { return StreamSupport.stream(path.spliterator(), false) .filter( tree -> tree instanceof MethodTree || (tree instanceof ClassTree && ((ClassTree) tree).getSimpleName().length() != 0) || tree instanceof VariableTree) .findFirst() .orElse(null); }
private Name createQualifiedName() { TypeElement enclosingType = getEnclosingClass(); String name; if (enclosingType == null) { if (packageName == null || packageName.length() == 0) { name = simpleName.toString(); } else { name = packageName + "." + simpleName; } } else { name = enclosingType.getQualifiedName() + "." + simpleName; } return CodeNames.of(name); }
public void enterClass(ClassTree ct) { if (ct.getSimpleName() == null || ct.getSimpleName().length() == 0 || anonymousCounter > 0) { anonymousCounter++; } else { if (fqn.length() > 0) fqn.append('.'); fqn.append(ct.getSimpleName()); } } public void leaveClass() {
TypeMirror asMemberOf(Element element, TypeMirror type, Types types) { TypeMirror ret = element.asType(); TypeMirror enclType = element.getEnclosingElement().asType(); if (enclType.getKind() == TypeKind.DECLARED) { enclType = types.erasure(enclType); } while (type != null && type.getKind() == TypeKind.DECLARED) { if ((enclType.getKind() != TypeKind.DECLARED || ((DeclaredType) enclType).asElement().getSimpleName().length() > 0) && types.isSubtype(type, enclType)) { ret = types.asMemberOf((DeclaredType) type, element); break; } type = ((DeclaredType) type).getEnclosingType(); } return ret; }
private String stripPrefixFromMethod(ExecutableElement element) { Name name = element.getSimpleName(); CharSequence propertyName; if (isGetter(element) || isSetter(element)) { propertyName = name.subSequence(3, name.length()); } else if (isBooleanGetter(element)) { propertyName = name.subSequence(2, name.length()); } else { L.e("@Bindable associated with method must follow JavaBeans convention %s", element); return null; } char firstChar = propertyName.charAt(0); return "" + Character.toLowerCase(firstChar) + propertyName.subSequence(1, propertyName.length()); }
static boolean isValidSupertypeForClass(TypeElement type) { if (!type.getKind().equals(CLASS)) { return false; } if (type.getModifiers().contains(FINAL)) { return false; } if (!type.getEnclosingElement().getKind().equals(PACKAGE) && !type.getModifiers().contains(STATIC)) { return false; } if (type.getSimpleName().length() == 0) { return false; } return true; }
private ExpressionTree qualIdentFor(Element e) { TreeMaker tm = copy.getTreeMaker(); if (e.getKind() == ElementKind.PACKAGE) { String name = ((PackageElement)e).getQualifiedName().toString(); if (e instanceof Symbol) { int lastDot = name.lastIndexOf('.'); if (lastDot < 0) return tm.Identifier(e); return tm.MemberSelect(qualIdentFor(name.substring(0, lastDot)), e); } return qualIdentFor(name); } Element ee = e.getEnclosingElement(); if (e instanceof Symbol) return ee.getSimpleName().length() > 0 ? tm.MemberSelect(qualIdentFor(ee), e) : tm.Identifier(e); return ee.getSimpleName().length() > 0 ? tm.MemberSelect(qualIdentFor(ee), e.getSimpleName()) : tm.Identifier(e.getSimpleName()); }
@Override public Void visitClass(ClassTree node, Void p) { if (firstInner == -1) { firstInner = ((IndexedClassDecl)node).index; } if (node.getSimpleName().length() != 0) { hasLocalClass = true; } noInner++; handleDoc(node); return super.visitClass(node, p); }
public String visitMemberSelect(MemberSelectTree mst, Void _) { String baseName = scan(mst.getExpression(),_); String name = mst.getIdentifier().toString(); if(baseName.length()>0) name = baseName+'.'+name; PackageElement pe = elements.getPackageElement(name); long ep = srcPos.getEndPosition(cu,mst); long sp = ep-mst.getIdentifier().length(); // addRef(sp,ep,pe); return name; } }.scan(packageName,null);
private boolean generateExceptionBeans(ExecutableElement method) { String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue(); if (packageName.length() == 0) beanPackage = JAXWS_PACKAGE_PD.getValue(); boolean beanGenerated = false; for (TypeMirror thrownType : method.getThrownTypes()) { TypeElement typeDecl = (TypeElement) ((DeclaredType) thrownType).asElement(); if (typeDecl == null) { builder.processError(WebserviceapMessages.WEBSERVICEAP_COULD_NOT_FIND_TYPEDECL( thrownType.toString(), context.getRound())); return false; } boolean tmp = generateExceptionBean(typeDecl, beanPackage); beanGenerated = beanGenerated || tmp; } return beanGenerated; }
private boolean generateExceptionBeans(ExecutableElement method) { String beanPackage = packageName + PD_JAXWS_PACKAGE_PD.getValue(); if (packageName.length() == 0) beanPackage = JAXWS_PACKAGE_PD.getValue(); boolean beanGenerated = false; for (TypeMirror thrownType : method.getThrownTypes()) { TypeElement typeDecl = (TypeElement) ((DeclaredType) thrownType).asElement(); if (typeDecl == null) { builder.processError(WebserviceapMessages.WEBSERVICEAP_COULD_NOT_FIND_TYPEDECL( thrownType.toString(), context.getRound())); return false; } boolean tmp = generateExceptionBean(typeDecl, beanPackage); beanGenerated = beanGenerated || tmp; } return beanGenerated; }
/** * Method invocation of the form "exp.method()" */ public Void visitMethodInvocation(MethodInvocationTree mi, Void _) { ExpressionTree ms = mi.getMethodSelect(); // PRIMARY.methodName portion Element e = TreeUtil.getElement(mi); if (e instanceof ExecutableElement) {// this check is needed in case the source code contains errors ExecutableElement ee = (ExecutableElement) e; Name methodName = ee.getSimpleName(); long ep = srcPos.getEndPosition(cu, ms); if(ep>=0) { // marker for the method name (and jump to definition) gen.add(new MethodRef(ep-methodName.length(),ep,ee)); } } return super.visitMethodInvocation(mi,_); }