summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorahaase2008-02-22 12:14:35 (EST)
committerahaase2008-02-22 12:14:35 (EST)
commit54f73c285509f6c0fc17c957b6ed6eb36903a0be (patch)
tree307610de1486f568057b9156c5167a4ad79f6980
parent8fec858243ba5681c62e92519ed32739d765a87a (diff)
downloadorg.eclipse.xpand-54f73c285509f6c0fc17c957b6ed6eb36903a0be.zip
org.eclipse.xpand-54f73c285509f6c0fc17c957b6ed6eb36903a0be.tar.gz
org.eclipse.xpand-54f73c285509f6c0fc17c957b6ed6eb36903a0be.tar.bz2
added conversion from and to arbitrary primitive types to their
canonical backend representation for all type systems
-rw-r--r--plugins/org.eclipse.xtend.backend.uml2types/src/org/eclipse/xtend/backend/types/uml2/internal/EnumType.java4
-rw-r--r--plugins/org.eclipse.xtend.backend.uml2types/src/org/eclipse/xtend/backend/types/uml2/internal/StereotypeType.java4
-rw-r--r--plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/common/Property.java2
-rw-r--r--plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/functions/java/internal/JavaBuiltinConverterFactory.java3
-rw-r--r--plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/AbstractProperty.java29
-rw-r--r--plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/AbstractType.java61
-rw-r--r--plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/builtin/BuiltinProperty.java21
-rw-r--r--plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/builtin/TypeType.java4
-rw-r--r--plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/java/internal/JavaBeansProperty.java42
-rw-r--r--plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/java/internal/JavaBeansType.java2
-rw-r--r--plugins/org.eclipse.xtend.middleend.old/src/org/eclipse/xtend/middleend/old/TypeToBackendType.java2
11 files changed, 117 insertions, 57 deletions
diff --git a/plugins/org.eclipse.xtend.backend.uml2types/src/org/eclipse/xtend/backend/types/uml2/internal/EnumType.java b/plugins/org.eclipse.xtend.backend.uml2types/src/org/eclipse/xtend/backend/types/uml2/internal/EnumType.java
index 004b835..f7a9a25 100644
--- a/plugins/org.eclipse.xtend.backend.uml2types/src/org/eclipse/xtend/backend/types/uml2/internal/EnumType.java
+++ b/plugins/org.eclipse.xtend.backend.uml2types/src/org/eclipse/xtend/backend/types/uml2/internal/EnumType.java
@@ -52,9 +52,9 @@ public final class EnumType extends AbstractType {
}
});
- register (new AbstractProperty (this, StringType.INSTANCE, "literal") {
+ register (new AbstractProperty (this, StringType.INSTANCE, String.class, "literal", false) {
@Override
- public Object get (ExecutionContext ctx, Object o) {
+ public Object getRaw (ExecutionContext ctx, Object o) {
if (o == null)
return null;
diff --git a/plugins/org.eclipse.xtend.backend.uml2types/src/org/eclipse/xtend/backend/types/uml2/internal/StereotypeType.java b/plugins/org.eclipse.xtend.backend.uml2types/src/org/eclipse/xtend/backend/types/uml2/internal/StereotypeType.java
index 4e8cfde..4469b44 100644
--- a/plugins/org.eclipse.xtend.backend.uml2types/src/org/eclipse/xtend/backend/types/uml2/internal/StereotypeType.java
+++ b/plugins/org.eclipse.xtend.backend.uml2types/src/org/eclipse/xtend/backend/types/uml2/internal/StereotypeType.java
@@ -134,11 +134,11 @@ public final class StereotypeType extends AbstractType {
private final class StereotypeProperty extends AbstractProperty {
public StereotypeProperty (BackendType type, String name) {
- super (StereotypeType.this, type, name);
+ super (StereotypeType.this, type, Object.class, name, false);
}
@Override
- public Object get (ExecutionContext ctx, Object target) {
+ public Object getRaw (ExecutionContext ctx, Object target) {
if (target instanceof Element) {
final Element ele = (Element) target;
diff --git a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/common/Property.java b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/common/Property.java
index 98b2825..1966030 100644
--- a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/common/Property.java
+++ b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/common/Property.java
@@ -23,4 +23,6 @@ public interface Property {
Object get (ExecutionContext ctx, Object o);
void set (ExecutionContext ctx, Object o, Object newValue);
+
+ boolean isWritable ();
}
diff --git a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/functions/java/internal/JavaBuiltinConverterFactory.java b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/functions/java/internal/JavaBuiltinConverterFactory.java
index f7c2c0f..1a0377d 100644
--- a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/functions/java/internal/JavaBuiltinConverterFactory.java
+++ b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/functions/java/internal/JavaBuiltinConverterFactory.java
@@ -65,6 +65,9 @@ public final class JavaBuiltinConverterFactory {
}
public static JavaBuiltinConverter getConverter (Class<?> cls) {
+ if (cls == null)
+ return NullConverter.INSTANCE;
+
final JavaBuiltinConverter resultRaw = _converters.get (cls);
if (resultRaw != null)
return resultRaw;
diff --git a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/AbstractProperty.java b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/AbstractProperty.java
index a155e2c..68dce8c 100644
--- a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/AbstractProperty.java
+++ b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/AbstractProperty.java
@@ -13,6 +13,8 @@ package org.eclipse.xtend.backend.types;
import org.eclipse.xtend.backend.common.BackendType;
import org.eclipse.xtend.backend.common.ExecutionContext;
import org.eclipse.xtend.backend.common.Property;
+import org.eclipse.xtend.backend.functions.java.internal.JavaBuiltinConverter;
+import org.eclipse.xtend.backend.functions.java.internal.JavaBuiltinConverterFactory;
/**
@@ -23,8 +25,10 @@ public abstract class AbstractProperty implements Property {
protected final BackendType _owner;
protected final BackendType _type;
protected final String _name;
-
- public AbstractProperty (BackendType owner, BackendType type, String name) {
+ protected final boolean _isWritable;
+ protected final JavaBuiltinConverter _converter;
+
+ public AbstractProperty (BackendType owner, BackendType type, Class<?> javaType, String name, boolean isWritable) {
if (owner == null)
throw new IllegalArgumentException ();
if (type == null)
@@ -33,6 +37,9 @@ public abstract class AbstractProperty implements Property {
_owner = owner;
_type = type;
_name = name;
+ _isWritable = isWritable;
+
+ _converter = JavaBuiltinConverterFactory.getConverter (javaType);
}
public String getName () {
@@ -47,11 +54,23 @@ public abstract class AbstractProperty implements Property {
return _type;
}
- public Object get (ExecutionContext ctx, Object o) {
- throw new IllegalStateException ("property " + _name + " of type " + _owner.getName() + " can not be read");
+ public final Object get (ExecutionContext ctx, Object o) {
+ return _converter.javaToBackend (getRaw (ctx, o));
}
- public void set (ExecutionContext ctx, Object o, Object newValue) {
+
+ public final void set (ExecutionContext ctx, Object o, Object newValue) {
+ setRaw (ctx, o, _converter.backendToJava (newValue));
+ }
+
+ protected abstract Object getRaw (ExecutionContext ctx, Object o);
+
+ @SuppressWarnings("unused")
+ public void setRaw (ExecutionContext ctx, Object o, Object newValue) {
throw new IllegalStateException ("property " + _name + " of type " + _owner.getName() + " can not be set");
}
+
+ public boolean isWritable () {
+ return _isWritable;
+ }
}
diff --git a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/AbstractType.java b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/AbstractType.java
index a9b67e2..c8e5016 100644
--- a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/AbstractType.java
+++ b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/AbstractType.java
@@ -21,12 +21,14 @@ import java.util.Map;
import org.eclipse.xtend.backend.common.BackendType;
import org.eclipse.xtend.backend.common.ExecutionContext;
+import org.eclipse.xtend.backend.common.ExpressionBase;
import org.eclipse.xtend.backend.common.Function;
import org.eclipse.xtend.backend.common.NamedFunction;
import org.eclipse.xtend.backend.common.Property;
import org.eclipse.xtend.backend.common.StaticProperty;
import org.eclipse.xtend.backend.types.builtin.ObjectType;
import org.eclipse.xtend.backend.types.builtin.VoidType;
+import org.eclipse.xtend.backend.util.StringHelper;
/**
@@ -64,6 +66,10 @@ public abstract class AbstractType implements BackendType {
protected void register (Property p) {
_properties.put (p.getName(), p);
+
+ register ("get" + StringHelper.firstUpper (p.getName()), new GetterOperation (p));
+ if (p.isWritable())
+ register ("set" + StringHelper.firstUpper (p.getName()), new SetterOperation (p));
}
protected void register (StaticProperty p) {
@@ -141,6 +147,61 @@ public abstract class AbstractType implements BackendType {
public String toString () {
return _name;
}
+
+ private class SetterOperation implements Function {
+ private final List<BackendType> _paramTypes = new ArrayList<BackendType> ();
+ private final Property _property;
+
+ public SetterOperation (Property property) {
+ _property = property;
+
+ _paramTypes.add (AbstractType.this);
+ _paramTypes.add (property.getType ());
+ }
+
+ public ExpressionBase getGuard () {
+ return null;
+ }
+
+ public List<? extends BackendType> getParameterTypes () {
+ return _paramTypes;
+ }
+
+ public Object invoke (ExecutionContext ctx, Object[] params) {
+ _property.set (ctx, params[0], params[1]);
+ return null;
+ }
+
+ public boolean isCached () {
+ return false;
+ }
+ }
+
+ private class GetterOperation implements Function {
+ private final List<BackendType> _paramTypes = new ArrayList<BackendType> ();
+ private final Property _property;
+
+ public GetterOperation (Property property) {
+ _property = property;
+ _paramTypes.add (AbstractType.this);
+ }
+
+ public ExpressionBase getGuard () {
+ return null;
+ }
+
+ public List<? extends BackendType> getParameterTypes () {
+ return _paramTypes;
+ }
+
+ public Object invoke (ExecutionContext ctx, Object[] params) {
+ return _property.get (ctx, params[0]);
+ }
+
+ public boolean isCached () {
+ return false;
+ }
+ }
}
diff --git a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/builtin/BuiltinProperty.java b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/builtin/BuiltinProperty.java
index 686a957..37a3224 100644
--- a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/builtin/BuiltinProperty.java
+++ b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/builtin/BuiltinProperty.java
@@ -14,8 +14,6 @@ import java.lang.reflect.Method;
import org.eclipse.xtend.backend.common.BackendType;
import org.eclipse.xtend.backend.common.ExecutionContext;
-import org.eclipse.xtend.backend.functions.java.internal.JavaBuiltinConverter;
-import org.eclipse.xtend.backend.functions.java.internal.JavaBuiltinConverterFactory;
import org.eclipse.xtend.backend.types.AbstractProperty;
import org.eclipse.xtend.backend.util.ErrorHandler;
@@ -28,15 +26,11 @@ public final class BuiltinProperty extends AbstractProperty {
private final Method _getter;
private final Method _setter;
- private final JavaBuiltinConverter _converter;
-
public BuiltinProperty (BackendType owner, BackendType type, String name, Method getter, Method setter) {
- super (owner, type, name);
+ super (owner, type, getJavaClassForProperty(getter, setter), name, setter != null);
_getter = getter;
_setter = setter;
-
- _converter = JavaBuiltinConverterFactory.getConverter (getJavaClassForProperty (getter, setter));
}
private static Class<?> getJavaClassForProperty (Method getter, Method setter) {
@@ -46,12 +40,9 @@ public final class BuiltinProperty extends AbstractProperty {
}
@Override
- public Object get (ExecutionContext ctx, Object o) {
- if (_getter == null)
- super.get (ctx, o);
-
+ public Object getRaw (ExecutionContext ctx, Object o) {
try {
- return _converter.javaToBackend (_getter.invoke(o));
+ return _getter.invoke(o);
} catch (Exception e) {
ErrorHandler.handle(e);
return null; // to make the compiler happy - this is never executed
@@ -59,12 +50,12 @@ public final class BuiltinProperty extends AbstractProperty {
}
@Override
- public void set (ExecutionContext ctx, Object o, Object newValue) {
+ public void setRaw (ExecutionContext ctx, Object o, Object newValue) {
if (_setter == null)
- super.set (ctx, o, newValue);
+ super.setRaw (ctx, o, newValue);
try {
- _setter.invoke (o, _converter.backendToJava (newValue));
+ _setter.invoke (o, newValue);
} catch (Exception e) {
ErrorHandler.handle(e);
}
diff --git a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/builtin/TypeType.java b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/builtin/TypeType.java
index d2d4385..8a64e5f 100644
--- a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/builtin/TypeType.java
+++ b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/builtin/TypeType.java
@@ -39,9 +39,9 @@ public final class TypeType extends AbstractType {
register (new BuiltinProperty (this, ListType.INSTANCE, "allProperties", ReflectionHelper.getKnownMethod (BackendType.class, "getProperties"), null));
register (new BuiltinProperty (this, ListType.INSTANCE, "allStaticProperties", ReflectionHelper.getKnownMethod (BackendType.class, "getStaticProperties"), null));
- register (new AbstractProperty (this, ListType.INSTANCE, "allOperations") {
+ register (new AbstractProperty (this, ListType.INSTANCE, java.util.List.class, "allOperations", false) {
@Override
- public Object get (ExecutionContext ctx, Object o) {
+ public Object getRaw (ExecutionContext ctx, Object o) {
final BackendType t = (BackendType) o;
final List<Object> result = new ArrayList<Object>();
result.addAll (ctx.getFunctionDefContext().getByFirstParameterType(t));
diff --git a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/java/internal/JavaBeansProperty.java b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/java/internal/JavaBeansProperty.java
index 0f827b0..11430a4 100644
--- a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/java/internal/JavaBeansProperty.java
+++ b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/java/internal/JavaBeansProperty.java
@@ -14,8 +14,7 @@ import java.beans.PropertyDescriptor;
import org.eclipse.xtend.backend.common.BackendType;
import org.eclipse.xtend.backend.common.ExecutionContext;
-import org.eclipse.xtend.backend.common.Property;
-import org.eclipse.xtend.backend.functions.java.internal.JavaBuiltinConverter;
+import org.eclipse.xtend.backend.types.AbstractProperty;
import org.eclipse.xtend.backend.util.ErrorHandler;
@@ -23,48 +22,33 @@ import org.eclipse.xtend.backend.util.ErrorHandler;
*
* @author Arno Haase (http://www.haase-consulting.com)
*/
-public final class JavaBeansProperty implements Property {
+public final class JavaBeansProperty extends AbstractProperty {
private final PropertyDescriptor _pd;
- private final BackendType _owner;
- private final BackendType _type;
- private final JavaBuiltinConverter _converter;
- public JavaBeansProperty (PropertyDescriptor pd, BackendType owner, BackendType type, JavaBuiltinConverter converter) {
+ public JavaBeansProperty (PropertyDescriptor pd, BackendType owner, BackendType type) {
+ super (owner, type, pd.getPropertyType(), pd.getName(), pd.getWriteMethod() != null);
+
_pd = pd;
- _owner = owner;
- _type = type;
- _converter = converter;
}
- public String getName () {
- return _pd.getName();
- }
-
- public BackendType getOwner () {
- return _owner;
- }
-
- public BackendType getType () {
- return _type;
- }
-
- public Object get (ExecutionContext ctx, Object o) {
+ @Override
+ public Object getRaw (ExecutionContext ctx, Object o) {
try {
- if (_pd.getReadMethod() == null)
- throw new IllegalArgumentException ("no readable property " + _pd.getName() + " for type " + _owner.getName());
- return _converter.javaToBackend (_pd.getReadMethod().invoke (o));
+ return _pd.getReadMethod().invoke (o);
} catch (Exception e) {
ErrorHandler.handle(e);
return null; // to make the compiler happy - this is never executed
}
}
- public void set (ExecutionContext ctx, Object o, Object newValue) {
+ @Override
+ public void setRaw (ExecutionContext ctx, Object o, Object newValue) {
try {
if (_pd.getWriteMethod() == null)
- throw new IllegalArgumentException ("no writeable property " + _pd.getName() + " for type " + _owner.getName());
- _pd.getWriteMethod().invoke (o, _converter.backendToJava (newValue));
+ super.setRaw (ctx, o, newValue);
+
+ _pd.getWriteMethod().invoke (o, newValue);
} catch (Exception e) {
ErrorHandler.handle (e);
}
diff --git a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/java/internal/JavaBeansType.java b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/java/internal/JavaBeansType.java
index c02b999..78b43f5 100644
--- a/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/java/internal/JavaBeansType.java
+++ b/plugins/org.eclipse.xtend.backend/src/org/eclipse/xtend/backend/types/java/internal/JavaBeansType.java
@@ -75,7 +75,7 @@ public final class JavaBeansType implements BackendType {
if (pd.getReadMethod().getDeclaringClass() == Object.class)
continue;
- _properties.put (pd.getName(), new JavaBeansProperty (pd, this, ts.getRootTypesystem().findType (pd.getPropertyType()), JavaBuiltinConverterFactory.getConverter (pd.getPropertyType ())));
+ _properties.put (pd.getName(), new JavaBeansProperty (pd, this, ts.getRootTypesystem().findType (pd.getPropertyType())));
}
// static properties
diff --git a/plugins/org.eclipse.xtend.middleend.old/src/org/eclipse/xtend/middleend/old/TypeToBackendType.java b/plugins/org.eclipse.xtend.middleend.old/src/org/eclipse/xtend/middleend/old/TypeToBackendType.java
index 3c172b1..852dc67 100644
--- a/plugins/org.eclipse.xtend.middleend.old/src/org/eclipse/xtend/middleend/old/TypeToBackendType.java
+++ b/plugins/org.eclipse.xtend.middleend.old/src/org/eclipse/xtend/middleend/old/TypeToBackendType.java
@@ -106,7 +106,7 @@ final class TypeToBackendType {
if (t instanceof EEnumType)
return convertEEnumType (t);
if (t instanceof EObjectType)
- return org.eclipse.xtend.backend.types.emf.EObjectType.INSTANCE;
+ return org.eclipse.xtend.backend.types.emf.internal.EObjectType.INSTANCE;
if (t instanceof JavaTypeImpl)
return convertJavaType (t);