views:

37

answers:

3

Hello there!

If I put:

public CountryState CountryState.find(long id) {
        return (CountryState) findById(CountryState.class, id);
}

I'm creating a method find in the class CountryState.

Is there a way to create a method in several classes? Do I need to repeat the code for each class I want to create?

I know that with aspect I can make a class inherit from another, but, doing this, I can create one superclass because java doesn't accept multiple inheritance.

A: 

You can actually solve it without using AOP. You could just use OOP/OOD. There are two ways (I assume you want to write method once):

  1. Create abstract base class with the method implementation and derive all classes from it. This not the best idea, actually.

  2. Creating helper class that will implement your find() method and share it between classes (either using Dependency Injection, or just by coupling them tightly).

So if I understand it correctly, what you want actually is a generic method that will return instances of the target class:

public <T> find(long id, T targetClassObject) {
  Class<? extends T> class = targetClassObject.getClass();
  // do something i.e. call target method via reflection
}
Paweł Dyda
Please, see my second post.http://stackoverflow.com/questions/3738545/aspectj-creating-innter-type-methods-in-multiple-classes
Ed Pichler
A: 

Thanks for the answer... I need to learn more about generics and I'll use your example in my problem too. But....

I just want to know if AOP supports that I declare a inner type method in a lot of classes, like this.

public String Class1 && Class2.getSomething(){
..
}

This example, of course, doesn't exists. It's just to explain what I would like to do. In this code, I was declaring a method in both classes at same line.

Ed Pichler
AOP is not about declaring methods, it is about intercepting them and modifying their behaviour. You could use it to avoid duplication of common code used across many modules (so called cross-cutting concerns). The typical examples are logging and authentication, i.e. you want to intercept method entering, exiting and any Exceptions thrown to log them during development, but when you ship app you want to see only exceptions (scrambled so customer see only binary error report). BTW. just because you could use AOP, it doesn't mean you should, as it make supporting your app harder.
Paweł Dyda
A: 

This 'pattern' is how you do it in AspectJ.

Declare an interface:

interface Holder {}

Make your intertype declarations on the interface:

public int Holder.getMeAnInt() {
  return 42;
}

When you make a declaration like that on an interface you are providing a 'default implementation'. So the interface will now define getMeAnInt() and any implementations of Holder that do not implement getMeAnInt() will get the default implementation.

The final piece of the puzzle is then to use declare parents to specify which group of types implement your interface:

declare parents: @Anno * implements Holder;

So now, any type annotated with @Anno will implement Holder and have the getMeAnInt() method.

AndyClement
Andy, this is exactly what I need. Perfect! many thanks!
Ed Pichler