javax.lang.model.type.NoType Java Examples

The following examples show how to use javax.lang.model.type.NoType. 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: 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 #2
Source File: TreeBackedTypesTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDeclaredTypeTopLevelRawType() throws IOException {
  compile("class Foo<T> { }");

  TypeElement fooElement = elements.getTypeElement("Foo");
  DeclaredType fooTypeMirror = types.getDeclaredType(fooElement);

  assertEquals(TypeKind.DECLARED, fooTypeMirror.getKind());
  DeclaredType fooDeclaredType = fooTypeMirror;
  assertNotSame(fooElement.asType(), fooDeclaredType);
  assertSame(fooElement, fooDeclaredType.asElement());
  assertEquals(0, fooDeclaredType.getTypeArguments().size());

  TypeMirror enclosingType = fooDeclaredType.getEnclosingType();
  assertEquals(TypeKind.NONE, enclosingType.getKind());
  assertTrue(enclosingType instanceof NoType);
}
 
Example #3
Source File: TreeBackedTypesTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDeclaredTypeTopLevelNoGenerics() throws IOException {
  compile("class Foo { }");

  TypeElement fooElement = elements.getTypeElement("Foo");
  DeclaredType fooTypeMirror = types.getDeclaredType(fooElement);

  assertEquals(TypeKind.DECLARED, fooTypeMirror.getKind());
  DeclaredType fooDeclaredType = fooTypeMirror;
  assertNotSame(fooElement.asType(), fooDeclaredType);
  assertSame(fooElement, fooDeclaredType.asElement());
  assertEquals(0, fooDeclaredType.getTypeArguments().size());

  TypeMirror enclosingType = fooDeclaredType.getEnclosingType();
  assertEquals(TypeKind.NONE, enclosingType.getKind());
  assertTrue(enclosingType instanceof NoType);
}
 
Example #4
Source File: TreeBackedTypeElementTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsTypeGeneric() throws IOException {
  compile("class Foo<T> { }");

  TypeElement fooElement = elements.getTypeElement("Foo");
  TypeMirror fooTypeMirror = fooElement.asType();

  assertEquals(TypeKind.DECLARED, fooTypeMirror.getKind());
  DeclaredType fooDeclaredType = (DeclaredType) fooTypeMirror;
  assertSame(fooElement, fooDeclaredType.asElement());
  List<? extends TypeMirror> typeArguments = fooDeclaredType.getTypeArguments();
  assertEquals("T", ((TypeVariable) typeArguments.get(0)).asElement().getSimpleName().toString());
  assertEquals(1, typeArguments.size());

  TypeMirror enclosingType = fooDeclaredType.getEnclosingType();
  assertEquals(TypeKind.NONE, enclosingType.getKind());
  assertTrue(enclosingType instanceof NoType);
}
 
Example #5
Source File: TreeBackedTypeElementTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsType() throws IOException {
  compile("class Foo { }");

  TypeElement fooElement = elements.getTypeElement("Foo");
  TypeMirror fooTypeMirror = fooElement.asType();

  assertEquals(TypeKind.DECLARED, fooTypeMirror.getKind());
  DeclaredType fooDeclaredType = (DeclaredType) fooTypeMirror;
  assertSame(fooElement, fooDeclaredType.asElement());
  assertEquals(0, fooDeclaredType.getTypeArguments().size());

  TypeMirror enclosingType = fooDeclaredType.getEnclosingType();
  assertEquals(TypeKind.NONE, enclosingType.getKind());
  assertTrue(enclosingType instanceof NoType);
}
 
Example #6
Source File: StructuralTypeEraser.java    From manifold with Apache License 2.0 6 votes vote down vote up
private Type eraseBound( Type t, Type bound )
{
  if( bound == null || bound instanceof NoType )
  {
    return bound;
  }

  Type erasedBound;
  if( bound.contains( t ) )
  {
    erasedBound = visit( _types.erasure( bound ) );
  }
  else
  {
    erasedBound = visit( bound );
  }
  return erasedBound;
}
 
