The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to access the variable Annotation using Java Reflection | Reflection in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
How to access the variable Annotation using Java Reflection | Reflection in java Posted: Nov 7, 2017 6:18 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: How to access the variable Annotation using Java Reflection | Reflection in java
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube : 
https://www.youtube.com/watch?v=I8QExTfkTSg&list=UUhwKlOVR041tngjerWxVccw

Display.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)

@interface MyAnnotation
{
public String name();

public String value();
}

public class Display
{

@MyAnnotation(name = "age", value = "25")
public String myField = null;
}
ReflectionDemo1.java
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
* Example of a field with annotations
*/

public class ReflectionDemo1
{
public static void main(String[] args)
{
try
{
Class<Display> classObj = Display.class;
Field field = classObj.getField("myField");
Annotation[] annotations = field.getDeclaredAnnotations();

for (Annotation annotation : annotations)
{
if (annotation instanceof MyAnnotation)
{
MyAnnotation myAnnotation = (MyAnnotation) annotation;
System.out.println("name: " + myAnnotation.name());
System.out.println("value: " + myAnnotation.value());
}
}
}
catch (NoSuchFieldException | SecurityException e)
{
e.printStackTrace();
}

}

}
ReflectionDemo2
import java.lang.reflect.Field;

/**
* Example of a field with annotations
*/

public class ReflectionDemo2
{
public static void main(String[] args)
{
try
{
Class<Display> classObj = Display.class;
Field field = classObj.getField("myField");
MyAnnotation myAnnotation = field.getAnnotation(MyAnnotation.class);

System.out.println("name: " + myAnnotation.name());
System.out.println("value: " + myAnnotation.value());
}
catch (NoSuchFieldException | SecurityException e)
{
e.printStackTrace();
}

}

}
Output
name: age
value: 25

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_access_field_annotation.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_access_field_annotation

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/7b0b4fbacd011a10dc23b42c4acc3ae6988db782/BasicJava/ReflectionDemo_access_field_annotation/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Read: How to access the variable Annotation using Java Reflection | Reflection in java

    Topic: Java Command-Line Interfaces (Part 30): Observations Previous Topic   Next Topic Topic: Jenkins vs Travis CI vs Circle CI vs TeamCity vs Codeship vs GitLab CI vs Bamboo

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use