Java Code Examples for javax.lang.model.element.TypeElement.getSuperclass()

The following are Jave code examples for showing how to use getSuperclass() of the javax.lang.model.element.TypeElement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: GitHub   File: PresenterInjectorRules.java   View Source Code Vote up 6 votes
private Collection<TypeMirror> getViewsType(TypeElement typeElement) {
	TypeMirror superclass = typeElement.asType();

	List<TypeMirror> result = new ArrayList<>();

	while (superclass.getKind() != TypeKind.NONE) {
		TypeElement superclassElement = (TypeElement) ((DeclaredType) superclass).asElement();
		Collection<? extends TypeMirror> interfaces = new HashSet<>(superclassElement.getInterfaces());
		for (TypeMirror typeMirror : interfaces) {
			if (typeMirror instanceof DeclaredType) {
				result.addAll(getViewsType((TypeElement) ((DeclaredType) typeMirror).asElement()));
			}
		}
		result.addAll(interfaces);
		result.add(superclass);

		superclass = superclassElement.getSuperclass();
	}

	return result;
}
 
Example 2
Project: GitHub   File: EasyMVPProcessor.java   View Source Code Vote up 6 votes
private String findViewTypeOfPresenter(TypeElement presenterElement) {
    TypeElement currentClass = presenterElement;
    while (currentClass != null) {
        if (currentClass.getSuperclass() instanceof DeclaredType) {
            List<? extends TypeMirror> superClassParameters =
                    ((DeclaredType) currentClass.getSuperclass()).getTypeArguments();

            if (superClassParameters.size() == 1) {
                String type = superClassParameters.get(0).toString();
                if (!"V".equals(type)) return type;
            }
        }
        currentClass = getSuperClass(currentClass);
    }
    return "";
}
 
Example 3
Project: auto-nav   File: NavProcessor.java   View Source Code Vote up 6 votes
private ActivityIntentModel getActivityModel(TypeElement typeElement) {
    ActivityIntentModel activityModel = new ActivityIntentModel(
            elementsUtil.getPackageOf(typeElement).getQualifiedName().toString(),
            typeElement.getSimpleName().toString());

    messager.printMessage(Diagnostic.Kind.NOTE, "\ntype:" + typeElement.getQualifiedName());

    activityModel.addParamModels(getParamModels(typeElement));

    TypeMirror superType = typeElement.getSuperclass();

    while (superType != null && !Object.class.getName().equals(superType.toString())) {
        messager.printMessage(Diagnostic.Kind.NOTE, "\n   superType:" + superType.toString());
        Element element = typesUtil.asElement(superType);
        if (element != null && element instanceof TypeElement) {
            TypeElement superE = (TypeElement) element;
            activityModel.addParamModels(getParamModels(superE));

            superType = superE.getSuperclass();
        } else {
            superType = null;
        }
    }

    return activityModel;
}
 
Example 4
Project: incubator-netbeans   File: FindSubtypesVisitor.java   View Source Code Vote up 6 votes
@Override
public Tree visitClass(ClassTree node, Element elementToFind) {
    if (workingCopy.getTreeUtilities().isSynthetic(getCurrentPath())) {
        return super.visitClass(node, elementToFind);
    }
    Trees trees = workingCopy.getTrees();
    Types types = workingCopy.getTypes();
    TypeMirror type2 = elementToFind.asType();
    type2 = types.erasure(type2);
    
    if (recursive) {
        TypeMirror type1 = trees.getTypeMirror(getCurrentPath());
        if (type1 != null) {
            type1 = types.erasure(type1);
            if (isSubtype(type1, type2)) {
                addUsage(getCurrentPath());
            }
        }
    } else {
        TypeElement el = (TypeElement) workingCopy.getTrees().getElement(getCurrentPath());
        if (el != null && el.getSuperclass()!=null && types.isSameType(types.erasure(el.getSuperclass()), type2) || containsType(el.getInterfaces(), type2)) {
            addUsage(getCurrentPath());
        }
    }
    return super.visitClass(node, elementToFind);
}
 
