EzDevInfo.com

annotations

The KISS PHP annotations library.

Why does Eclipse complain about @Override on interface methods?

I have an existing project that uses @Override on methods that override interface methods, rather than superclass methods. I cannot alter this in code, but I would like Eclpse to stop complaining about the annotation, as I can still build with Maven.

How would I go about disabling this error?

Note: Due to project requirements, I need to compile for Java 1.5.


Source: (StackOverflow)

Java Annotations

What is the purpose of annotations in Java? I have this fuzzy idea of them as somewhere in between a comment and actual code. Do they affect the program at run time?

What are their typical usages?

Are they unique to Java? Is there a C++ equivalent?


Source: (StackOverflow)

Advertisements

Spring: @Component versus @Bean

I understand that @Component annotation was introduced in spring 2.5 in order to get rid of xml bean definition by using classpath scanning.

@Bean was introduced in spring 3.0 and can be used with @Configuration in order to fully get rid of xml file and use java config instead.

Would it have been possible to re-use the @Component annotation instead of introducing @Bean annotation? My understanding is that the final goal is to create beans in both cases.


Source: (StackOverflow)

Scanning Java annotations at runtime

What is the best way of searching the whole classpath for an annotated class?

I'm doing a library and I want to allow the users to annotate their classes, so when the Web application starts I need to scan the whole classpath for certain annotation.

Do you know a library or a Java facility to do this?

Edit: I'm thinking about something like the new functionality for Java EE 5 Web Services or EJB's. You annotate your class with @WebService or @EJB and the system find these classes while loading so they are accessible remotely.


Source: (StackOverflow)

What's the difference between @Component, @Repository & @Service annotations in Spring?

Can @Component, @Repository & @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device?

In other words, if I have a Service class and I change the annotation from @Service to @Component, will it still behave the same way?

Or does the annotation also influence the behavior and functionality of the class?


Source: (StackOverflow)

Which @NotNull Java annotation should I use?

I'm looking to make my code more readable as well as use tooling like IDE code inspection and/or static code analysis (FindBugs and Sonar) to avoid NullPointerExceptions. Many of the tools seem incompatible with each others' @NotNull/@NonNull/@Nonnull annotation and listing all of them in my code would be terrible to read. Any suggestions of which one is the 'best'? Here is the list of equivalent annotations I've found:

  • javax.validation.constraints.NotNull
    Created for runtime validation, not static analysis.
    documentation

  • edu.umd.cs.findbugs.annotations.NonNull
    Used by Findbugs static analysis and therefore Sonar
    documentation

  • javax.annotation.Nonnull
    This might work with Findbugs too, but JSR-305 is inactive.
    source

  • com.intellij.annotations.NotNull
    Used by IntelliJ IDEA IDE for static analysis.
    documentation

  • lombok.NonNull
    Used to control code generation in Project Lombok.
    Placeholder annotation since there is no standard.
    source, documentation


Source: (StackOverflow)

Difference between vs

I'm learning Spring 3 and I don't seem to grasp the functionality behind <context:annotation-config> and <context:component-scan>.

From what I've read they seem to handle different annotations (@Required, @Autowired etc vs @Component, @Repository, @Service etc) but also from what I've read they register the same bean post processor classes.

To confuse me even more, there is an annotation-config attribute on <context:component-scan>.

Can someone shed some light on these tags? What's similar, what's different, is one superseded by the other, they complete each other, do I need one of them, both?


Source: (StackOverflow)

Spring @Transactional Annotation Best Practice

We are currently discussing the Best Practice for placing the @Transactional annotations in our code.

Do you place the @Transactional in the DAO classes and/or their methods or is it better to annotate the Service classes which are calling using the DAO objects? Or does it make sense to annotate both "layers"?


Source: (StackOverflow)

Should we @Override an interface's method implementation?

Should a method that implements an interface method be annotated with @Override?

The javadoc of the Override annotation says:

Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.

I don't think that an interface is technically a superclass. Or is it?

Question Elaboration


Source: (StackOverflow)

What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?

I am going through some blogs on SpringSource and in one of the blog author is using @Inject and I suppose he can also use @Autowired

Here is the piece of code:

