logging in interface methods - java

I have been working on java 7 so far and recently moved to java-8, one thing which was surprising is that you can add methods in java-8 interfaces.
So far so good....loved this new stuff!
Now, my problem is that logging is an essential part of any development but seems lombok.extern.slf4j won't let you add log stuffs in by interface methods as it is only allowed on classes and enums.
How do you log your interface methods (if by lombok or is this the only way?? ) ? Or is interface methods not supposed to be logged? what am i missing here?
P.S : At present i am working with System.out.println.... yeah...thats noob :)

Currently Lombok #Slf4j annotation is not supported on interfaces,
but it can be circumvented like this
public interface MyInterface
{
#Slf4j
final class LogHolder
{}
default void action() {
LogHolder.log.error("Error TEST");
}
}

you can add logger to your interface manually, but your logger will be public:
public interface SomeInterface {
Logger log = LoggerFactory.getLogger(SomIface.class);
default void action() {
log.info("TEST");
}
}

Logging is an implementation detail, so an interface shouldn't deal with it. If logging is considered as the responsibility of the interface that will lead to several problems. For example:
If there are more than one class implementations, you don't know which is used because they log with the same name. It's also not possible to fine-tune their log levels by their names in config.
Default methods are public which means they can be called from the outside. This is not very desirable for logging methods.
Logging methods would just pollute the interface. What would one say if 'Map' interface would contain such kind of default methods? 'logDebug' or so. It's just confusing and leads to unnecessary questions. An interface should be a clean API for the intended purpose.
What you could use instead:
Some kind of delegation which Lombok also has support for. (Composition over inheritance)
Some kind of Aspect Oriented Programming technique. There are frameworks for that but it's also possible to achieve the same by "Dynamic Proxies". This is also in connection with interfaces.

We can add an abstract method getLogger in interface. And ask the implementer to pass the logger object.
public interface CustomRepo{
Logger getLogger();
default void processData(Data data){
// Do something
getLogger().info("Processing");
// Do something
}
}
#Slf4j
public RepoImplementer implements CustomRepo {
#Override
public Logger getLogger(){
return log;
}
}

Related

why using service implementation pattern in spring

Why loose coupling under java code?
i don't understand, be loose coupling when using interface
why using interface?
Service.java
interface Service{
public void method();
}
in ServiceImpl.java
#Override
ServiceImpl implements Service{
public void method(){
//To-do override
}
}
When you program to the interface you usually inject that interface in other classes, when calling methods you then call the methods on the interface and not the actual implementation. Thus, when you want to switch the implementation it is as simple as replacing your #Bean method in your Configuration class with the new implementation.
Imagine you don't do this and want to change the implementation. You would need to find all occurences in your codebase and replace it with the new implementation.
Other advantages of coding to the interface include increased testability, since you can mock your dependencies, allows for the use of JDK dynamic proxy and increased cohesion between your classes.

Can interfaces evolve with time?

