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

The following examples show how to use javax.lang.model.element.TypeElement#getInterfaces() . 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: EntityAnnotationProcessor.java    From droitatedDB with Apache License 2.0 6 votes vote down vote up
private String getHookName(final RoundEnvironment roundEnv, Class<? extends Annotation> hookAnnotation, Class<?> hookInterface) {
	Set<? extends Element> annotated = roundEnv.getElementsAnnotatedWith(hookAnnotation);
	if (annotated.size() == 0) {
		return null;
	}
	if (annotated.size() > 1) {
           messager.printMessage(Kind.ERROR,
                   "Only one " + hookAnnotation.getCanonicalName() + " hook is allowed with the project", annotated.iterator().next());
		return null;
	}
	Element updateElement = annotated.iterator().next();
	TypeElement typeElement = updateElement.accept(new TypeResolvingVisitor(), null);
	boolean implementsDbUpdate = false;
	for (TypeMirror typeMirror : typeElement.getInterfaces()) {
		if (typeMirror.toString().equals(hookInterface.getCanonicalName())) {
			implementsDbUpdate = true;
		}
	}
	if (!implementsDbUpdate) {
           messager.printMessage(Kind.ERROR,
                   "The " + hookAnnotation + " hook has to implement the " + hookInterface.getCanonicalName() + " interface", updateElement);
		return null;
	}
	return typeElement.getQualifiedName().toString();
}
 
Example 2
Source File: TypeModeler.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static TypeElement getDeclaringClassMethod(TypeElement theClass, String methodName, TypeMirror[] args) {

        TypeElement retClass = null;
        if (theClass.getKind().equals(ElementKind.CLASS)) {
            TypeMirror superClass = theClass.getSuperclass();
            if (!superClass.getKind().equals(TypeKind.NONE))
                retClass = getDeclaringClassMethod(superClass, methodName, args);
        }
        if (retClass == null) {
            for (TypeMirror interfaceType : theClass.getInterfaces()) {
                retClass = getDeclaringClassMethod(interfaceType, methodName, args);
            }
        }
        if (retClass == null) {
            Collection<? extends ExecutableElement> methods = ElementFilter.methodsIn(theClass.getEnclosedElements());
            for (ExecutableElement method : methods) {
                if (method.getSimpleName().toString().equals(methodName)) {
                    retClass = theClass;
                    break;
                }
            }
        }
        return retClass;
    }
 
Example 3
Source File: OutInterfaceManager.java    From data-mediator with Apache License 2.0 6 votes vote down vote up
/**
 * get the super interface flags for parent
 * @param te the current type element
 * @param pp the log printer
 * @return the flags.
 */
public static int getSuperInterfaceFlagForParent(TypeElement te,
                                                 Elements mElements, Types types, ProcessorPrinter pp){
    final String tag = te.getSimpleName().toString();
    List<? extends TypeMirror> interfaces = te.getInterfaces();
    for(TypeMirror tm: interfaces) {
        // pp.note(TAG, "getSuperInteraceFlagForParent_" + tag, "TypeMirror : " + tm);
        FieldData.TypeCompat tc = new FieldData.TypeCompat(types, tm);
        tc.replaceIfNeed(mElements, pp);
        pp.note(TAG, "getSuperInteraceFlagForParent_" + tag, "TypeMirror : " + tm
                + " , hasAnnotationFields = " + tc.hasAnnotationFields());
        if(tc.hasAnnotationFields()){
            //we want.
            int sum = 0;
            final Set<Integer> flags = getSuperInterfaceFlags(tag,
                    tc.getElementAsType(), types, pp);
            for(Integer val : flags){
                sum += val;
            }
            return sum;
        }
    }
    return 0;
}
 