@Inject private CustomerOrderService customerOrderService;

I am not sure about the difference between @Inject and @Autowired and would appreciate if someone can explain the difference and which one to use under what situation?


Source: (StackOverflow)

Why is not possible to extend annotations in Java?

I don't understand why there is no inheritance in Java annotations, just as Java classes. I think it would be very useful.

For example: I want to know if a given annotation is a validator. With inheritance, I could reflexively navigate through superclasses to know if this annotation extends a ValidatorAnnotation. Otherwise, how can I achieve this?

So, can anyone give me a reason why this design decision?


Source: (StackOverflow)

Injecting Mockito mocks into a Spring bean

I would like to inject a Mockito mock object into a Spring (3+) bean for the purposes of unit testing with JUnit. My bean dependencies are currently injected by using the @Autowired annotation on private member fields.

I have considered using ReflectionTestUtils.setField but the bean instance that I wish to inject is actually a proxy and hence does not declare the private member fields of the target class. I do not wish to create a public setter to the dependency as I will then be modifying my interface purely for the purposes of testing.

I have followed some advice given by the Spring community but the mock does not get created and the auto-wiring fails:

<bean id="dao" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="com.package.Dao" />
</bean>

The error I currently encounter is as follows:

...
Caused by: org...NoSuchBeanDefinitionException:
    No matching bean of type [com.package.Dao] found for dependency:
    expected at least 1 bean which qualifies as autowire candidate for this dependency.
    Dependency annotations: {
        @org...Autowired(required=true),
        @org...Qualifier(value=dao)
    }
at org...DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(D...y.java:901)
at org...DefaultListableBeanFactory.doResolveDependency(D...y.java:770)

If I set the constructor-arg value to something invalid no error occurs when starting the application context.


Source: (StackOverflow)

JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

I am getting the following error when trying to get a JSON request and process it>

org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.myweb.ApplesDO]: can not instantiate from JSON object (need to add/enable type information?)

Here is the JSON I am trying to send:

{
  "applesDO" : [
    {
      "apple" : "Green Apple"
    },
    {
      "apple" : "Red Apple"
    }
  ]
}

In Controller , I have the following method signature

@RequestMapping("showApples.do")
public String getApples(@RequestBody final AllApplesDO applesRequest){
    // Method Code
}

AllApplesDO is a wrapper of ApplesDO :

public class AllApplesDO {

    private List<ApplesDO> applesDO;

    public List<ApplesDO> getApplesDO() {
        return applesDO;
    }

    public void setApplesDO(List<ApplesDO> applesDO) {
        this.applesDO = applesDO;
    }
}

ApplesDO

public class ApplesDO {

    private String apple;

    public String getApple() {
        return apple;
    }

    public void setApple(String appl) {
        this.apple = apple;
    }

    public ApplesDO(CustomType custom){
        //constructor Code
    }
}

I am thinking that JACKSON is unable to convert JSON into JAVA objects for sublclasses. Please help with the configuration parameters for JACKSON to convert JSON into JAVA Objects! I am using Spring Framework

EDIT: Included the major bug that is causing this problem in the above sample class - Please look accepted answer for solution.


Source: (StackOverflow)

Which types can be used for Java annotation members?

Today I wanted to create my first annotation interface following this documentation and I got the compiler error "Invalid type for annotation member":

public @interface MyAnnotation {
    Object myParameter;
    ^^^^^^
}

Obviously Object cannot be used as type of an annotation member. Unfortunately I could not find any information on which types can be used in general.

This I found out using trial-and-error:

String --> Valid

int --> Valid

Integer --> Invalid (Surprisingly)

String[] --> Valid (Surprisingly)

Object --> Invalid

Perhaps someone can shed some light on which types are actually allowed and why.


Source: (StackOverflow)

Xml configuration versus Annotation based configuration [closed]

In a few large projects i have been working on lately it seems to become increasingly important to choose one or the other (XML or Annotation). As projects grow, consistency is very important for maintainability.

My question is, what do people prefer. Do you prefer XML based or Annotation based? or Both? Everybody talks about XML configuration hell and how annotations are the answer, what about Annotation configuration hell?


Source: (StackOverflow)