Compare commits
No commits in common. "master" and "feature/annotation" have entirely different histories.
master
...
feature/an
11
src/Annotations/ValidLength.java
Normal file
11
src/Annotations/ValidLength.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package Annotations;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface ValidLength {
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
package Characters;
|
package Characters;
|
||||||
|
|
||||||
|
import Annotations.ValidLength;
|
||||||
import Entity.Entity;
|
import Entity.Entity;
|
||||||
import Items.Item;
|
import Items.Item;
|
||||||
|
|
||||||
@ -25,6 +26,8 @@ public abstract class Character extends Entity implements IMovable, IFeelable, I
|
|||||||
|
|
||||||
private final Gender gender;
|
private final Gender gender;
|
||||||
private Entity position;
|
private Entity position;
|
||||||
|
|
||||||
|
@ValidLength
|
||||||
private String currentState = "";
|
private String currentState = "";
|
||||||
protected Feels feel;
|
protected Feels feel;
|
||||||
protected Item itemInHand;
|
protected Item itemInHand;
|
||||||
@ -62,6 +65,7 @@ public abstract class Character extends Entity implements IMovable, IFeelable, I
|
|||||||
|
|
||||||
protected final void setPosition(Entity obj) {
|
protected final void setPosition(Entity obj) {
|
||||||
this.position = obj;
|
this.position = obj;
|
||||||
|
this.validateFields();
|
||||||
}
|
}
|
||||||
|
|
||||||
public final String getState() {
|
public final String getState() {
|
||||||
@ -70,6 +74,7 @@ public abstract class Character extends Entity implements IMovable, IFeelable, I
|
|||||||
|
|
||||||
protected final void setState(String state) {
|
protected final void setState(String state) {
|
||||||
this.currentState = state;
|
this.currentState = state;
|
||||||
|
this.validateFields();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -89,4 +94,34 @@ public abstract class Character extends Entity implements IMovable, IFeelable, I
|
|||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(this.name, this.gender);
|
return Objects.hash(this.name, this.gender);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validateFields() {
|
||||||
|
for (java.lang.reflect.Field field : Character.class.getDeclaredFields()) {
|
||||||
|
if (field.isAnnotationPresent(ValidLength.class)) {
|
||||||
|
String value = getValueForField(this, field);
|
||||||
|
if (value.length() < 3 || value.length() > 20) {
|
||||||
|
setValueForField(this, field, "unknown");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getValueForField(Object obj, java.lang.reflect.Field field) {
|
||||||
|
try {
|
||||||
|
field.setAccessible(true);
|
||||||
|
return (String) field.get(obj);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setValueForField(Object obj, java.lang.reflect.Field field, String value) {
|
||||||
|
try {
|
||||||
|
field.setAccessible(true);
|
||||||
|
field.set(obj, value);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user