Example #7
Source File: BaseGenerator.java    From data-mediator with Apache License 2.0 6 votes vote down vote up
private TypeElement getSuperInterfaceImpl(TypeMirror tm, GroupPropertyGenerator.TypeElementDelegate delegate) {
    String superName = tm.toString();
    if(tm instanceof NoType){
        //no super.
    }else if(superName.startsWith("java.") || superName.startsWith("android.")){
        // no super too.
    }else {
        TypeElement newTe = new FieldData.TypeCompat(getTypes(), tm).getElementAsType();
        TypeElement ele = delegate.get(superName);
        if(ele == null){
            if(hasAnnotation(newTe)){
                return newTe;
            }
            return getSuperInterface(newTe, delegate);
        }else {
            return newTe;
        }
    }
    return null;
}
 
Example #8
Source File: SrcClassUtil.java    From manifold with Apache License 2.0 6 votes vote down vote up
private SrcType makeNestedType( Type type )
{
  String fqn = type.toString();
  Type enclosingType = type.getEnclosingType();
  SrcType srcType;
  if( enclosingType != null && !(enclosingType instanceof NoType) && fqn.length() > enclosingType.toString().length() )
  {
    String simpleName = fqn.substring( enclosingType.toString().length() + 1 );
    srcType = new SrcType( simpleName );
    srcType.setEnclosingType( makeNestedType( enclosingType ) );
  }
  else
  {
    srcType = new SrcType( fqn );
  }
  return srcType;
}
 
Example #9
Source File: SrcClassUtil.java    From manifold with Apache License 2.0 6 votes vote down vote up
private SrcType makeTypeVarType( Symbol.TypeVariableSymbol typeVar )
{
  StringBuilder sb = new StringBuilder( typeVar.type.toString() );
  Type lowerBound = typeVar.type.getLowerBound();
  if( lowerBound != null && !(lowerBound instanceof NullType) )
  {
    sb.append( " super " ).append( lowerBound.toString() );
  }
  else
  {
    Type upperBound = typeVar.type.getUpperBound();
    if( upperBound != null && !(upperBound instanceof NoType) && !upperBound.toString().equals( Object.class.getName() ) )
    {
      sb.append( " extends " ).append( upperBound.toString() );
    }
  }
  return new SrcType( sb.toString() );
}
 
Example #10
Source File: Factory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public NoType getNoType(TypeKind kind)
{
	switch (kind) {
	case NONE:
		return NoTypeImpl.NO_TYPE_NONE;
	case VOID:
		return NoTypeImpl.NO_TYPE_VOID;
	case PACKAGE:
		return NoTypeImpl.NO_TYPE_PACKAGE;
	default:
		throw new IllegalArgumentException();
	}
}
 
Example #11
Source File: PluggableProcessor.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private Set<String> getPluggableTypes(final TypeElement classElement)
{

    final Set<String> types = new HashSet<>();

    List<? extends TypeMirror> interfaces = classElement.getInterfaces();
    for(TypeMirror typeMirror : interfaces)
    {
        TypeElement interfaceElt = (TypeElement) processingEnv.getTypeUtils().asElement(typeMirror);
        if(interfaceElt.getQualifiedName().toString().equals("org.apache.qpid.server.plugin.Pluggable"))
        {
            types.add(classElement.getQualifiedName().toString());
        }
        else
        {
            types.addAll(getPluggableTypes(interfaceElt));
        }

    }
    TypeMirror superClass = classElement.getSuperclass();
    if(!(superClass instanceof NoType))
    {
        types.addAll(getPluggableTypes((TypeElement) processingEnv.getTypeUtils().asElement(superClass)));
    }

    return types;
}
 
Example #12
Source File: DataBindingAnnotationProcessor.java    From data-mediator with Apache License 2.0 5 votes vote down vote up
private TypeElement getSuperClassForDataBinding(TypeElement te) {
    TypeMirror superclass = te.getSuperclass();
    String superName = superclass.toString();
    if(superclass instanceof NoType){
        //no super.
    }else if(superName.startsWith("java.") || superName.startsWith("android.")){
        // no super too.
    }else{
        TypeElement newTe = new FieldData.TypeCompat(mContext.getTypes(), superclass).getElementAsType();
        DataBindingInfo info = mInfoMap.get(superName);
        if(info == null){
            //-------------- handle cross modules ------------
            //by class annotation
            if(hasDataBindingClassAnnotation(newTe)){
                return newTe;
            }
            //by field annotation
            List<VariableElement> elements = ElementFilter.fieldsIn(newTe.getEnclosedElements());
            if(elements.size() > 0){
                for(VariableElement ve: elements){
                    if(hasDataBindingFieldAnnotation(ve)){
                        return newTe;
                    }
                }
            }
            //may super's N class is using data-binding
            return getSuperClassForDataBinding(newTe);
        }else{
            //found
            return newTe;
        }
    }
    return null;
}
 
Example #13
Source File: MoreTypes.java    From auto with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean visitNoType(NoType noType, Void p) {
  if (noType.getKind().equals(TypeKind.VOID)) {
    return clazz.equals(Void.TYPE);
  }
  throw new IllegalArgumentException(noType + " cannot be represented as a Class<?>.");
}
 
Example #14
Source File: SignatureFactory.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitNoType(NoType t, SignatureVisitor visitor) {
  if (t.getKind() == TypeKind.VOID) {
    visitor.visitBaseType(descriptorFactory.getDescriptor(t).charAt(0));
    return null;
  }

  return defaultAction(t, visitor);
}
 
Example #15
Source File: StructuralTypeProxyGenerator.java    From manifold with Apache License 2.0 5 votes vote down vote up
private boolean hasPotentialMethod( Symbol.ClassSymbol rootClassSymbol, String name, int paramCount )
{
  if( rootClassSymbol == null || rootClassSymbol instanceof NoType )
  {
    return false;
  }

  for( Symbol member : IDynamicJdk.instance().getMembers( rootClassSymbol, e -> e.flatName().toString().equals( name ) ) )
  {
    Symbol.MethodSymbol methodSym = (Symbol.MethodSymbol)member;
    if( methodSym.getParameters().size() == paramCount )
    {
      return true;
    }
  }
  if( hasPotentialMethod( (Symbol.ClassSymbol)rootClassSymbol.getSuperclass().tsym, name, paramCount ) )
  {
    return true;
  }
  for( Type iface : rootClassSymbol.getInterfaces() )
  {
    if( hasPotentialMethod( (Symbol.ClassSymbol)iface.tsym, name, paramCount ) )
    {
      return true;
    }
  }
  return false;
}
 
Example #16
Source File: StandaloneTypeMirror.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  switch (kind) {
    case BOOLEAN:
    case BYTE:
    case SHORT:
    case INT:
    case LONG:
    case CHAR:
    case FLOAT:
    case DOUBLE:
      return v.visitPrimitive((PrimitiveType) this, p);
    case PACKAGE:
    case VOID:
    case NONE:
      return v.visitNoType((NoType) this, p);
    case NULL:
      return v.visitNull((NullType) this, p);
    case ARRAY:
      return v.visitArray((ArrayType) this, p);
    case DECLARED:
      return v.visitDeclared((DeclaredType) this, p);
    case ERROR:
      return v.visitError((ErrorType) this, p);
    case TYPEVAR:
      return v.visitTypeVariable((TypeVariable) this, p);
    case WILDCARD:
      return v.visitWildcard((WildcardType) this, p);
    case EXECUTABLE:
      return v.visitExecutable((ExecutableType) this, p);
    case OTHER:
      return v.visit(this, p);
    case UNION:
      return v.visitUnion((UnionType) this, p);
    case INTERSECTION:
      return v.visitIntersection((IntersectionType) this, p);
    default:
      throw new AssertionError(String.format("Unknown TypeKind: %s", kind));
  }
}
 