Example 5
Project: incubator-netbeans   File: ExportNonAccessibleElement.java   View Source Code Vote up 6 votes
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 6
Project: openjdk-jdk10   File: SerializedFormWriterImpl.java   View Source Code Vote up 6 votes
/**
 * Get the serializable class heading.
 *
 * @param typeElement the class being processed
 * @return a content tree for the class header
 */
public Content getClassHeader(TypeElement typeElement) {
    Content classLink = (isVisibleClass(typeElement))
            ? getLink(new LinkInfoImpl(configuration, LinkInfoImpl.Kind.DEFAULT, typeElement)
                    .label(configuration.getClassName(typeElement)))
            : new StringContent(utils.getFullyQualifiedName(typeElement));
    Content li = HtmlTree.LI(HtmlStyle.blockList, getMarkerAnchor(
            utils.getFullyQualifiedName(typeElement)));
    Content superClassLink = typeElement.getSuperclass() != null
            ? getLink(new LinkInfoImpl(configuration, LinkInfoImpl.Kind.SERIALIZED_FORM,
                    typeElement.getSuperclass()))
            : null;

    //Print the heading.
    Content className = superClassLink == null ?
        configuration.getContent(
        "doclet.Class_0_implements_serializable", classLink) :
        configuration.getContent(
        "doclet.Class_0_extends_implements_serializable", classLink,
        superClassLink);
    li.addContent(HtmlTree.HEADING(HtmlConstants.SERIALIZED_MEMBER_HEADING,
            className));
    return li;
}
 
Example 7
Project: arez   File: ProcessorUtil.java   View Source Code Vote up 6 votes
private static void enumerateMethods( @Nonnull final TypeElement scope,
                                      @Nonnull final Types typeUtils,
                                      @Nonnull final TypeElement element,
                                      @Nonnull final Map<String, ExecutableElement> methods )
{
  final TypeMirror superclass = element.getSuperclass();
  if ( TypeKind.NONE != superclass.getKind() )
  {
    enumerateMethods( scope, typeUtils, (TypeElement) ( (DeclaredType) superclass ).asElement(), methods );
  }
  for ( final TypeMirror interfaceType : element.getInterfaces() )
  {
    final TypeElement interfaceElement = (TypeElement) ( (DeclaredType) interfaceType ).asElement();
    enumerateMethods( scope, typeUtils, interfaceElement, methods );
  }
  for ( final Element member : element.getEnclosedElements() )
  {
    if ( member.getKind() == ElementKind.METHOD )
    {
      final ExecutableType methodType =
        (ExecutableType) typeUtils.asMemberOf( (DeclaredType) scope.asType(), member );
      methods.put( member.getSimpleName() + methodType.toString(), (ExecutableElement) member );
    }
  }
}
 
Example 8
Project: react4j   File: ProcessorUtil.java   View Source Code Vote up 6 votes
private static void enumerateMethods( @Nonnull final TypeElement scope,
                                      @Nonnull final Types typeUtils,
                                      @Nonnull final TypeElement element,
                                      @Nonnull final Map<String, ExecutableElement> methods )
{
  final TypeMirror superclass = element.getSuperclass();
  if ( TypeKind.NONE != superclass.getKind() )
  {
    enumerateMethods( scope, typeUtils, (TypeElement) ( (DeclaredType) superclass ).asElement(), methods );
  }
  for ( final TypeMirror interfaceType : element.getInterfaces() )
  {
    final TypeElement interfaceElement = (TypeElement) ( (DeclaredType) interfaceType ).asElement();
    enumerateMethods( scope, typeUtils, interfaceElement, methods );
  }
  for ( final Element member : element.getEnclosedElements() )
  {
    if ( member.getKind() == ElementKind.METHOD )
    {
      final ExecutableType methodType =
        (ExecutableType) typeUtils.asMemberOf( (DeclaredType) scope.asType(), member );
      methods.put( member.getSimpleName() + methodType.toString(), (ExecutableElement) member );
    }
  }
}
 
