Java Code Examples for javax.lang.model.element.ExecutableElement#isDefault()

The following examples show how to use javax.lang.model.element.ExecutableElement#isDefault() . 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: DaoMetaFactory.java    From doma with Apache License 2.0 6 votes vote down vote up
private void validateMethod(TypeElement interfaceElement, ExecutableElement methodElement) {
  TypeElement foundAnnotationTypeElement = null;
  for (AnnotationMirror annotation : methodElement.getAnnotationMirrors()) {
    DeclaredType declaredType = annotation.getAnnotationType();
    TypeElement typeElement = ctx.getMoreTypes().toTypeElement(declaredType);
    if (typeElement.getAnnotation(DaoMethod.class) != null) {
      if (foundAnnotationTypeElement != null) {
        throw new AptException(
            Message.DOMA4087,
            methodElement,
            new Object[] {
              foundAnnotationTypeElement.getQualifiedName(), typeElement.getQualifiedName()
            });
      }
      if (methodElement.isDefault()
          || ctx.getMoreElements().isVirtualDefaultMethod(interfaceElement, methodElement)) {
        throw new AptException(
            Message.DOMA4252, methodElement, new Object[] {typeElement.getQualifiedName()});
      }
      foundAnnotationTypeElement = typeElement;
    }
  }
}
 
Example 2
Source File: GeneratorUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private Map<String, Object> createBindings(TypeElement clazz, ExecutableElement element) {
    CodeStyle cs = DiffContext.getCodeStyle(copy);       
    Map<String, Object> bindings = new HashMap<>();
    if (clazz != null) {
        bindings.put(CLASS_NAME, clazz.getQualifiedName().toString());
        bindings.put(SIMPLE_CLASS_NAME, clazz.getSimpleName().toString());
    }
    if (element != null) {
        bindings.put(METHOD_NAME, element.getSimpleName().toString());
        bindings.put(METHOD_RETURN_TYPE, element.getReturnType().toString()); //NOI18N
        Object value;
        switch(element.getReturnType().getKind()) {
            case BOOLEAN:
                value = "false"; //NOI18N
                break;
            case BYTE:
            case CHAR:
            case DOUBLE:
            case FLOAT:
            case INT:
            case LONG:
            case SHORT:
                value = 0;
                break;
            default:
                value = "null"; //NOI18N
        }
        bindings.put(DEFAULT_RETURN_TYPE_VALUE, value);
    }
    if (clazz != null && element != null) {
        StringBuilder sb = new StringBuilder();
        if (element.isDefault() && element.getEnclosingElement().getKind().isInterface()) {
            Types types = copy.getTypes();
            Context ctx = ((JavacTaskImpl) copy.impl.getJavacTask()).getContext();
            com.sun.tools.javac.code.Types typesImpl = com.sun.tools.javac.code.Types.instance(ctx);
            TypeMirror enclType = typesImpl.asSuper((Type)clazz.asType(), ((Type)element.getEnclosingElement().asType()).tsym);
            if (!types.isSubtype(clazz.getSuperclass(), enclType)) {
                TypeMirror selected = enclType;
                for (TypeMirror iface : clazz.getInterfaces()) {
                    if (types.isSubtype(iface, selected) &&
                        !types.isSameType(iface, enclType)) {
                        selected = iface;
                        break;
                    }
                }
                sb.append(((DeclaredType)selected).asElement().getSimpleName()).append('.');
            }
        }
        sb.append("super.").append(element.getSimpleName()).append('('); //NOI18N
        for (Iterator<? extends VariableElement> it = element.getParameters().iterator(); it.hasNext();) {
            VariableElement ve = it.next();
            sb.append(addParamPrefixSuffix(removeParamPrefixSuffix(ve, cs), cs));
            if (it.hasNext())
                sb.append(","); //NOI18N
        }
        sb.append(')'); //NOI18N
        bindings.put(SUPER_METHOD_CALL, sb);
    }
    return bindings;
}
 
Example 3
Source File: Added.java    From revapi with Apache License 2.0 4 votes vote down vote up
@Override
protected List<Difference> doEnd() {
    ActiveElements<JavaMethodElement> methods = popIfActive();
    if (methods == null) {
        return null;
    }

    // we need to consider several cases here:
    // 1) method added to a interface
    // 2) method added to a final class
    // 3) concrete method added to a non-final class
    // 4) abstract method added to a non-final class
    // 5) final method added to a non-final class
    // 5) previously inherited method is now declared in class

    ExecutableElement method = methods.newElement.getDeclaringElement();

    if (methods.newElement.getParent() == null) {
        LOG.warn("Could not find an enclosing class of method " + method + ". That's weird.");
        return null;
    }

    TypeElement enclosingClass = (TypeElement) methods.newElement.getParent().getDeclaringElement();

    Difference difference;

    if (enclosingClass.getKind() == ElementKind.INTERFACE) {
        if (method.isDefault()) {
            difference = createDifference(Code.METHOD_DEFAULT_METHOD_ADDED_TO_INTERFACE,
                    Code.attachmentsFor(methods.oldElement, methods.newElement));
        } else if (method.getModifiers().contains(Modifier.STATIC)) {
            //statics on interface can only be called using the interface they are declared on, even if a method
            //with a same signature was declared on some of the super types in the old version, the users would
            //not have been able to call those methods using the current type. So we don't need to specialize here
            //based on the presence of a previously inherited method.
            difference = createDifference(Code.METHOD_STATIC_METHOD_ADDED_TO_INTERFACE,
                    Code.attachmentsFor(methods.oldElement, methods.newElement));
        } else {
            difference = createDifference(Code.METHOD_ADDED_TO_INTERFACE,
                    Code.attachmentsFor(methods.oldElement, methods.newElement));
        }
    } else if (method.getModifiers().contains(Modifier.ABSTRACT)) {
        difference = createDifference(Code.METHOD_ABSTRACT_METHOD_ADDED,
                Code.attachmentsFor(methods.oldElement, methods.newElement));
    } else if (method.getModifiers().contains(Modifier.FINAL) &&
            !enclosingClass.getModifiers().contains(Modifier.FINAL)) {

         difference = createDifference(Code.METHOD_FINAL_METHOD_ADDED_TO_NON_FINAL_CLASS,
                 Code.attachmentsFor(methods.oldElement, methods.newElement));
    } else {
        difference = createDifference(Code.METHOD_ADDED,
                Code.attachmentsFor(methods.oldElement, methods.newElement));
    }

    return Collections.singletonList(difference);
}