Example 4
Source File: WebServiceVisitor.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
protected boolean classImplementsSei(TypeElement classElement, TypeElement interfaceElement) {
    for (TypeMirror interfaceType : classElement.getInterfaces()) {
        if (((DeclaredType) interfaceType).asElement().equals(interfaceElement))
            return true;
    }
    List<ExecutableElement> classMethods = getClassMethods(classElement);
    boolean implementsMethod;
    for (ExecutableElement interfaceMethod : ElementFilter.methodsIn(interfaceElement.getEnclosedElements())) {
        implementsMethod = false;
        for (ExecutableElement classMethod : classMethods) {
            if (sameMethod(interfaceMethod, classMethod)) {
                implementsMethod = true;
                classMethods.remove(classMethod);
                break;
            }
        }
        if (!implementsMethod) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_NOT_IMPLEMENTED(interfaceElement.getSimpleName(), classElement.getSimpleName(), interfaceMethod), interfaceMethod);
            return false;
        }
    }
    return true;
}
 
Example 5
Source File: MultiModuleSuperFieldDelegate.java    From data-mediator with Apache License 2.0 6 votes vote down vote up
@Override
public Set<FieldData> getDependFields(TypeElement te) {
    Set<FieldData> list = new HashSet<>();

    List<? extends AnnotationMirror> mirrors = te.getAnnotationMirrors();
    AnnotationMirror expect = null;
    for(AnnotationMirror am : mirrors){
        DeclaredType type = am.getAnnotationType();
        if(type.toString().equals(Fields.class.getName())){
            expect = am;
            break;
        }
    }
    if(expect != null){
        ElementHelper.parseFields(elements, types, expect, list, pp);
    }
    //a depend b, b depend c ,,, etc.
    List<? extends TypeMirror> superInterfaces = te.getInterfaces();
    for(TypeMirror tm : superInterfaces){
        final TypeElement newTe = (TypeElement) ((DeclaredType) tm).asElement();
        list.addAll(getDependFields(newTe)); //recursion
    }
    return list;
}
 
Example 6
Source File: PrintingProcessor.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private void printInterfaces(TypeElement e) {
    ElementKind kind = e.getKind();

    if(kind != ANNOTATION_TYPE) {
        List<? extends TypeMirror> interfaces = e.getInterfaces();
        if (interfaces.size() > 0) {
            writer.print((kind.isClass() ? " implements" : " extends"));

            boolean first = true;
            for(TypeMirror interf: interfaces) {
                if (!first)
                    writer.print(",");
                writer.print(" ");
                writer.print(interf.toString());
                first = false;
            }
        }
    }
}
 
Example 7
Source File: Visitor.java    From helidon-build-tools with Apache License 2.0 6 votes vote down vote up
private boolean implementsCommandExecution(TypeElement type) {
    for (TypeMirror iface : type.getInterfaces()) {
        TypeElement ifaceTypeElt = (TypeElement) env.getTypeUtils().asElement(iface);
        if (CommandExecution.class.getName().equals(ifaceTypeElt.getQualifiedName().toString())) {
            return true;
        }
    }
    TypeMirror parent = type.getSuperclass();
    if (parent != null) {
        return implementsCommandExecution((TypeElement) env.getTypeUtils().asElement(parent));
    }
    env.getMessager().printMessage(Diagnostic.Kind.ERROR,
            String.format("%s does not implement %s", type, CommandExecution.class.getSimpleName()),
            type);
    return false;
}
 
Example 8
Source File: WebServiceVisitor.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
protected boolean classImplementsSei(TypeElement classElement, TypeElement interfaceElement) {
    for (TypeMirror interfaceType : classElement.getInterfaces()) {
        if (((DeclaredType) interfaceType).asElement().equals(interfaceElement))
            return true;
    }
    List<ExecutableElement> classMethods = getClassMethods(classElement);
    boolean implementsMethod;
    for (ExecutableElement interfaceMethod : ElementFilter.methodsIn(interfaceElement.getEnclosedElements())) {
        implementsMethod = false;
        for (ExecutableElement classMethod : classMethods) {
            if (sameMethod(interfaceMethod, classMethod)) {
                implementsMethod = true;
                classMethods.remove(classMethod);
                break;
            }
        }
        if (!implementsMethod) {
            builder.processError(WebserviceapMessages.WEBSERVICEAP_METHOD_NOT_IMPLEMENTED(interfaceElement.getSimpleName(), classElement.getSimpleName(), interfaceMethod), interfaceMethod);
            return false;
        }
    }
    return true;
}
 
Example 9
Source File: ElementUtils.java    From spring-init with Apache License 2.0 6 votes vote down vote up
public boolean implementsInterface(TypeElement te, ClassName intface) {
	if (te == null) {
		return false;
	}
	if (ClassName.get(te).equals(intface)) {
		return true;
	}
	for (TypeMirror t : te.getInterfaces()) {
		boolean b = implementsInterface((TypeElement) asElement(t), intface);
		if (b) {
			return true;
		}
	}
	TypeMirror superclass = te.getSuperclass();
	if (superclass == null) {
		return false;
	}
	return implementsInterface((TypeElement) asElement(superclass), intface);
}
 
Example 10
Source File: JavaOperationsImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@NonNull
@Override
public final Collection<? extends String> getInterfaces(@NonNull final String cls) throws QueryException {
    final TypeElement te = findClass(cls);
    if (te == null) {
        return null;
    }
    final List<? extends TypeMirror> interfaceTypes = te.getInterfaces();
    final List<String> result = new ArrayList<String>(interfaceTypes.size());
    for (TypeMirror tm : interfaceTypes) {
        if (tm.getKind() == TypeKind.DECLARED) {
            result.add(
                ((TypeElement)((DeclaredType)tm).asElement()).getQualifiedName().toString());
        }
    }
    return Collections.unmodifiableCollection(result);
}
 
Example 11
Source File: HtmlDocletWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add method information.
 *
 * @param method the method to be documented
 * @param dl the content tree to which the method information will be added
 */
private void addMethodInfo(ExecutableElement method, Content dl) {
    TypeElement enclosing = utils.getEnclosingTypeElement(method);
    List<? extends TypeMirror> intfacs = enclosing.getInterfaces();
    ExecutableElement overriddenMethod = utils.overriddenMethod(method);
    // Check whether there is any implementation or overridden info to be
    // printed. If no overridden or implementation info needs to be
    // printed, do not print this section.
    if ((!intfacs.isEmpty()
            && new ImplementedMethods(method, this.configuration).build().isEmpty() == false)
            || overriddenMethod != null) {
        MethodWriterImpl.addImplementsInfo(this, method, dl);
        if (overriddenMethod != null) {
            MethodWriterImpl.addOverridden(this,
                    utils.overriddenType(method),
                    overriddenMethod,
                    dl);
        }
    }
}
 
Example 12
Source File: ButterKnifeProcessor.java    From butterknife with Apache License 2.0 5 votes vote down vote up
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 13
Source File: TypeModeler.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static Collection<DeclaredType> collectInterfaces(TypeElement type) {
    @SuppressWarnings({"unchecked"})
    Collection<DeclaredType> interfaces = (Collection<DeclaredType>) type.getInterfaces();
    for (TypeMirror interfaceType : type.getInterfaces()) {
        interfaces.addAll(collectInterfaces(getDeclaration(interfaceType)));
    }
    return interfaces;
}
 
Example 14
Source File: TypeModeler.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static Collection<DeclaredType> collectInterfaces(TypeElement type) {
    @SuppressWarnings({"unchecked"})
    Collection<DeclaredType> interfaces = (Collection<DeclaredType>) type.getInterfaces();
    for (TypeMirror interfaceType : type.getInterfaces()) {
        interfaces.addAll(collectInterfaces(getDeclaration(interfaceType)));
    }
    return interfaces;
}
 
Example 15
Source File: Overrides.java    From auto with Apache License 2.0 5 votes vote down vote up
private ImmutableList<TypeElement> superinterfaces(TypeElement type) {
  ImmutableList.Builder<TypeElement> types = ImmutableList.builder();
  for (TypeMirror sup : type.getInterfaces()) {
    types.add(MoreElements.asType(typeUtils.asElement(sup)));
  }
  return types.build();
}
 
