Created
May 23, 2017 13:52
-
-
Save fermopili/66b1ba925e570b1ca6be2f6f6eb1062d to your computer and use it in GitHub Desktop.
com.javarush.task.task38.task3809 Annotation + Reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task38.task3809; | |
public class IncorrectAccount { | |
@LongPositive | |
private String amountString; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task38.task3809; | |
public class JavaRushBankAccount { | |
private String ownerName; | |
@LongPositive | |
private long amount; | |
public JavaRushBankAccount(String ownerName) { | |
this.ownerName = ownerName; | |
} | |
public long getAmount() { | |
return amount; | |
} | |
public void setAmount(long amount) { | |
this.amount = amount; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task38.task3809; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Target(value = ElementType.FIELD) | |
@Retention(value=RetentionPolicy.RUNTIME) | |
public @interface LongPositive { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task38.task3809; | |
import java.lang.reflect.Field; | |
public final class ReflectionAnnotationUtil | |
{ | |
public static void check(Object someObject) throws IllegalAccessException | |
{ | |
Class<?> testedClass = someObject.getClass ( ); | |
for (Field field : testedClass.getDeclaredFields ( )) | |
{ | |
if (field.isAnnotationPresent ( LongPositive.class )) | |
{ | |
processLongPositiveAnnotationField ( someObject, testedClass, field ); | |
} | |
} | |
} | |
private static void processLongPositiveAnnotationField(Object someObject, Class<?> testedClass, Field field) throws | |
IllegalAccessException | |
{ | |
field.setAccessible ( true ); | |
Class<?> fieldType = field.getType ( ); | |
//assert type is long | |
if (!fieldType.equals ( long.class )) | |
{ | |
String msg = String.format ( "Поле %s в классе %s имеет аннотацию LongPositive, но его тип %s.", | |
field.getName ( ), testedClass.getSimpleName ( ), fieldType.getSimpleName ( ) ); | |
System.out.println ( msg ); | |
return; | |
} | |
//assert value is positive | |
long value = (long) field.get ( someObject ); | |
if (value <= 0) | |
{ | |
String msg = String.format ( "Поле %s в классе %s имеет аннотацию LongPositive, но его значение не положительное.", | |
field.getName ( ), testedClass.getSimpleName ( ) ); | |
System.out.println ( msg ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task38.task3809; | |
/* | |
Annotation + Reflection | |
*/ | |
public class Solution { | |
public static void main(String[] args) throws IllegalAccessException { | |
JavaRushBankAccount account = new JavaRushBankAccount("Mr.Smith"); | |
System.out.println("Проверка №1:"); | |
ReflectionAnnotationUtil.check(account); | |
System.out.println("Проверка №2:"); | |
account.setAmount(100); | |
ReflectionAnnotationUtil.check(account); | |
System.out.println("Проверка №3:"); | |
ReflectionAnnotationUtil.check(new IncorrectAccount()); | |
/* Ожидаемый вывод: | |
Проверка №1: | |
Поле amount в классе JavaRushBankAccount имеет аннотацию LongPositive, но его значение не положительное. | |
Проверка №2: | |
Проверка №3: | |
Поле amountString в классе IncorrectAccount имеет аннотацию LongPositive, но его тип String. | |
*/ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
taskKey="com.javarush.task.task38.task3809" | |
Annotation + Reflection | |
Разберись в коде и исправьте ошибку. | |
Ориентируйся на ожидаемый вывод. | |
Сделай минимально возможные изменения. | |
Требования: | |
1. Программа должна выводить данные на экран. | |
2. Вывод на экран должен соответствовать ожидаемому. | |
3. Аннотация(@interface) LongPositive должна быть отмечена двумя другими аннотациями. | |
4. Аннотация(@interface) LongPositive должна применяться только к полям. | |
5. Аннотация(@interface) LongPositive должна быть доступна во время выполнения программы. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment