Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some updates #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/.settings
.classpath
.project
/.idea/
*.iml
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@
<version>${findbugs.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package ua.yet.adv.java.annotation;

import lombok.extern.slf4j.Slf4j;

/**
* Sample class to showcase JDK's build-in annotations
*
* @author Yuriy Tkach
*/
@Slf4j
public class JavaBuildInAnnotations {

@Deprecated
Expand All @@ -16,7 +19,7 @@ public JavaBuildInAnnotations(@Deprecated int hello) {
}

public void someMethod(@Deprecated int haha) {
System.out.println(haha + hello);
log.info(String.valueOf(haha + hello));
}

@SafeVarargs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Method;

import lombok.extern.slf4j.Slf4j;
import ua.yet.adv.java.annotation.Init;
import ua.yet.adv.java.annotation.Note;
import ua.yet.adv.java.annotation.Service;
Expand All @@ -15,17 +16,18 @@
* annotation in them. If found, then it searches for @{@link Note} annotations
* to output notes and the it inspects methods of class and search for @
* {@link Init} annotation on the method.
*
*
* The result of inspection is output to {@link System#out}.
*
*
* @author Yuriy Tkach
*
*/
@Slf4j
public class AnnotationProcessor {

/**
* Main method that will call inspection on service classes
*
*
* @param args
* No arguments are expected
*/
Expand All @@ -42,54 +44,52 @@ public static void main(String[] args) {
* Inspector method. Checks if @{@link Service} annotation is present. If
* found then outputs its params and searches for @{@link Note} annotations.
* Then calls method to inspect and output more information about the class.
*
*
* @param service
* Service class object
*/
private static void inspectService(Class<?> service) {
if (service.isAnnotationPresent(Service.class)) {
System.out.println("Class " + service.getSimpleName()
+ " has annotation @Service");

log.info("Class " + service.getSimpleName() + " has annotation @Service");
Service annotation = service.getAnnotation(Service.class);
System.out.println(" Name: " + annotation.name());
log.info(" Name: " + annotation.name());
if (annotation.lazyLoad()) {
System.out.println(" Service should load lazy");
log.info(" Service should load lazy");
}

inspectNotes(service);

inspectMethodInformation(service);

} else {
System.out.println("Class " + service.getSimpleName()
log.info("Class " + service.getSimpleName()
+ " does not have annotation @Service");
}
}

/**
* Searches for @{@link Note} annotations and outputs their value if found
*
*
* @param service
* Service class object
*/
private static void inspectNotes(Class<?> service) {
Note[] notes = service.getAnnotationsByType(Note.class);
if (notes != null && notes.length > 0) {
System.out.println(" Founds notes:");
log.info(" Founds notes:");
for (Note note : notes) {
System.out.println(" " + note.value());
log.info(" " + note.value());
}
} else {
System.out.println(" No notes found");
log.info(" No notes found");
}
}

/**
* Inspects declared method of the class and searches @{@link Init}
* annotation on them. If found outputs parameters of annotation and checks
* if method accepts arguments.
*
*
* @param service
* Service class object
*/
Expand All @@ -99,27 +99,26 @@ private static void inspectMethodInformation(Class<?> service) {

for (Method method : methods) {
if (method.isAnnotationPresent(Init.class)) {
System.out.println(" Method " + method.getName()
log.info(" Method " + method.getName()
+ " has annotation @Init");

Init ann = method.getAnnotation(Init.class);
if (ann.suppressException()) {
System.out.println(
" Exceptions of method will be suppressed");
log.info(" Exceptions of method will be suppressed");
}

if (method.getParameterTypes().length != 0) {
System.out.println(" Method expects arguments!");
log.info(" Method expects arguments!");
}

} else {
System.out.println(" Method " + method.getName()
log.info(" Method " + method.getName()
+ " does not have annotation @Init");
}
}

} else {
System.out.println(" Service has no methods.");
log.info(" Service has no methods.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ua.yet.adv.java.annotation.services;

import lombok.extern.slf4j.Slf4j;
import ua.yet.adv.java.annotation.Service;

/**
Expand All @@ -9,13 +10,14 @@
* @author Yuriy Tkach
*
*/
@Slf4j
public class DummyService {

/**
* Init method that will never be called by service loader
*/
public void init() {
System.out.println("I am dumb");
log.info("I am dumb");
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ua.yet.adv.java.annotation.services;

import lombok.extern.slf4j.Slf4j;
import ua.yet.adv.java.annotation.Init;
import ua.yet.adv.java.annotation.Service;

Expand All @@ -10,6 +11,7 @@
* @author Yuriy Tkach
*
*/
@Slf4j
@Service(name = "Lazy service", lazyLoad = true)
public class LazyService {

Expand All @@ -21,7 +23,7 @@ public class LazyService {
*/
@Init
public void init() throws Exception {
System.out.println("I was lazy inited");
log.info("I was lazy inited");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ua.yet.adv.java.annotation.services;

import lombok.extern.slf4j.Slf4j;
import ua.yet.adv.java.annotation.Init;
import ua.yet.adv.java.annotation.Note;
import ua.yet.adv.java.annotation.Service;
Expand All @@ -11,6 +12,7 @@
* @author Yuriy Tkach
*
*/
@Slf4j
@Service(name = "Simple service")
@Note("This is an interesting service")
@Note("I bet you've never seen something like that :)")
Expand All @@ -34,10 +36,9 @@ public class SimpleService {
@Init
public void initService() {
inits++;

System.out.println(
" >> Initializing in public init... (inits="
+ inits + ", const=" + CONST + ")");

log.info(" >> Initializing in public init... (inits="
+ inits + ", const = " + CONST + " )");
}

/**
Expand All @@ -49,7 +50,7 @@ public void initService() {
@Init
public void initServiceArgs(int arg) {
inits++;
System.out.println(" >> Initializing with args ...");
log.info(" >> Initializing with args ...");
}

/**
Expand All @@ -59,7 +60,7 @@ public void initServiceArgs(int arg) {
@Init
private void privateInit() {
inits++;
System.out.println(
log.info(
" >> Initializing in private init... (inits="
+ inits + ", const=" + CONST + ")");
}
Expand Down
Loading