Interfaces are great from a flexibility standpoint. But in case, where an interface is used by a large number of clients. Adding new methods to the interface while keeping the old mehtods intact will break all clients' code as new methods won't be present in clients. As shown below:
public interface CustomInterface {
public void method1();
}
public class CustomImplementation implements CustomInterface {
#Override
public void method1() {
System.out.println("This is method1");
}
}
If at some point later in time, we add another method to this interface all clients' code will break.
public interface CustomInterface {
public void method1();
public void method2();
}
To avoid this we have to explicitly implement new methods in all clients' code.
So I think of interfaces and this scenario as following:
Interfaces once written are like carving in stone. They are rarely supposed, and expected to change. And if they do, they come with a huge cost(rewriting the whole code) which programmers should be ready for.
In continuation with the point above, Is it possible to write interfaces that can stand the test of time?
How such a scenario is handled in interfaces where you expect additional functionality in future? That is anticipating change in the contract by which all clients are binded.
EDIT: Default method is indeed a nice addition to Java Interfaces which a lot of people have mentioned in their answers. But my question was more in the context of code design. And how forcing method implementation on the client is an intrinsic character of an interface. But this contract between an interface and a client seems fragile as functionality will eventually evolve.
One solution to this problem was introduced in Java 8 in the form of default methods in interfaces. It allowed to add new methods to existing Java SE interfaces without breaking existing code, since it supplied default implementation to all the new methods.
For example, the Iterable interface, which is widely used (it's a super interface of the Collection interface) was added two new default methods - default void forEach(Consumer<? super T> action) and default Spliterator<T> spliterator().
public interface CustomInterface {
public void method1();
}
public interface CustomInterface2 extends CustomInterface {
public void meathod2();
}
Other than default method you can use inheritance property as show above by which new interface will have all previous method along with new methods and use this interface in your required situation.
Java 8 has introduced default implementation for methods. These implementations reside in the interface. If a new method with a default implementation is created in an interface that is already implemented by many classes, there is no need to modify all the classes, but only the ones that we want to have a different implementation for the newly defined method than the default one.
Now, what about older Java versions? Here we can have another interface that extends the first one. After that, classes that we want to implement the newly-declared method will be changed to implement the new interface. As shown below.
public interface IFirst {
void method1();
}
public class ClassOne implements IFirst() {
public void method1();
}
public class ClassTwo implements IFirst() {
public void method1();
}
Now, we want method2() declared, but it should only be implemented by ClassOne.
public interface ISecond extends iFirst {
void method2();
}
public class ClassOne implements ISecond() {
public void method1();
public void method2();
}
public class ClassTwo implements IFirst() {
public void method1();
}
This approach will be ok in most cases, but it does have downsides as well. For example, we want method3() (and only that one) for ClassTwo. We will need a new interface IThird. If later we will want to add method4() that should be implemented by both ClassOne and ClassTwo, we will need to modify (but not ClassThree that also implemented IFirst) we will need to change both ISecond and IThird.
There rarely is a "magic bullet" when it comes to programming. In the case of interfaces, it is best if they don't change. This isn't always the case in real-life situations. That is why it is advised that interfaces offer just "the contract" (must-have functionality) and when possible use abstract classes.
A future interface change shouldn't break anything that has been working -- if it does, it's a different interface. (It may deprecate things, though, and a full cycle after deprecation it may be acceptable to say that throwing an Unimplemented exception is acceptable.)
To add things to an interface, the cleanest answer is to derive a new interface from it. That will allow using objects implementing the new behaviors with code expecting the old ones, while letting the user declare appropriately and/or typecast to get access to the new features. It's a bit annoying since it may require instanceof tests, but it's the most robust approach, and it's the one you'll see in many industry standards.
Interfaces are contracts between the developer and clients, so you're right - they are carved in stone and should not be changed. Therefore, an interface should expose (= demand) only the basic functionality that's absolutely required from a class.
Take the List interface for example. There are many implementations of lists in Java, many of which evolve over time (better under-the-hood algorithms, improved memory storage), but the basic "concept" of a list - add an item, search for an item, remove an item - should not and will not ever change.
So, to your question: Instead of writing interfaces which classes implement, you can use abstract classes. Interfaces are basically purely-abstract classes, in the sense that they do not provide any built-in functionality. However, one can add new, non-abstract methods to an abstract class that clients will not be required to implement (override).
Take this abstract class (= interface) for example:
abstract class BaseQueue {
abstract public Object pop();
abstract public void push(Object o);
abstract public int length();
public void clearEven() {};
}
public class MyQueue extends BaseQueue {
#Override
public Object pop() { ... }
...
}
Just like in interfaces, every class that extends BaseQueue is contractually bound to implement the abstract methods. The clearEven() method, however, is not an abstract method (and already comes with an empty implementation), so the client is not forced to implement it, or even use it.
That means that you can leverage the power of abstract classes in Java in order to create non-contractually-binding methods. You can add other methods to the base class in the future as much as you like, provided that they are not abstract methods.
I think your question is more about design and techniques, so java8 answers are a bit misleading. This problem was known long before java8, so there are some other solutions for it.
First, there are no absolutely chargeless ways to solve a problem. The size of inconviniences that come from interface evolving depends on how the library is used and how deliberate your design is.
1) No techniques will help, if you designed an interface and forgot to include a mandatory method in it. Plan your design better and try to anticipate how clients will use your interfaces.
Example: Imagine Machine interface that has turnOn() method but misses turnOff() method. Introducing a new method with default empty implementation in java8 will prevent compilation errors but will not really help, because calling a method will have no effect. Providing working implementation is sometimes impossible because interface has no fields and state.
2) Different implementations usually have things in common. Don't be afraid to keep common logic in parent class. Inherit your library classes from this parent class. This will enforce library clients to inherit their own implementations from your parent class as well. Now you can make small changes to the interface without breaking everything.
Example: You decided to include isTurnedOn() method to your interface. With a basic class, you can write a default method implementation that would make sence. Classes that were not inherited from parent class still need to provide their own method implementations, but since method is not mandatory, it will be easy for them.
3) Upgrading the functionality is usually achieved by extending the interfaces. There's no reason to force library clients to implement a bunch of new methods because they may not need them.
Example: You decided to add stayIdle() method to your interface. It makes sence for classes in your library, but not for custom client classes. Since this functionality is new, it's better to create a new interface that will extend Machine and use it when it's needed.

Using annotations to force method implementation

Ok, this may be a silly question but I have to know for sure... I have a class which I need to annotate with a custom annotation. Also, this class has to implement a custom interface. Now the question: is there any way I can just annotate the class and then use the annotation ALSO like an interface for example forcing a method implementation? I think that is not possible but maybe there's a way... Thank you.
Yes and no. You can eg. write an annotation processor which checks if a particular method has been implemented. At runtime you will have to check for the annotation and method using reflection if you're not implementing an interface. But this is acutually how most modern frameworks work these days (not forcing you to implement/ extend some specific interface/ class).
No, because annotations are used to describe some meta-info and they are not used by compiler to define class types (and java as you know is strongly typed language).
The main issue will be in the next case:
public interface Hello{
public void sayHello();
}
#Hello
public class HelloImpl{
...
}
public class HelloService{
public void perform (Hello hello) {}
}
So if your HelloImpl will not implement Hello interface, then in java there is no way to pass your HelloImpl in HelloService

