-
Notifications
You must be signed in to change notification settings - Fork 44
Fredegrec container #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fredegrec container #101
Changes from all commits
da33a00
1074460
8bdd924
cb2e02a
95ffafa
1a7b396
53165f9
dc289bc
5c9b8a3
37bb08d
848a338
5fdc371
8398d5d
7ba7be8
c93d4fd
ab2d408
8354313
7590f75
8338c57
1aa0e4f
87bb950
54b80df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Alyona Pasevyeva |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,100 @@ | ||
| package track.container; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import java.lang.reflect.Type; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import com.fasterxml.jackson.databind.AnnotationIntrospector; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import track.container.config.Bean; | ||
| import track.container.config.InvalidConfigurationException; | ||
| import track.container.config.Property; | ||
| import track.container.config.ValueType; | ||
|
|
||
| /** | ||
| * Основной класс контейнера | ||
| * У него определено 2 публичных метода, можете дописывать свои методы и конструкторы | ||
| */ | ||
| public class Container { | ||
|
|
||
| private List<Bean> beans; | ||
| private Map<String, Bean> beanById; | ||
| private Map<String, Bean> beanByClassName; | ||
| private Map<String, Object> objByClassName; | ||
|
|
||
| // Реализуйте этот конструктор, используется в тестах! | ||
| public Container(List<Bean> beans) { | ||
|
|
||
| this.beans = beans; | ||
| beanById = new HashMap<>(); | ||
| beanByClassName = new HashMap<>(); | ||
| objByClassName = new HashMap<>(); | ||
| for (Bean bean: beans) { | ||
| beanById.put(bean.getId(), bean); | ||
| beanByClassName.put(bean.getClassName(), bean); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Вернуть объект по имени бина из конфига | ||
| * Например, Car car = (Car) container.getById("carBean") | ||
| */ | ||
| public Object getById(String id) { | ||
| return null; | ||
| public Object getById(String id) throws ClassNotFoundException, IllegalAccessException, InstantiationException, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Слишком сложная сигнатура, как этим методом пользоваться будем? Давайте бросать какое-то одно кастомное исключение на все ошибки |
||
| InvalidConfigurationException, InvocationTargetException, NoSuchFieldException, NoSuchMethodException { | ||
| if (beanById.containsKey(id)) { | ||
| Bean bean = beanById.get(id); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. так его можно и вернуть? |
||
| if (objByClassName.containsKey(bean.getClassName())) { | ||
| return objByClassName.get(bean.getClassName()); | ||
| } | ||
| Class clazz = Class.forName(bean.getClassName()); | ||
| Object object = clazz.newInstance(); | ||
| for (Map.Entry<String, Property> propertyPair: bean.getProperties().entrySet()) { | ||
| Property property = propertyPair.getValue(); | ||
| Field field = clazz.getDeclaredField(property.getName()); | ||
| field.setAccessible(true); | ||
| Object value; | ||
| if (property.getType() == ValueType.VAL) { | ||
| value = stringToPrimitive(field.getType(), property.getValue()); | ||
| } else { | ||
| value = getByClass(field.getType().getName()); | ||
| } | ||
| field.set(object, value); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В задании нужно сделать через метод setField() |
||
| field.setAccessible(false); | ||
| } | ||
| objByClassName.put(bean.getClassName(), object); | ||
| return object; | ||
| } | ||
| throw new InvalidConfigurationException("Invalid configuration"); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Вернуть объект по имени класса | ||
| * Например, Car car = (Car) container.getByClass("track.container.beans.Car") | ||
| */ | ||
| public Object getByClass(String className) { | ||
| return null; | ||
| public Object getByClass(String className) throws ClassNotFoundException, IllegalAccessException, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ТОже с сигнатурой |
||
| InstantiationException, InvalidConfigurationException, InvocationTargetException, NoSuchFieldException, | ||
| NoSuchMethodException { | ||
| if (beanByClassName.containsKey(className)) { | ||
| return getById(beanByClassName.get(className).getId()); | ||
| } | ||
| throw new InvalidConfigurationException("Invalid configuration"); | ||
| } | ||
|
|
||
| private Object stringToPrimitive(Type type, String value) { | ||
| switch (type.getTypeName()) { | ||
| case ("byte") : return Byte.parseByte(value); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А если Byte/Integer/Long? |
||
| case ("short") : return Short.parseShort(value); | ||
| case("int") : return Integer.parseInt(value); | ||
| case ("long") : return Long.parseLong(value); | ||
| case ("boolean") : return Boolean.parseBoolean(value); | ||
| case ("float") : return Float.parseFloat(value); | ||
| case ("double") : return Double.parseDouble(value); | ||
| case ("char") : return value.charAt(0); | ||
| default: return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,51 @@ | ||
| package track.container; | ||
|
|
||
| import java.io.File; | ||
| import java.util.List; | ||
| import java.io.IOException; | ||
| import java.util.*; | ||
|
|
||
| import track.container.config.Bean; | ||
| import track.container.config.ConfigReader; | ||
| import track.container.config.InvalidConfigurationException; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import track.container.config.*; | ||
|
|
||
| /** | ||
| * TODO: Реализовать | ||
| */ | ||
| public class JsonConfigReader implements ConfigReader { | ||
|
|
||
|
|
||
| @Override | ||
| public List<Bean> parseBeans(File configFile) throws InvalidConfigurationException { | ||
| return null; | ||
| List<Bean> beansList = new ArrayList<Bean>(); | ||
| try { | ||
| Iterator<JsonNode> nodeIterator = new ObjectMapper().readTree(configFile).path("beans").elements(); | ||
| while (nodeIterator.hasNext()) { | ||
| JsonNode currentNode = nodeIterator.next(); | ||
| beansList.add(new Bean(currentNode.path("id").asText(), currentNode.path("className").asText(), | ||
| asMap(currentNode.path("properties")))); | ||
|
|
||
| } | ||
| } catch (IOException e) { | ||
| throw new InvalidConfigurationException(e.getMessage()); | ||
| } | ||
| return beansList; | ||
| } | ||
|
|
||
| private Map<String, Property> asMap(JsonNode jsonNode) throws InvalidConfigurationException { | ||
| Map<String, Property> properties = new HashMap<>(); | ||
| Iterator<JsonNode> jsonNodeIterator = jsonNode.elements(); | ||
| while (jsonNodeIterator.hasNext()) { | ||
| JsonNode currentNode = jsonNodeIterator.next(); | ||
| if (currentNode.has("ref")) { | ||
| properties.put(currentNode.path("name").asText(),new Property(currentNode.path("name").asText(), | ||
| currentNode.path("ref").asText(), ValueType.REF)); | ||
| } else if (currentNode.has("val")) { | ||
| properties.put(currentNode.path("name").asText(), new Property(currentNode.path("name").asText(), | ||
| currentNode.path("val").asText(), ValueType.VAL)); | ||
| } else { | ||
| throw new InvalidConfigurationException("Invalid configuration"); | ||
| } | ||
| } | ||
| return properties; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
зачем?