Java Code Examples for javax.lang.model.type.TypeMirror#accept()

The following examples show how to use javax.lang.model.type.TypeMirror#accept() . 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: Util.java    From revapi with Apache License 2.0 6 votes vote down vote up
@Override
protected Void doVisitTypeVariable(TypeVariable t, StringBuilderAndState<TypeMirror> state) {
    Name tName = t.asElement().getSimpleName();
    if (state.depth > state.anticipatedTypeVarDeclDepth && state.forwardTypeVarDecls.contains(tName)) {
        state.bld.append(tName);
        return null;
    }

    state.bld.append(t.asElement().getSimpleName());

    TypeMirror lowerBound = IgnoreCompletionFailures.in(t::getLowerBound);

    if (lowerBound != null && lowerBound.getKind() != TypeKind.NULL) {
        state.bld.append(" super ");
        lowerBound.accept(this, state);
    }

    TypeMirror upperBound = IgnoreCompletionFailures.in(t::getUpperBound);

    if (!isJavaLangObject.visit(upperBound)) {
        state.bld.append(" extends ");
        upperBound.accept(this, state);
    }

    return null;
}
 
Example 2
Source File: MoreTypes.java    From auto with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("TypeEquals")
private static boolean equal(TypeMirror a, TypeMirror b, Set<ComparedElements> visiting) {
  // TypeMirror.equals is not guaranteed to return true for types that are equal, but we can
  // assume that if it does return true then the types are equal. This check also avoids getting
  // stuck in infinite recursion when Eclipse decrees that the upper bound of the second K in
  // <K extends Comparable<K>> is a distinct but equal K.
  // The javac implementation of ExecutableType, at least in some versions, does not take thrown
  // exceptions into account in its equals implementation, so avoid this optimization for
  // ExecutableType.
  if (Objects.equal(a, b) && !(a instanceof ExecutableType)) {
    return true;
  }
  EqualVisitorParam p = new EqualVisitorParam();
  p.type = b;
  p.visiting = visiting;
  return (a == b) || (a != null && b != null && a.accept(EqualVisitor.INSTANCE, p));
}
 
Example 3
Source File: ElementTo.java    From sundrio with Apache License 2.0 6 votes vote down vote up
public TypeRef apply(TypeMirror item) {
    if (item instanceof NoType) {
        return new VoidRef();
    }

    Element element = CodegenContext.getContext().getTypes().asElement(item);
    TypeDef known = element != null ? CodegenContext.getContext().getDefinitionRepository().getDefinition(element.toString()) : null;

    if (known == null && element instanceof TypeElement) {
        known = TYPEDEF.apply((TypeElement) element);
    }
    TypeRef typeRef = item.accept(new TypeRefTypeVisitor(), 0);
    if (typeRef instanceof ClassRef && known != null) {
        return new ClassRefBuilder((ClassRef) typeRef).withDefinition(known).build();
    }
    return typeRef;
}
 
Example 4
Source File: SignatureFactory.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitTypeParameter(TypeParameterElement element, SignatureVisitor visitor) {
  visitor.visitFormalTypeParameter(element.getSimpleName().toString());
  for (TypeMirror boundType : element.getBounds()) {
    boolean isClass;
    try {
      if (boundType.getKind() == TypeKind.DECLARED) {
        isClass = ((DeclaredType) boundType).asElement().getKind().isClass();
      } else {
        isClass = boundType.getKind() == TypeKind.TYPEVAR;
      }
    } catch (CannotInferException e) {
      // We can't know whether an inferred type is a class or interface, but it turns out
      // the compiler does not distinguish between them when reading signatures, so we can
      // write inferred types as interface bounds. We go ahead and write all bounds as
      // interface bounds to make the SourceAbiCompatibleSignatureVisitor possible.
      isClass = false;
    }

    boundType.accept(
        typeVisitorAdapter,
        isClass ? visitor.visitClassBound() : visitor.visitInterfaceBound());
  }

  return null;
}
 
Example 5
Source File: Util.java    From revapi with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitTypeVariable(TypeVariable t, StringBuilderAndState<TypeMirror> state) {
    if (state.visitingMethod) {
        TypeMirror upperBound = IgnoreCompletionFailures.in(t::getUpperBound);
        upperBound.accept(this, state);
        return null;
    }

    if (state.visitedObjects.contains(t)) {
        state.bld.append("%");
        return null;
    }

    state.visitedObjects.add(t);

    TypeMirror lowerBound = IgnoreCompletionFailures.in(t::getLowerBound);

    if (lowerBound != null && lowerBound.getKind() != TypeKind.NULL) {
        lowerBound.accept(this, state);
        state.bld.append("-");
    }

    IgnoreCompletionFailures.in(t::getUpperBound).accept(this, state);
    state.bld.append("+");
    return null;
}
 