Example #17
Source File: JobProcessor.java    From cathode with Apache License 2.0 5 votes vote down vote up
private boolean isJob(Element element) {
  TypeMirror superType = ((TypeElement) element).getSuperclass();
  while (!(superType instanceof NoType)) {
    Element superTypeElement = ((DeclaredType) superType).asElement();
    if (JOB.equals(ClassName.get(superTypeElement.asType()))) {
      return true;
    }

    superType = ((TypeElement) superTypeElement).getSuperclass();
  }

  return false;
}
 
Example #18
Source File: TurbineTypes.java    From turbine with Apache License 2.0 5 votes vote down vote up
@Override
public NoType getNoType(TypeKind kind) {
  switch (kind) {
    case VOID:
      return (NoType) factory.asTypeMirror(Type.VOID);
    case NONE:
      return factory.noType();
    default:
      throw new IllegalArgumentException(kind.toString());
  }
}
 
Example #19
Source File: Util.java    From revapi with Apache License 2.0 5 votes vote down vote up
@Override
protected Void doVisitNoType(NoType t, StringBuilderAndState<TypeMirror> state) {
    switch (t.getKind()) {
    case VOID:
        state.bld.append("void");
        break;
    case PACKAGE:
        state.bld.append("package");
        break;
    default:
        break;
    }

    return null;
}
 
Example #20
Source File: MarkdownGenerator.java    From copybara with Apache License 2.0 5 votes vote down vote up
private List<? extends Element> findStarlarkMethods(TypeElement module) {
  TypeMirror superclass = module.getSuperclass();
  ImmutableList.Builder<Element> result = ImmutableList.builder();
  if (!(superclass instanceof NoType)) {
    Element element = processingEnv.getTypeUtils().asElement(superclass);
    if (element instanceof TypeElement) {
      result.addAll(findStarlarkMethods((TypeElement) element));
    }
  }
  result.addAll(module.getEnclosedElements().stream().filter(member -> {
    AnnotationHelper<StarlarkMethod> ann = annotationHelper(member, StarlarkMethod.class);
    return ann != null && member instanceof ExecutableElement && ann.ann.documented();
  }).collect(Collectors.toList()));
  return result.build();
}
 
Example #21
Source File: MoreTypes.java    From auto-parcel with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link NoType} if the {@link TypeMirror} represents an non-type such
 * as void, or package, etc. or throws an {@link IllegalArgumentException}.
 */
public static NoType asNoType(TypeMirror maybeNoType) {
    return maybeNoType.accept(new CastingTypeVisitor<NoType>() {
        @Override
        public NoType visitNoType(NoType noType, String p) {
            return noType;
        }
    }, "non-type");
}
 
Example #22
Source File: Util.java    From revapi with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitNoType(NoType t, StringBuilderAndState<TypeMirror> state) {
    switch (t.getKind()) {
    case VOID:
        state.bld.append("void");
        break;
    case PACKAGE:
        state.bld.append("package");
        break;
    default:
        break;
    }

    return null;
}
 
Example #23
Source File: Util.java    From revapi with Apache License 2.0 5 votes vote down vote up
@Override
public final T visitNoType(NoType t, StringBuilderAndState<S> st) {
    try {
        st.depth++;
        return doVisitNoType(t, st);
    } finally {
        st.depth--;
    }
}
 
Example #24
Source File: AdapterInfo.java    From AnnotatedAdapter with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if this AnnotatedAdapter has another AnnotatedAdapter as super class. Therfore the
 * binder interface must extends from this one.
 */