Example 9
Project: GitHub   File: ButterKnifeProcessor.java   View Source Code Vote up 5 votes
static boolean isSubtypeOfType(TypeMirror typeMirror, String otherType) {
  if (isTypeEqual(typeMirror, otherType)) {
    return true;
  }
  if (typeMirror.getKind() != TypeKind.DECLARED) {
    return false;
  }
  DeclaredType declaredType = (DeclaredType) typeMirror;
  List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
  if (typeArguments.size() > 0) {
    StringBuilder typeString = new StringBuilder(declaredType.asElement().toString());
    typeString.append('<');
    for (int i = 0; i < typeArguments.size(); i++) {
      if (i > 0) {
        typeString.append(',');
      }
      typeString.append('?');
    }
    typeString.append('>');
    if (typeString.toString().equals(otherType)) {
      return true;
    }
  }
  Element element = declaredType.asElement();
  if (!(element instanceof TypeElement)) {
    return false;
  }
  TypeElement typeElement = (TypeElement) element;
  TypeMirror superType = typeElement.getSuperclass();
  if (isSubtypeOfType(superType, otherType)) {
    return true;
  }
  for (TypeMirror interfaceType : typeElement.getInterfaces()) {
    if (isSubtypeOfType(interfaceType, otherType)) {
      return true;
    }
  }
  return false;
}
 
Example 10
Project: GitHub   File: TypeHierarchyCollector.java   View Source Code Vote up 5 votes
private void collectHierarchyMirrors(TypeMirror typeMirror, String stringRepresentation) {
  if (typeMirror.getKind() != TypeKind.DECLARED
      || typeMirror.toString().equals(Object.class.getName())) {
    return;
  }

  DeclaredType declaredType = toDeclaredType(typeMirror);
  TypeElement e = toTypeElement(declaredType);
  TypevarContext context = new TypevarContext(e, stringRepresentation);

  collectInterfacesMirrors(declaredType, context);

  TypeMirror superclassMirror = e.getSuperclass();
  if (superclassMirror.getKind() != TypeKind.NONE) {
    DeclaredType superclass = toDeclaredType(superclassMirror);
    String stringified = stringify(superclass, context);

    if (!stringified.equals(Object.class.getName())) {
      extendedClasses.add(toTypeElement(superclass));
      extendedClassNames.add(stringified);
      collectHierarchyMirrors(superclass, stringified);
    }
  }

  for (TypeMirror m : e.getInterfaces()) {
    collectUnresolvedInterface(m, context);
    collectInterfacesMirrors(m, context);
  }
}
 
Example 11
Project: android-auto-mapper   File: AutoMappperProcessor.java   View Source Code Vote up 5 votes
private boolean ancestorIsAutoParcel(TypeElement type) {
    while (true) {
        TypeMirror parentMirror = type.getSuperclass();
        if (parentMirror.getKind() == TypeKind.NONE) {
            return false;
        }
        TypeElement parentElement = (TypeElement) mTypeUtils.asElement(parentMirror);
        if (MoreElements.isAnnotationPresent(parentElement, AutoMapper.class)) {
            return true;
        }
        type = parentElement;
    }
}
 
Example 12
Project: incubator-netbeans   File: Nodes.java   View Source Code Vote up 5 votes
private static Node superTypeHierarchy(
        @NonNull final DeclaredType type,
        @NonNull final ClasspathInfo cpInfo,
        @NonNull final HierarchyFilters filters,
        final int order) {
    final TypeElement element = (TypeElement)type.asElement();
    final TypeMirror superClass = element.getSuperclass();
    final List<? extends TypeMirror> interfaces = element.getInterfaces();
    final List<Node> childNodes = new ArrayList<Node>(interfaces.size()+1);
    int childOrder = 0;
    if (superClass.getKind() != TypeKind.NONE) {
        childNodes.add(superTypeHierarchy((DeclaredType)superClass, cpInfo, filters, childOrder));
    }
    for (TypeMirror superInterface : interfaces) {
        childOrder++;
        childNodes.add(superTypeHierarchy((DeclaredType)superInterface, cpInfo, filters, childOrder));
    }
    final Children cld;
    if (childNodes.isEmpty()) {
        cld = Children.LEAF;
    } else {
        cld = new SuperTypeChildren(filters);
        cld.add(childNodes.toArray(new Node[childNodes.size()]));
    }
    return new TypeNode(
        cld,
        new Description(
            cpInfo,
            ElementHandle.create(element),
            order),
        filters,
        globalActions(filters));
    
}
 