Example 6
Source File: ExportNonAccessibleElement.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public Boolean visitType(TypeElement arg0, Void arg1) {
    for (TypeParameterElement e : arg0.getTypeParameters()) {
        if (stop) {
            return false;
        }
        
        for (TypeMirror b : e.getBounds()) {
            if (stop) {
                return false;
            }
            
            if (b.accept(this, arg1)) {
                return true;
            }
        }
    }

    TypeMirror superclass = arg0.getSuperclass();
    if (superclass.getKind() == TypeKind.DECLARED) {
        if (!((DeclaredType) superclass).asElement().getKind().isInterface()) {
            return false;
        }
    }

    return superclass.accept(this, arg1);
}
 
Example 7
Source File: MoreTypes.java    From auto-parcel with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link TypeVariable} if the {@link TypeMirror} represents a type variable
 * or throws an {@link IllegalArgumentException}.
 */
public static TypeVariable asTypeVariable(TypeMirror maybeTypeVariable) {
    return maybeTypeVariable.accept(new CastingTypeVisitor<TypeVariable>() {
        @Override
        public TypeVariable visitTypeVariable(TypeVariable type, String p) {
            return type;
        }
    }, "type variable");
}
 
Example 8
Source File: MoreTypes.java    From auto with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitDeclared(DeclaredType t, ImmutableSet.Builder<TypeElement> p) {
  p.add(MoreElements.asType(t.asElement()));
  for (TypeMirror typeArgument : t.getTypeArguments()) {
    typeArgument.accept(this, p);
  }
  return null;
}
 
Example 9
Source File: Util.java    From revapi with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitWildcard(WildcardType t, StringBuilderAndState<TypeMirror> state) {
    TypeMirror extendsBound = IgnoreCompletionFailures.in(t::getExtendsBound);

    if (state.visitingMethod) {
        if (extendsBound != null) {
            extendsBound.accept(this, state);
        } else {
            //super bound or unbound wildcard
            state.bld.append("java.lang.Object");
        }

        return null;
    }

    TypeMirror superBound = IgnoreCompletionFailures.in(t::getSuperBound);

    if (superBound != null) {
        superBound.accept(this, state);
        state.bld.append("-");
    }

    if (extendsBound != null) {
        extendsBound.accept(this, state);
        state.bld.append("+");
    }

    return null;
}
 
Example 10
Source File: ExportNonAccessibleElement.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public Boolean visitDeclared(DeclaredType arg0, Void arg1) {
    if (!isVisible(arg0.asElement())) {
        return true;
    }
    for (TypeMirror t : arg0.getTypeArguments()) {
        if (stop) {
            return false;
        }
        
        if (t.accept(this, arg1)) {
            return true;
        }
    }
    return arg0.getEnclosingType().accept(this, arg1);
}
 
Example 11
Source File: TypeSimplifier.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
private void appendTypeParameterWithBounds(StringBuilder sb, TypeParameterElement typeParameter) {
  sb.append(typeParameter.getSimpleName());
  String sep = " extends ";
  for (TypeMirror bound : typeParameter.getBounds()) {
    if (!bound.toString().equals("java.lang.Object")) {
      sb.append(sep);
      sep = " & ";
      bound.accept(TO_STRING_TYPE_VISITOR, sb);
    }
  }
}
 
Example 12
Source File: MoreTypes.java    From doma with Apache License 2.0 5 votes vote down vote up
public Void visitWildcard(WildcardType t, StringBuilder p) {
  p.append("?");
  TypeMirror extendsBound = t.getExtendsBound();
  if (extendsBound != null) {
    p.append(" extends ");
    extendsBound.accept(this, p);
  }
  TypeMirror superBound = t.getSuperBound();
  if (superBound != null) {
    p.append(" super ");
    superBound.accept(this, p);
  }
  return null;
}
 
Example 13
Source File: BasicAnnoTests.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
R scan(TypeMirror t, P p) {
    return (t == null) ? DEFAULT_VALUE : t.accept(this, p);
}
 
Example 14
Source File: MissingTypes.java    From auto with Apache License 2.0 4 votes vote down vote up
private Void visitAll(List<? extends TypeMirror> types, TypeMirrorSet visiting) {
  for (TypeMirror type : types) {
    type.accept(this, visiting);
  }
  return null;
}
 