Example 16
Source File: Processor.java    From vault with Apache License 2.0 5 votes vote down vote up
private boolean isSubtypeOfType(TypeMirror typeMirror, String otherType) {
  if (otherType.equals(typeMirror.toString())) {
    return true;
  }
  if (!(typeMirror instanceof DeclaredType)) {
    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
Source File: SerializableClass.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean extendsFromSerializable(TypeElement subject) {
    for (TypeMirror iface : subject.getInterfaces()) {
        if ("java.io.Serializable".equals(iface.toString())) { //NOI18N
            return true;
        } else if (iface instanceof DeclaredType) {
            DeclaredType iType = (DeclaredType) iface;
            if (extendsFromSerializable((TypeElement) iType.asElement())) {
                return true;
            }
        }
    }
    return false;
}
 
Example 18
Source File: SignatureGenerator.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * ClassSignature ::=
 *   OptFormalTypeParameters SuperclassSignature {SuperinterfaceSignature}.
 */
private void genClassSignature(TypeElement type, StringBuilder sb) {
  genOptFormalTypeParameters(type.getTypeParameters(), sb);
  // JDT returns null for an interface's superclass, but signatures expect Object.
  if (type.getKind().isInterface()) {
    sb.append(JAVA_OBJECT_SIGNATURE);
  } else {
    genTypeSignature(type.getSuperclass(), sb);
  }
  for (TypeMirror intrface : type.getInterfaces()) {
    genTypeSignature(intrface, sb);
  }
}
 
Example 19
Source File: Processor.java    From reflection-no-reflection with Apache License 2.0 4 votes vote down vote up
private Class createClass(TypeMirror typeMirror, int level) {
    Class result;
    String className = null;
    boolean isPrimitive = false;
    boolean isArray = false;
    boolean isInterface = false;
    Class component = null;
    Class superClass = null;
    GenericDeclarationImpl declaration = null;
    Class[] superInterfaces = null;

    if (typeMirror instanceof DeclaredType) {
        DeclaredType declaredType = (DeclaredType) typeMirror;
        className = ((TypeElement) declaredType.asElement()).getQualifiedName().toString();
        if (!declaredType.getTypeArguments().isEmpty()) {
            declaration = new GenericDeclarationImpl();
            TypeVariable[] typesVariables = new TypeVariable[declaredType.getTypeArguments().size()];
            int index = 0;
            for (TypeMirror mirror : declaredType.getTypeArguments()) {
                TypeVariableImpl typeVariableImpl = new TypeVariableImpl();
                typesVariables[index] = typeVariableImpl;
                typeVariableImpl.setName(mirror.toString());
                index++;
            }

            declaration.setTypeParameters(typesVariables);
        }
        isInterface = ((com.sun.tools.javac.code.Type) typeMirror).isInterface();
        final int indexOfChevron = className.indexOf('<');
        if (indexOfChevron != -1) {
            className = className.substring(0, indexOfChevron);
        }
        TypeElement typeElement = (TypeElement) declaredType.asElement();
        if (level + 1 <= maxLevel) {
            TypeMirror superclass = typeElement.getSuperclass();
            superClass = createClass(superclass, level + 1);
        }
        if (level + 1 <= maxLevel) {
            superInterfaces = new Class[typeElement.getInterfaces().size()];
            int indexInterface = 0;
            for (TypeMirror superInterface : typeElement.getInterfaces()) {
                superInterfaces[indexInterface++] = createClass(superInterface, level +1);
            }
        }
        if (level + 1 <= maxLevel) {
            final List<? extends Element> enclosedElements = ((TypeElement) declaredType.asElement()).getEnclosedElements();
            for (Element enclosedElement : enclosedElements) {
                mapElementToReflection(enclosedElement, level + 1);
            }
        }
    } else if (typeMirror instanceof ArrayType) {
        //warning, this must come before Primitive as arrays are also primitives (here)
        isArray = true;
        className = ((ArrayType) typeMirror).getComponentType().toString() + "[]";
        component = createClass(((ArrayType) typeMirror).getComponentType(), level);
    } else if (typeMirror instanceof PrimitiveType) {
        isPrimitive = true;
        className = typeMirror.toString();
    }

    result = Class.forNameSafe(className, level);

    if (superClass!=null) {
        result.setSuperclass(superClass);
    }

    if (superInterfaces!=null) {
        result.setInterfaces(superInterfaces);
    }

    if (isArray) {
        result.setIsArray(true);
    }
    if (isPrimitive) {
        result.setIsPrimitive(true);
    }
    if (component != null) {
        result.setComponentType(component);
    }

    if (declaration != null) {
        result.setGenericInfo(declaration);
    }
    if (isInterface) {
        result.setIsInterface(true);
    }
    return result;
}
 
Example 20
Source File: ElementScanningTask.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private String createHtmlHeader(CompilationInfo info, TypeElement e, boolean isDeprecated, boolean isInherited, boolean fqn) {

        StringBuilder sb = new StringBuilder();            
        if ( isDeprecated ) {
            sb.append("<s>"); // NOI18N
        }
        if( isInherited ) {
            sb.append( "<font color=" + ui.getInheritedColor() + ">" ); // NOI18N
        }
        sb.append(Utils.escape(
            fqn?
            e.getQualifiedName().toString():
            e.getSimpleName().toString()));
        if ( isDeprecated ) {
            sb.append("</s>"); // NOI18N
        }
        // sb.append(print(info, e.asType()));
        List<? extends TypeParameterElement> typeParams = e.getTypeParameters();

        //System.out.println("Element " + e + "type params" + typeParams.size() );

        if ( typeParams != null && !typeParams.isEmpty() ) {
            sb.append("&lt;"); // NOI18N

            for( Iterator<? extends TypeParameterElement> it = typeParams.iterator(); it.hasNext(); ) {
                TypeParameterElement tp = it.next();
                sb.append( Utils.escape(tp.getSimpleName().toString()) );                    
                try { // XXX Verry ugly -> file a bug against Javac?
                    List<? extends TypeMirror> bounds = tp.getBounds();
                    //System.out.println( tp.getSimpleName() + "   bounds size " + bounds.size() );
                    if ( !bounds.isEmpty() ) {
                        sb.append(printBounds(info, bounds, fqn));
                    }
                }
                catch ( NullPointerException npe ) {
                    System.err.println("El " + e );
                    npe.printStackTrace();
                }                    
                if ( it.hasNext() ) {
                    sb.append(", "); // NOI18N
                }
            }

            sb.append("&gt;"); // NOI18N
        }

        // Add superclass and implemented interfaces

        TypeMirror sc = e.getSuperclass();
        String scName = print(info, sc, fqn);

        if ( sc == null || 
             e.getKind() == ElementKind.ENUM ||
             e.getKind() == ElementKind.ANNOTATION_TYPE ||
             "Object".equals(scName) || // NOI18N
             "<none>".equals(scName)) { // NOI18N
            scName = null;
        }

        List<? extends TypeMirror> ifaces = e.getInterfaces();

        if ( ( scName != null || !ifaces.isEmpty() ) &&
              e.getKind() != ElementKind.ANNOTATION_TYPE ) {
            sb.append( " :: " ); // NOI18N
            if (scName != null) {                
                sb.append( "<font color=" + ui.getTypeColor() + ">" ); // NOI18N
                sb.append( scName );
                sb.append("</font>"); // NOI18N
            }
            if ( !ifaces.isEmpty() ) {
                if ( scName != null ) {
                    sb.append( " : " ); // NOI18N
                }
                for (Iterator<? extends TypeMirror> it = ifaces.iterator(); it.hasNext();) {
                    TypeMirror typeMirror = it.next();
                    sb.append( "<font color=" + ui.getTypeColor() + ">" ); // NOI18N
                    sb.append( print(info, typeMirror, fqn) );
                    sb.append("</font>"); // NOI18N
                    if ( it.hasNext() ) {
                        sb.append(", "); // NOI18N
                    }
                }

            }
        }

        return sb.toString();            
    }