Created
September 11, 2016 14:11
-
-
Save ZhdanRuslan/a53c591a9f913d129a97349ad0ef061a to your computer and use it in GitHub Desktop.
Level 38, Lesson 10, Home 01
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.test.level38.lesson10.home01; | |
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.test.level38.lesson10.home01; | |
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.test.level38.lesson10.home01; | |
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.test.level38.lesson10.home01; | |
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.test.level38.lesson10.home01; | |
/* 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. | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment