package org.minimalj.model.properties;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Logger;

import org.minimalj.util.FieldUtils;

public class Properties {
	private static final Logger logger = Logger.getLogger(Properties.class.getName());

	private static final Map<Class<?>, Map<String, PropertyInterface>> properties = 
			new HashMap<Class<?>, Map<String, PropertyInterface>>();
	
	public static PropertyInterface getProperty(Class<?> clazz, String fieldName) {
		if (fieldName == null) throw new NullPointerException();

		Map<String, PropertyInterface> propertiesForClass = getProperties(clazz);
		PropertyInterface propertyInterface = propertiesForClass.get(fieldName);

		if (propertyInterface != null) {
			return propertyInterface;
		} else {
			logger.severe("No field/setMethod " + fieldName + " in Class " + clazz.getName());
			return null;
		}
	}

	public static PropertyInterface getProperty(Field field) {
		return getProperty(field.getDeclaringClass(), field.getName());
	}
	
	public static Map<String, PropertyInterface> getProperties(Class<?> clazz) {
		if (!properties.containsKey(clazz)) {
			properties.put(clazz, Collections.unmodifiableMap(properties(clazz)));
		}
		Map<String, PropertyInterface> propertiesForClass = properties.get(clazz);
		return propertiesForClass;
	}
	
	private static Map<String, PropertyInterface> properties(Class<?> clazz) {
		Map<String, PropertyInterface> properties = new LinkedHashMap<String, PropertyInterface>();
		
		for (Field field : clazz.getFields()) {
			if (!FieldUtils.isStatic(field)) {
				properties.put(field.getName(), new FieldProperty(field));
			} 
		}
		return properties; 
	}
	
}

@@ -196,24 +196,82 @@
 til.
-logging.Logger;%0A
+Objects;%0Aimport java.util.logging.Logger;%0A%0Aimport org.minimalj.model.Keys;
 %0Aimp
@@ -628,21 +628,24 @@
  String 
-field
+property
 Name) %7B%0A
@@ -650,62 +650,43 @@
 %7B%0A%09%09
-if (fieldName == null) throw new NullPointerException(
+Objects.requireNonNull(propertyName
 );%0A%0A
@@ -789,25 +789,16 @@
 property
-Interface
  = prope
@@ -815,21 +815,24 @@
 ass.get(
-field
+property
 Name);%0A%0A
@@ -845,25 +845,108 @@
 property
-Interface
+ == null) %7B%0A%09%09%09property = Keys.getMethodProperty(clazz, propertyName);%0A%09%09%7D%0A%09%09%0A%09%09if (property
  != null
@@ -967,25 +967,16 @@
 property
-Interface
 ;%0A%09%09%7D el
@@ -1011,27 +1011,39 @@
 eld/
-setMethod %22 + field
+access methods for %22 + property
 Name

support MethodProperties