Skip to content
peichhorn edited this page Jun 30, 2012 · 4 revisions

@AutoGenMethodStub

Overview

With this annotation you can avoid cluttering your code with empty methods that an interface forces you to implement. Just implement the ones you need and lombok will create stubs for the rest.

Example

With Lombok

import lombok.AutoGenMethodStub;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

@AutoGenMethodStub
class AutoGenMethodStubExample implements MouseListener {
  public void mouseExited(MouseEvent e) {
    // defined
  }
}

Vanilla Java

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

class AutoGenMethodStubExample implements MouseListener {
  public void mouseExited(MouseEvent e) {
    // defined
  }

  public void mouseEntered(java.awt.event.MouseEvent arg1) {
  }

  public void mouseReleased(java.awt.event.MouseEvent arg1) {
  }

  public void mousePressed(java.awt.event.MouseEvent arg1) {
  }

  public void mouseClicked(java.awt.event.MouseEvent arg1) {
  }
}

Behind the Scenes

This annotation instructs lombok to find all unimplemented methods of the annotated type and create method stubs for all of them. If the return type of the method is void the method body of the stub will be empty. Otherwise the default value[1] of the return type will be returned.

Configuration

By default the stub method will return the default value of the return type. If you prefer that an java.lang.UnsupportedOperationException is thrown, use: @AutoGenMethodStub(throwException = true).

With Lombok

import lombok.AutoGenMethodStub;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

@AutoGenMethodStub(throwException = true)
class AutoGenMethodStubExample implements MouseListener {
  public void mouseExited(MouseEvent e) {
    // defined
  }
}

Vanilla Java

class AutoGenMethodStubExample implements MouseListener {
  public void mouseExited(MouseEvent e) {
    // defined
  }
  //...
  public void mouseClicked(java.awt.event.MouseEvent arg1) {
    throw new java.lang.UnsupportedOperationException("This method is not implemented yet.");
  }
}

[1] The Java™ Language Specification; 4.12.5. Initial Values of Variables

Clone this wiki locally