What is there to "override" in an interface?

Starting with Java 1.6, we can use #Override to mark the implementation of methods defined in the interface. I understand the values of having that annotation, which I use systematically. But can someone explain to me what on earth is there to "override" since the interface just defines a contract and doesn't provide a default implementation?
Yes, there is nothing to override and the only plausible explanation is that this was an offshoot of convention.
In the context of a class implementing an interface, you don't really need this since the compiler will come screaming if you fail to write code for all interface methods anyway. In this setting, the annotation works like a marker, no different than a comment.
Also, if it's interface-related, IDE's ought to stop including #Override it in auto-generated quick-fix method stubs.
On top of that they should generate an "unused" code warning if the #Override annotation is used on an implementation of an interface method.
Semantically, it only makes sense if you consider an interface to be a kind of empty base class instead of a completely different thing. And historically this is in fact the origin of the interface concept. C++ was a language that didn't have a native concept of "interface", but that supported giving a single class multiple base classes. Many people who used the language felt that having multiple base classes was too unwieldly to be useful - except in the case of base classes that had no implementation at all, and only method definitions. Subsequent languages, such as Java, formalized the concept of interface-only base classes.
I guess it might be useful in this situation.
interface Goable {
void go(Location loc);
}
class Vehicle implements Goable {
void go() {
...
}
void go(Location loc) {
...
}
}
In the class Vehicle, using #Override can help identify the implemented method.

API design: one generic interface VS three specialized interfaces?

I'm working on a tool where users can use their own annotations to describe data processing workflow (like validation, transformation etc).
Besides using ready-to-use annotations, users can user their own: in order to do this they need to declare annotation class itself, and then implement annotation processor (<--it's the main point of this question actualy).
The configured method for data processing may look like this one:
void foo(#Provide("dataId") #Validate(Validator.class) String str) {
doSmth(str);
}
There're naturally three groups of annotations:
those which produce initial values;
those which transforms values (converters);
those which just read values and perform some work (validators, different consumers).
So I need to make a choise: either create one interface for handling all these types of annotations, which can look like this one:
interface GenericAnnotationProcessor {
Object processAnnotation(Annotation annotation, Object processedValue);
}
Or I can add 3 intefaces to the API:
interface ProducerAnnotationProcessor {
Object produceInitValue(Annotation annotation);
}
interface TransformerAnnotationProcessor {
Object transformValue(Annotation annotation, Object currentValue);
}
interface ConsumerAnnotationProcessor {
void consumeValue(Annotation annotation, Object currentValue);
}
The first option is not very clear in use, but the third option pollutes the API with 3 almost similar interfaces.
What would you choose (first of all as an API user) and why?
Thanks!
I would create the first, more general interface, then define the three different implementation classes. Without knowing more about how you will be using this, my first instinct would be to define the Interface and/or a base class (depending upon how much common implementation code was shared between the different processors), and then add specialized processor implementation in derived types, all of whihc share the common interface.
In using the API, I would expect to declare a variable which implements GenericAnnotationProcessor, and then assign the appropriate implementation type depending upon my needs.
It is early here in Portland, OR, but at this moment, at 50% of my required caffeine level, this seems to me like it would provide maximum flexibility while maximizing cade re-use.
Of course, your actual reuirements might dictate otherwise . . .
Hope that was helpful!
Just diving deep into your problem.
As they are executing similar task, with some variance, Strategy pattern #Example should assist you.
Your problem should look like something below.
interface GenericAnnotationProcessor {
Object processAnnotation(Annotation annotation, Object processedValue);
}
interface ProducerAnnotationProcessor implements GenericAnnotationProcessor {
}
interface TransformerAnnotationProcessor implements GenericAnnotationProcessor {
}
interface ConsumerAnnotationProcessor implements GenericAnnotationProcessor {
}
Now you can follow example from Wiki
class Context {
// map of annotation processors
// register(add/remove) annotation processors to the map
public int executeAnnotationProcessor(Annotation annotation, Object processedValue) {
return locateAnnotationProcessor(annotation).processAnnotation(annotation, processedValue);
}
private GenericAnnotationProcessor locateAnnotationProcessor(Annotation annotation) {
// return expected annotation processor
}
}
I believe you can understand.
You can use Interfaces Extending Interfaces More on there
Similar to classes, you can build up inheritance hierarchies of interfaces by using the extends keyword, as in:
interface Washable {
void wash();
}
interface Soakable extends Washable {
void soak();
}
In this example, interface Soakable extends interface Washable. Consequently, Soakable inherits all the members of Washable. A class that implements Soakable must provide bodies for all the methods declared in or inherited by Soakable, wash() and soak(), or be declared abstract. Note that only interfaces can "extend" other interfaces. Classes can't extend interfaces, they can only implement interfaces.
Hope it helps.

Categories