Skip to content

Instantly share code, notes, and snippets.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MethodInfo {
String author() default "Igor Brishkoski";
int revision() default 1;
String comments();
}
public class MyParentClass {
public void justaMethod() {
System.out.println("Parent class method");
}
}
public class MyChildClass extends MyParentClass {
@Override
public void justaMethod() {
System.out.println("Child class method");
/**
* @deprecated
* reason for why it was deprecated
*/
@Deprecated
public void anyMethodHere(){
// Do something
}
@igor-brishkoski
igor-brishkoski / warnings_annotations.java
Created February 5, 2017 00:37
Suppress Warnings example
@SuppressWarnings("deprecation")
void myMethod() {
myObject.deprecatedMethod();
}
@igor-brishkoski
igor-brishkoski / custom_annotation.java
Created February 5, 2017 00:38
Custom Annotation
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MethodInfo {
String author() default "Igor Brishkoski";
int revision() default 1;
String comments();
}
@igor-brishkoski
igor-brishkoski / custom_annotation_runtime.java
Created February 5, 2017 00:40
Custom annotation runtime example
@MethodInfo(author = "John Snow", revision = 2, comments = "Hey!")
public void awesomeMethod() {
Method method = getClass().getMethod("awesomeMethod");
MethodInfo methodInfo = method.getAnnotation(MethodInfo.class);
Log.d("MethodInfo", methodInfo.author());
Log.d("MethodInfo", methodInfo.revision());
Log.d("MethodInfo", methodInfo.comments());
}
//AwesomeApp
class User {
String firstName;
String lastName;
String city;
}
...
public class MainActivity extends AppCompatActivity {
@Override
@igor-brishkoski
igor-brishkoski / app_build.gradle
Created February 5, 2017 00:43
App build gradle
//our app build.gradle file
dependencies {
...
provided project(':annotation')
...
}
@igor-brishkoski
igor-brishkoski / awesome_logger.java
Created February 5, 2017 00:44
Annotation Module Awesome Logger
// annotation module
package com.example.annotation;
@Target(ElementType.TYPE) //class level
@Retention(RetentionPolicy.SOURCE) //we only need it at compile time
public @interface AwesomeLogger {}
@AwesomeLogger
public class User {
String firstName;
String lastName;
String city;
}