]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/reflection/BindingRequest.java
Cherry-picked changes to org.simantics.objmap2 from master.
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / reflection / BindingRequest.java
index 51feb39c3815861e4edde721cb4970ddf33898c0..55f809f5138dc2a02bd7b6a57bee7189a7d0101d 100644 (file)
@@ -8,12 +8,13 @@
  *
  * Contributors:
  *     VTT Technical Research Centre of Finland - initial API and implementation
- *     Semantum Oy - gitlab #82
+ *     Semantum Oy - gitlab #82, gitlab #313
  *******************************************************************************/
 package org.simantics.databoard.binding.reflection;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -44,6 +45,13 @@ public class BindingRequest {
        return new BindingRequest(fieldClass, annotations);
        }
        
+    public static BindingRequest create( Method method )
+    {
+        Annotation[] annotations = ClassBindingFactory.getMethodAnnotations(method);
+        Class<?> valueClass = method.getReturnType(); 
+        return new BindingRequest(valueClass, annotations);
+    }
+       
        /** Requested class */
     private Class<?> clazz;
     private ClassLoader cl;
@@ -60,6 +68,23 @@ public class BindingRequest {
     
     transient int hash;
 
+    /**
+     * Cloning constructor with replacement annotations.
+     * 
+     * @param other the request to clone
+     * @param annotations the annotations to use while cloning
+     */
+    private BindingRequest(BindingRequest other, Annotation...annotations)
+    {
+        this.clazz = other.clazz;
+        this.cl = other.cl;
+        this.annotations = annotations;
+        this.className = other.className;
+        this.signature = other.signature;
+        this.descriptor = other.descriptor;
+        hash = calcHash(clazz.getName());
+    }
+
     /**
      * Create BindingRequest that creates class lazily. 
      * 
@@ -76,10 +101,7 @@ public class BindingRequest {
        this.signature = classSignature;
        this.annotations = annotations;
        this.descriptor = classDescriptor;
-        hash = className.hashCode();
-        for (Annotation a : annotations) {
-            hash = 7*hash + a.hashCode();
-        }
+       hash = calcHash(className);
     }
 
     /**
@@ -114,17 +136,23 @@ public class BindingRequest {
         
         className = clazz.getCanonicalName();
         signature = getSignature(clazz);
-        List<Class<?>> args = createArgsList();
-        StringBuilder desc = new StringBuilder();
-        _buildDescriptor(desc, clazz, args, new MutableInteger(0));
-        descriptor = desc.toString();
-        hash = clazz.getName().hashCode();
-        for (Annotation a : annotations) {
-            hash = 7*hash + a.hashCode();
+        descriptor = _buildDescriptor(new StringBuilder(), clazz, createArgsList(), new MutableInteger(0)).toString();
+        hash = calcHash(clazz.getName());
+    }
+
+    public BindingRequest withAnnotations(Annotation... newAnnotations) {
+        return new BindingRequest(this, newAnnotations);
+    }
+
+    private int calcHash(String className) {
+        int hash = className.hashCode();
+        for (Annotation a : this.annotations) {
+            hash += a.hashCode();
         }
+        return hash;
     }
-    
-    private void _buildDescriptor(StringBuilder sb, Class<?> c, List<Class<?>> classes, MutableInteger pos)
+
+    private StringBuilder _buildDescriptor(StringBuilder sb, Class<?> c, List<Class<?>> classes, MutableInteger pos)
     {
        int genericCount = c.getTypeParameters().length;
        int genericsLeft = classes.size()-pos.value;
@@ -142,6 +170,7 @@ public class BindingRequest {
        } else {
                sb.append( getSignature(c) );
        }
+       return sb;
     }
 
     public BindingRequest(Class<?> clazz, List<Annotation> annotations)