public static Builder overriding(ExecutableElement method) {
    checkNotNull(method, "method == null");

    Element enclosingClass = method.getEnclosingElement();
    if (enclosingClass.getModifiers().contains(Modifier.FINAL)) {
      throw new IllegalArgumentException("Cannot override method on final class " + enclosingClass);
    }

    Set<Modifier> modifiers = method.getModifiers();
    if (modifiers.contains(Modifier.PRIVATE)
        || modifiers.contains(Modifier.FINAL)
        || modifiers.contains(Modifier.STATIC)) {
      throw new IllegalArgumentException("cannot override method with modifiers: " + modifiers);
    }

    String methodName = method.getSimpleName().toString();
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName);

    methodBuilder.addAnnotation(Override.class);

    modifiers = new LinkedHashSet<>(modifiers);
    modifiers.remove(Modifier.ABSTRACT);
    modifiers.remove(Modifier.DEFAULT);
    methodBuilder.addModifiers(modifiers);

    for (TypeParameterElement typeParameterElement : method.getTypeParameters()) {
      TypeVariable var = (TypeVariable) typeParameterElement.asType();
      methodBuilder.addTypeVariable(TypeVariableName.get(var));
    }

    methodBuilder.returns(TypeName.get(method.getReturnType()));
    methodBuilder.addParameters(ParameterSpec.parametersOf(method));
    methodBuilder.varargs(method.isVarArgs());

    for (TypeMirror thrownType : method.getThrownTypes()) {
      methodBuilder.addException(TypeName.get(thrownType));
    }

    return methodBuilder;
  }
public static Builder overriding ( ExecutableElement method ) { checkNotNull ( method , "method == null" ) ; Element enclosingClass = method . getEnclosingElement ( ) ; if ( enclosingClass . getModifiers ( ) . contains ( Modifier . FINAL ) ) { throw new IllegalArgumentException ( "Cannot override method on final class " + enclosingClass ) ; } Set < Modifier > modifiers = method . getModifiers ( ) ; if ( modifiers . contains ( Modifier . PRIVATE ) || modifiers . contains ( Modifier . FINAL ) || modifiers . contains ( Modifier . STATIC ) ) { throw new IllegalArgumentException ( "cannot override method with modifiers: " + modifiers ) ; } String methodName = method . getSimpleName ( ) . toString ( ) ; MethodSpec . Builder methodBuilder = MethodSpec . methodBuilder ( methodName ) ; methodBuilder . addAnnotation ( Override . class ) ; modifiers = new LinkedHashSet < > ( modifiers ) ; modifiers . remove ( Modifier . ABSTRACT ) ; modifiers . remove ( Modifier . DEFAULT ) ; methodBuilder . addModifiers ( modifiers ) ; for ( TypeParameterElement typeParameterElement : method . getTypeParameters ( ) ) { TypeVariable var = ( TypeVariable ) typeParameterElement . asType ( ) ; methodBuilder . addTypeVariable ( TypeVariableName . get ( var ) ) ; } methodBuilder . returns ( TypeName . get ( method . getReturnType ( ) ) ) ; methodBuilder . addParameters ( ParameterSpec . parametersOf ( method ) ) ; methodBuilder . varargs ( method . isVarArgs ( ) ) ; for ( TypeMirror thrownType : method . getThrownTypes ( ) ) { methodBuilder . addException ( TypeName . get ( thrownType ) ) ; } return methodBuilder ; }
Returns a new method spec builder that overrides {@code method}.

<p>This will copy its visibility modifiers, type parameters, return type, name, parameters, and
throws declarations. An {@link Override} annotation will be added.

<p>Note that in JavaPoet 1.2 through 1.7 this method retained annotations from the method and
parameters of the overridden method. Since JavaPoet 1.8 annotations must be added separately.