Example 15
Source File: WebServiceVisitor.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
protected boolean isLegalMethod(ExecutableElement method, TypeElement typeElement) {
    WebMethod webMethod = method.getAnnotation(WebMethod.class);
    //SEI cannot have methods with @WebMethod(exclude=true)
    if (typeElement.getKind().equals(ElementKind.INTERFACE) && webMethod != null && webMethod.exclude())
        builder.processError(WebserviceapMessages.WEBSERVICEAP_INVALID_SEI_ANNOTATION_ELEMENT_EXCLUDE("exclude=true", typeElement.getQualifiedName(), method.toString()), method);
    // With https://jax-ws.dev.java.net/issues/show_bug.cgi?id=577, hasWebMethods has no effect
    if (hasWebMethods && webMethod == null) // backwards compatibility (for legacyWebMethod computation)
        return true;

    if ((webMethod != null) && webMethod.exclude()) {
        return true;
    }
    /*
    This check is not needed as Impl class is already checked that it is not abstract.
    if (typeElement instanceof TypeElement && method.getModifiers().contains(Modifier.ABSTRACT)) {  // use Kind.equals instead of instanceOf
        builder.processError(method.getPosition(), WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_METHOD_IS_ABSTRACT(typeElement.getQualifiedName(), method.getSimpleName()));
        return false;
    }
    */
    TypeMirror returnType = method.getReturnType();
    if (!isLegalType(returnType)) {
        builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_RETURN_TYPE_CANNOT_IMPLEMENT_REMOTE(typeElement.getQualifiedName(),
                method.getSimpleName(),
                returnType), method);
    }
    boolean isOneWay = method.getAnnotation(Oneway.class) != null;
    if (isOneWay && !isValidOneWayMethod(method, typeElement))
        return false;

    SOAPBinding soapBinding = method.getAnnotation(SOAPBinding.class);
    if (soapBinding != null) {
        if (soapBinding.style().equals(SOAPBinding.Style.RPC)) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_RPC_SOAPBINDING_NOT_ALLOWED_ON_METHOD(typeElement.getQualifiedName(), method.toString()), method);
        }
    }

    int paramIndex = 0;
    for (VariableElement parameter : method.getParameters()) {
        if (!isLegalParameter(parameter, method, typeElement, paramIndex++))
            return false;
    }

    if (!isDocLitWrapped() && soapStyle.equals(SOAPStyle.DOCUMENT)) {
        VariableElement outParam = getOutParameter(method);
        int inParams = getModeParameterCount(method, WebParam.Mode.IN);
        int outParams = getModeParameterCount(method, WebParam.Mode.OUT);
        if (inParams != 1) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_DOC_BARE_AND_NO_ONE_IN(typeElement.getQualifiedName(), method.toString()), method);
        }
        if (returnType.accept(NO_TYPE_VISITOR, null)) {
            if (outParam == null && !isOneWay) {
                builder.processError(WebserviceapMessages.WEBSERVICEAP_DOC_BARE_NO_OUT(typeElement.getQualifiedName(), method.toString()), method);
            }
            if (outParams != 1) {
                if (!isOneWay && outParams != 0)
                    builder.processError(WebserviceapMessages.WEBSERVICEAP_DOC_BARE_NO_RETURN_AND_NO_OUT(typeElement.getQualifiedName(), method.toString()), method);
            }
        } else {
            if (outParams > 0) {
                builder.processError(WebserviceapMessages.WEBSERVICEAP_DOC_BARE_RETURN_AND_OUT(typeElement.getQualifiedName(), method.toString()), outParam);
            }
        }
    }
    return true;
}
 
Example 16
Source File: BasicAnnoTests.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
R scan(TypeMirror t, P p) {
    return (t == null) ? DEFAULT_VALUE : t.accept(this, p);
}
 
Example 17
Source File: Utils.java    From paperparcel with Apache License 2.0 4 votes vote down vote up
/** Returns true if {@code typeMirror} contains any wildcards. */
static boolean containsWildcards(TypeMirror typeMirror) {
  Set<TypeParameterElement> visited = new HashSet<>();
  return typeMirror.accept(CheckWildcardsVisitor.INSTANCE, visited);
}
 
Example 18
Source File: BasicAnnoTests.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
R scan(TypeMirror t, P p) {
    return (t == null) ? DEFAULT_VALUE : t.accept(this, p);
}
 
Example 19
Source File: MoreTypes.java    From auto with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a {@link ExecutableType} if the {@link TypeMirror} represents an executable type such
 * as a method, constructor, or initializer or throws an {@link IllegalArgumentException}.
 */
public static ExecutableType asExecutable(TypeMirror maybeExecutableType) {
  return maybeExecutableType.accept(ExecutableTypeVisitor.INSTANCE, null);
}
 
Example 20
Source File: MoreTypes.java    From immutables with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a {@link PrimitiveType} if the {@link TypeMirror} represents a primitive type or throws
 * an {@link IllegalArgumentException}.
 */
public static PrimitiveType asPrimitiveType(TypeMirror maybePrimitiveType) {
  return maybePrimitiveType.accept(PrimitiveTypeVisitor.INSTANCE, null);
}