public AdapterInfo getAnnotatedAdapterSuperClass(Map<String, AdapterInfo> adaptersMap) {

  if (searchedForSuperAnnotatedAdapterClass) {
    return superAnnotatedAdapterClass;
  }

  searchedForSuperAnnotatedAdapterClass = true;

  // Search from bottom up along inheritance three for the fist Annotated adapter class we find
  TypeElement currentClass = adapterClass;

  while (currentClass != null) {
    TypeMirror currentMirror = currentClass.getSuperclass();

    if (currentMirror instanceof NoType || currentMirror.getKind() == TypeKind.NONE) {
      // java.lang.Object has been found, there is no other super class
      return null;
    }

    AdapterInfo superAdapter = adaptersMap.get(currentMirror.toString());
    if (superAdapter != null) {
      // "Cache" the found super class for performance reasons
      superAnnotatedAdapterClass = superAdapter;
      return superAnnotatedAdapterClass;
    }

    // Continue with the super class
    currentClass = elementUtils.getTypeElement(currentMirror.toString());
  }

  return null;
}
 
Example #25
Source File: MoreTypes.java    From auto with Apache License 2.0 4 votes vote down vote up
@Override
public Boolean visitNoType(NoType noType, Void p) {
  return noType.getKind().equals(TypeKind.VOID);
}
 
Example #26
Source File: TypeExtractor.java    From immutables with Apache License 2.0 4 votes vote down vote up
@Override
public Type visitNoType(NoType t, Type.Parameters p) {
  return Type.Primitive.VOID;
}
 
Example #27
Source File: GenericMirror.java    From FreeBuilder with Apache License 2.0 4 votes vote down vote up
@Override
public NoType getEnclosingType() {
  return NoTypes.NONE;
}
 
Example #28
Source File: MoreTypes.java    From auto with Apache License 2.0 4 votes vote down vote up
@Override
public NoType visitNoType(NoType type, Void ignore) {
  return type;
}
 
Example #29
Source File: MissingTypeAwareDelegatingTypes.java    From revapi with Apache License 2.0 4 votes vote down vote up
@Override
public NoType getNoType(TypeKind kind) {
    return types.getNoType(kind);
}
 
Example #30
Source File: TypeSpec.java    From javapoet with Apache License 2.0 4 votes vote down vote up
/**
 * Call this to always fully qualify any types that would conflict with possibly nested types of
 * this {@code typeElement}. For example - if the following type was passed in as the
 * typeElement:
 *
 * <pre><code>
 *   class Foo {
 *     class NestedTypeA {
 *
 *     }
 *     class NestedTypeB {
 *
 *     }
 *   }
 * </code></pre>
 *
 * <p>
 * Then this would add {@code "NestedTypeA"} and {@code "NestedTypeB"} as names that should
 * always be qualified via {@link #alwaysQualify(String...)}. This way they would avoid
 * possible import conflicts when this JavaFile is written.
 *
 * @param typeElement the {@link TypeElement} with nested types to avoid clashes with.
 * @return this builder instance.
 */
public Builder avoidClashesWithNestedClasses(TypeElement typeElement) {
  checkArgument(typeElement != null, "typeElement == null");
  for (TypeElement nestedType : ElementFilter.typesIn(typeElement.getEnclosedElements())) {
    alwaysQualify(nestedType.getSimpleName().toString());
  }
  TypeMirror superclass = typeElement.getSuperclass();
  if (!(superclass instanceof NoType) && superclass instanceof DeclaredType) {
    TypeElement superclassElement = (TypeElement) ((DeclaredType) superclass).asElement();
    avoidClashesWithNestedClasses(superclassElement);
  }
  for (TypeMirror superinterface : typeElement.getInterfaces()) {
    if (superinterface instanceof DeclaredType) {
      TypeElement superinterfaceElement
          = (TypeElement) ((DeclaredType) superinterface).asElement();
      avoidClashesWithNestedClasses(superinterfaceElement);
    }
  }
  return this;
}