Skip to content

Commit c68e109

Browse files
committed
nov1
1 parent f01f6f2 commit c68e109

42 files changed

Lines changed: 548 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ReflectionDemo</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.lang.annotation.ElementType;
2+
import java.lang.annotation.Retention;
3+
import java.lang.annotation.RetentionPolicy;
4+
import java.lang.annotation.Target;
5+
6+
@Retention(RetentionPolicy.RUNTIME)
7+
@Target(ElementType.METHOD)
8+
9+
@interface MyAnnotation
10+
{
11+
public String name();
12+
13+
public String value();
14+
}
15+
16+
public class Display
17+
{
18+
@MyAnnotation(name = "Country", value = "India")
19+
public void doSomething()
20+
{
21+
}
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.lang.annotation.Annotation;
2+
import java.lang.reflect.Method;
3+
4+
/**
5+
* We can access the annotations of a class,method or field at runtime.
6+
*
7+
* Method Annotations Example:
8+
*/
9+
public class ReflectionDemo1
10+
{
11+
public static void main(String[] args)
12+
{
13+
try
14+
{
15+
Class<Display> classObj = Display.class;
16+
Method method = classObj.getMethod("doSomething", null);
17+
/*
18+
* Returns:annotations present on this element *
19+
*/
20+
Annotation[] annotationArray = method.getAnnotations();
21+
22+
for (Annotation annotation : annotationArray)
23+
{
24+
if(annotation instanceof MyAnnotation)
25+
{
26+
MyAnnotation myAnnotation = (MyAnnotation)annotation;
27+
System.out.println("name: "+myAnnotation.name());
28+
System.out.println("value: "+myAnnotation.value());
29+
}
30+
}
31+
}
32+
catch (NoSuchMethodException | SecurityException e)
33+
{
34+
e.printStackTrace();
35+
}
36+
}
37+
38+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.lang.reflect.Method;
2+
3+
/**
4+
* We can access the annotations of a class,method or field at
5+
* runtime.
6+
*
7+
* Method Annotations Example:
8+
*/
9+
public class ReflectionDemo2
10+
{
11+
public static void main(String[] args)
12+
{
13+
try
14+
{
15+
Class<Display> classObj = Display.class;
16+
Method method = classObj.getMethod("doSomething", null);
17+
/*
18+
* Returns: this element's annotation for the specified
19+
* annotation type if present on this element, else null
20+
*
21+
*/
22+
MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
23+
System.out.println("name: " + myAnnotation.name());
24+
System.out.println("value: " + myAnnotation.value());
25+
26+
}
27+
catch (NoSuchMethodException | SecurityException e)
28+
{
29+
e.printStackTrace();
30+
}
31+
}
32+
33+
}

0 commit comments

Comments
 (0)