AI2 Component  (Version nb184)
PropertyUtil.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 package com.google.appinventor.components.runtime.util;
7 
11 
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 
20 public class PropertyUtil {
21 
29  public static Component copyComponentProperties(Component source, Component target)
30  throws Throwable {
31  if (!source.getClass().equals(target.getClass())) {
32  throw new IllegalArgumentException("Source and target classes must be identical");
33  }
34 
35  final Class componentClass = source.getClass();
36  final Method[] componentMethods = componentClass.getMethods();
37  for (Method componentMethod : componentMethods) {
38  // A SimpleProperty which takes a single argument is a property setter.
39  if (componentMethod.isAnnotationPresent(SimpleProperty.class)
40  && componentMethod.getParameterTypes().length == 1) {
41  final Method propertySetterMethod = componentMethod;
42  try {
43  final String propertyName = propertySetterMethod.getName();
44 
45  // Look for a property copier method.
46  final Method propertyCopierMethod =
47  getPropertyCopierMethod("Copy" + propertyName, componentClass);
48  if (propertyCopierMethod != null) {
49  propertyCopierMethod.invoke(target, source);
50  continue; // Don't return here because we still need to copy more properties.
51  }
52  // It's fine if there's no copy method. We'll look for a getter.
53 
54  // The getter will have the same name as the setter
55  final Method propertyGetterMethod = componentClass.getMethod(propertyName);
56  final Class propertySetterParameterType = propertySetterMethod.getParameterTypes()[0];
57  // The getter also needs to be a SimpleProperty and its return type needs to be
58  // compatible with the setter's single argument type
59  if (propertyGetterMethod.isAnnotationPresent(SimpleProperty.class) &&
60  propertySetterParameterType.isAssignableFrom(propertyGetterMethod.getReturnType())) {
61  final Object propertyValue = propertyGetterMethod.invoke(source);
62  propertySetterMethod.invoke(target, propertyValue);
63  }
64  } catch (NoSuchMethodException e) {
65  // It's fine if there's no getter method. We just won't invoke the setter.
66  continue;
67  } catch (InvocationTargetException e2) {
68  // This re-throws any Exceptions generated by the property getters or setters themselves.
69  throw e2.getCause();
70  }
71  }
72  }
73  return target;
74  }
75 
76  private static Method getPropertyCopierMethod(String copierMethodName, Class componentClass) {
77  // If the copier method is declared in a superclass of componentClass, the parameter
78  // would be the superclass also.
79  // For example, componentClass might be Button. But the CopyWidth method is declared in
80  // AndroidViewComponent and the paremeter is also AndroidViewComponent.
81  // So, we may need to look up the superclass chain to
82  do {
83  try {
84  Method propertyCopierMethod = componentClass.getMethod(copierMethodName, componentClass);
85  if (propertyCopierMethod.isAnnotationPresent(SimplePropertyCopier.class)) {
86  return propertyCopierMethod;
87  }
88  } catch (NoSuchMethodException e) {
89  // It's fine if there's no copier method. We'll try the superclass.
90  }
91  componentClass = componentClass.getSuperclass();
92  } while (componentClass != null);
93 
94  // It's fine if we didn't find a copier method. Just return null;
95  return null;
96  }
97 }
com.google.appinventor.components
com.google.appinventor.components.runtime.util.PropertyUtil
Definition: PropertyUtil.java:20
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.util.PropertyUtil.copyComponentProperties
static Component copyComponentProperties(Component source, Component target)
Definition: PropertyUtil.java:29
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google
com
com.google.appinventor.components.annotations.SimplePropertyCopier
Definition: SimplePropertyCopier.java:24
com.google.appinventor.components.annotations
com.google.appinventor