background preloader

Java

Facebook Twitter

Scanning Java annotations at runtime. Annovention - Java Annotation Discovery Library. Version 0.1 Java Annotation Discovery Library Annovention is a Java Annotation discovery library.

annovention - Java Annotation Discovery Library

It works on subscribe-and-listen pattern. It's fully customizable and you can easily extend it. It comes with a ClasspathDiscoverer (extends Discoverer) class which scans your classpath for possible Annotations. Annovention uses Javassist ( library to read class files and discover annotations from them. Read a 5 minute starter here: Sample Usage public class SampleAnnotationDiscoverer { /** Dummy ClassAnnotation listener */static class MyClassAnnotationDiscoveryListener implements ClassAnnotationDiscoveryListener { private static Log log = LogFactory.getLog(MyClassAnnotationDiscoveryListener.class); @Override public void discovered(String clazz, String annotation) { log.info("Discovered Class(" + clazz + ") with Annotation(" + annotation + ")"); } @Override public String[] supportedAnnotations() { // Listens for @Entity and @Table annotations.

Parsing - Get Class Annotations from Java Source File. Playing with Java annotation processing. How Java annotations might help to bring world peace. Home > How Java annotations might help to bring world peace … well ok, that title might be a little too catchy.

How Java annotations might help to bring world peace

But here’s the story: Using Java annotations to make programmers’ lifes a bit easier, is one of the language features that is, in my opinion, underestimated. A long time ago I’ve introduced a mighty @Workaround annotation in our code base in order to mark, guess it, workarounds! I simply felt bad to implement temporary workarounds that should be removed once an issue of a third party framework was resolved. And the javadoc belonging to the annotation: Signifies that a workaround was implemented due to bugs/inadequateness of third party software or to mark “code smells” and hacks. Here it is in action: The annotation turned out as incredible useful and is used heavily.

It will make your code base a bit cleaner in the mid-term and may save some peoples’ nerves. Source Code Analysis Using Java 6 APIs. Have you ever thought of how tools like " or " perform a static code analysis, or how Integrated Development Environments (IDEs) like NetBeans or " execute quick code fixes or find the exact references of a field declared in your code?

Source Code Analysis Using Java 6 APIs

In many cases, IDEs have their own APIs to parse the source code and generate a standard tree structure, called an Abstract Syntax Tree (AST) or "parse tree," which can be used for deeper analysis of the source elements. The good news is that it is now possible to accomplish the said tasks plus a lot more with the help of three new APIs introduced in Java as part of the Java Standard Edition 6 release. The APIs that might be of interest to developers of Java applications that need to perform source code analysis are the " Compiler API (JSR 199), the " Annotation Processing API (JSR 269), and the " Tree API. [/prettify] Let us go on and analyze this class as part of the build process with the help of these three APIs.

Plugging In the Annotation Processor. Aspect oriented programming with Java. 2013 Cost of Cyber Crime study A simple example As a simple example of an implementation of an Aspect let us implement the world's simplest logging Aspect.

Aspect oriented programming with Java

To do this we will log what happens during the execution of the classic Hello World Java program. For the purposes of this example, the Hello World program I shall use is presented below: package com.reg.dev.aspects; public class HelloWorld { public void print() { System.out.println("Hello World"); } public static void main(String [] args) { HelloWorld hw = new HelloWorld(); hw.print(); } } We can now implement an Aspect using AspectJ to provide for system logging. I then implemented my logging aspect within Eclipse. In the example in Figure 1, the whole aspect is called Logger, log is the pointcut that implements the join point for this aspect and there are two advice points. Method call and execution Constructor call and execution Read/write access to a field Exception handler execution Object and class initialization execution Summary. Java Code Geeks. Exception Handling Strategies.

In my work as consultant I often get to look into a clients existing applications.

Exception Handling Strategies

The exception handling I see in these applications is anywhere from almost random, to almost useful. There is often no formal, coherent exception handling strategy. Or, there is a strategy, but it is insufficient, meaning it does not contain all the rules, information and precautions necessary to fully handle all exceptions. That's why I decided to write this trail on exception handling strategies. You may have noticed my trail on Java Exception Handling, which contains a list of text explaining a set of basic exception handling techniques. In this trail I will explore how to put all the individual exception handling techniques into a single, coherent exception handling strategy. The exception handling strategy presented in this trail is language independent. Being an exploration of the subject, the suggestions inhere may be able to be improved.