Example 13
Project: openjdk-jdk10   File: Utils.java   View Source Code Vote up 5 votes
public TypeElement getSuperClass(TypeElement te) {
    if (isInterface(te) || isAnnotationType(te) ||
            te.asType().equals(getObjectType())) {
        return null;
    }
    TypeMirror superclass = te.getSuperclass();
    if (isNoType(superclass) && isClass(te)) {
        superclass = getObjectType();
    }
    return asTypeElement(superclass);
}
 
Example 14
Project: incubator-netbeans   File: AddConstructor.java   View Source Code Vote up 5 votes
@Override
public List<Fix> run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
    if (!TreeUtilities.CLASS_TREE_KINDS.contains(treePath.getLeaf().getKind())) return Collections.emptyList();
    
    Element el = info.getTrees().getElement(treePath);
    
    if (el == null || !el.getKind().isClass()) return Collections.emptyList();
    
    TypeElement clazz = (TypeElement) el;
    TypeMirror superType = clazz.getSuperclass();
    
    if (superType.getKind() != TypeKind.DECLARED) return Collections.emptyList();
    
    TypeElement superClazz = (TypeElement) info.getTypes().asElement(superType);
    DeclaredType targetType = (DeclaredType) clazz.asType();
    Scope classScope = info.getTrees().getScope(treePath);
    List<Fix> result = new ArrayList<Fix>();
    
    for (ExecutableElement constr : ElementFilter.constructorsIn(superClazz.getEnclosedElements())) {
        if (!info.getTrees().isAccessible(classScope, constr, (DeclaredType) superType)) continue;
        
        StringBuilder name = new StringBuilder();
        
        name.append(clazz.getSimpleName()).append("(");
        
        ExecutableType target = (ExecutableType) info.getTypes().asMemberOf(targetType, constr);
        boolean firstParam = true;
        
        for (TypeMirror p : target.getParameterTypes()) {
            if (!firstParam) name.append(", ");
            firstParam = false;
            name.append(info.getTypeUtilities().getTypeName(p));
        }
        
        name.append(")");
        result.add(new FixImpl(info, treePath, constr, StringEscapeUtils.escapeHtml(name.toString())).toEditorFix());
    }
    
    return result;
}
 
Example 15
Project: incubator-netbeans   File: AnnotationHelper.java   View Source Code Vote up 5 votes
public TypeElement getSuperclass(TypeElement type) {
    TypeMirror supertype = type.getSuperclass();
    if (TypeKind.DECLARED.equals(supertype.getKind())) {
        Element element = ((DeclaredType)supertype).asElement();
        if (ElementKind.CLASS.equals(element.getKind())) {
            TypeElement superclass = (TypeElement)element;
            if (!superclass.getQualifiedName().contentEquals(
                    Object.class.getCanonicalName())) { 
                return superclass;
            }
        }
    }
    return null;
}
 
Example 16
Project: prefpin   File: PrefPinProcessor.java   View Source Code Vote up 5 votes
private boolean isSubtypeOfType(TypeMirror typeMirror, String otherType) {
  if (isTypeEqual(typeMirror, otherType)) {
    return true;
  }
  if (typeMirror.getKind() != TypeKind.DECLARED) {
    return false;
  }
  DeclaredType declaredType = (DeclaredType) typeMirror;
  List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
  if (typeArguments.size() > 0) {
    StringBuilder typeString = new StringBuilder(declaredType.asElement().toString());
    typeString.append('<');
    for (int i = 0; i < typeArguments.size(); i++) {
      if (i > 0) {
        typeString.append(',');
      }
      typeString.append('?');
    }
    typeString.append('>');
    if (typeString.toString().equals(otherType)) {
      return true;
    }
  }
  Element element = declaredType.asElement();
  if (!(element instanceof TypeElement)) {
    return false;
  }
  TypeElement typeElement = (TypeElement) element;
  TypeMirror superType = typeElement.getSuperclass();
  if (isSubtypeOfType(superType, otherType)) {
    return true;
  }
  for (TypeMirror interfaceType : typeElement.getInterfaces()) {
    if (isSubtypeOfType(interfaceType, otherType)) {
      return true;
    }
  }
  return false;
}
 
Example 17
Project: openjdk-jdk10   File: Utils.java   View Source Code Vote up 4 votes
public TypeMirror getSuperType(TypeElement te) {
    TypeMirror t = te.getSuperclass();
    return getType(t);
}
 
Example 18
Project: GitHub   File: PresenterInjectorRules.java   View Source Code Vote up 4 votes
private String getViewClassFromGeneric(TypeElement typeElement, DeclaredType declaredType) {
	TypeMirror superclass = declaredType;
	Map<TypeParameterElement, TypeMirror> mTypedMap = Collections.emptyMap();

	if (!typeElement.getTypeParameters().isEmpty()) {
		mTypedMap = getChildInstanceOfClassFromGeneric(typeElement, MvpView.class);
	}


	Map<String, String> parentTypes = Collections.emptyMap();
	List<? extends TypeMirror> totalTypeArguments = new ArrayList<>(((DeclaredType) superclass).getTypeArguments());
	while (superclass.getKind() != TypeKind.NONE) {
		TypeElement superclassElement = (TypeElement) ((DeclaredType) superclass).asElement();
		List<? extends TypeMirror> typeArguments = ((DeclaredType) superclass).getTypeArguments();
		totalTypeArguments.retainAll(typeArguments);
		final List<? extends TypeParameterElement> typeParameters = superclassElement.getTypeParameters();

		Map<String, String> types = new HashMap<>();
		for (int i = 0; i < typeArguments.size(); i++) {
			types.put(typeParameters.get(i).toString(), fillGenerics(parentTypes, typeArguments.get(i)));
		}

		if (superclassElement.toString().equals(MvpPresenter.class.getCanonicalName())) {
			if (!typeArguments.isEmpty()) {
				TypeMirror typeMirror = typeArguments.get(0);
				if (typeMirror instanceof TypeVariable) {
					Element key = ((TypeVariable) typeMirror).asElement();

					for (Map.Entry<TypeParameterElement, TypeMirror> entry : mTypedMap.entrySet()) {
						if (entry.getKey().toString().equals(key.toString())) {
							return Util.getFullClassName(entry.getValue());
						}
					}
				}
			}

			if (typeArguments.isEmpty() && typeParameters.isEmpty()) {
				return ((DeclaredType) superclass).asElement().getSimpleName().toString();
			}
			// MvpPresenter is typed only on View class
			return fillGenerics(parentTypes, typeArguments);
		}

		parentTypes = types;

		superclass = superclassElement.getSuperclass();
	}

	return "";
}
 
Example 19
Project: GitHub   File: EasyMVPProcessor.java   View Source Code Vote up 4 votes
private TypeElement getSuperClass(TypeElement typeElement) {
    if (!(typeElement.getSuperclass() instanceof DeclaredType)) return null;
    DeclaredType declaredAncestor = (DeclaredType) typeElement.getSuperclass();
    return (TypeElement) declaredAncestor.asElement();
}
 
Example 20
Project: openjdk-jdk10   File: GraphNodeVerifier.java   View Source Code Vote up 4 votes
private TypeElement getSuperType(TypeElement element) {
    if (element.getSuperclass() != null) {
        return (TypeElement) env.getProcessingEnv().getTypeUtils().asElement(element.getSuperclass());
    }
    return null;
}