summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsefftinge2008-03-12 05:53:20 (EDT)
committer sefftinge2008-03-12 05:53:20 (EDT)
commit2062d73c6077794652c2c6c98db483295a1ee887 (patch)
tree27821f50695dc90532d476c7cb853b51e875ebb9
parent5b33b350710dd272953bb0839b99fc1437ae8734 (diff)
downloadorg.eclipse.xpand-2062d73c6077794652c2c6c98db483295a1ee887.zip
org.eclipse.xpand-2062d73c6077794652c2c6c98db483295a1ee887.tar.gz
org.eclipse.xpand-2062d73c6077794652c2c6c98db483295a1ee887.tar.bz2
static type system
-rw-r--r--plugins/org.eclipse.xpand3/META-INF/MANIFEST.MF15
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/DeclarationsContributor.java49
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/GenericsUtil.java73
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/LanguageSpecificTypeSystemFactory.java (renamed from plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/LanguageSpecificDeclarationContributorFactory.java)4
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/TypeSystem.java36
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/TypeSystemFactory.java32
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/AbstractTypeSystemImpl.java53
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/CompositeTypeSystemImpl.java89
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/TypeSystemImpl.java98
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/builtin/BuiltinTypeSystem.java80
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaDeclarationsContributor.java83
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystem.java295
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystemFactory.java59
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/xpand3/Xpand3ResourceDeclarationsContributor.java30
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/util/CreateCache.java46
-rw-r--r--tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/TypeSystemTest.java18
-rw-r--r--tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/typesystem/java/GenericsBean.java32
-rw-r--r--tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystemTest.java77
-rw-r--r--tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/typesystem/java/SimpleBean.java33
19 files changed, 741 insertions, 461 deletions
diff --git a/plugins/org.eclipse.xpand3/META-INF/MANIFEST.MF b/plugins/org.eclipse.xpand3/META-INF/MANIFEST.MF
index 9359613..1055976 100644
--- a/plugins/org.eclipse.xpand3/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.xpand3/META-INF/MANIFEST.MF
@@ -13,21 +13,28 @@ Require-Bundle: org.antlr.patched;bundle-version="3.0.0",
org.eclipse.xtend.backend;bundle-version="0.7.0",
org.junit,
org.eclipse.emf.ecore.xmi;bundle-version="2.3.0"
-Export-Package: org.eclipse.xpand3.analyzation,
- org.eclipse.xpand3,
- org.eclipse.xpand3.analyzation.typesystem,
+Export-Package: org.eclipse.xpand3,
+ org.eclipse.xpand3.analyzation,
org.eclipse.xpand3.analyzation.typesystem.builtin,
+ org.eclipse.xpand3.analyzation.typesystem.java,
+ org.eclipse.xpand3.analyzation.typesystem.util,
+ org.eclipse.xpand3.analyzation.typesystem.xpand3,
+ org.eclipse.xpand3.ast,
org.eclipse.xpand3.declaration,
org.eclipse.xpand3.declaration.impl,
org.eclipse.xpand3.declaration.util,
+ org.eclipse.xpand3.declaration.validation,
org.eclipse.xpand3.expression,
org.eclipse.xpand3.expression.impl,
org.eclipse.xpand3.expression.util,
+ org.eclipse.xpand3.expression.validation,
org.eclipse.xpand3.impl,
org.eclipse.xpand3.statement,
org.eclipse.xpand3.statement.impl,
org.eclipse.xpand3.statement.util,
+ org.eclipse.xpand3.statement.validation,
org.eclipse.xpand3.staticTypesystem,
org.eclipse.xpand3.staticTypesystem.impl,
org.eclipse.xpand3.staticTypesystem.util,
- org.eclipse.xpand3.util
+ org.eclipse.xpand3.util,
+ org.eclipse.xpand3.validation
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/DeclarationsContributor.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/DeclarationsContributor.java
deleted file mode 100644
index cef4752..0000000
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/DeclarationsContributor.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/**
- * <copyright>
- *
- * Copyright (c) 2002-2007 itemis AG and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * itemis AG - Initial API and implementation
- *
- * </copyright>
- *
- */
-package org.eclipse.xpand3.analyzation;
-
-import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
-import org.eclipse.xpand3.staticTypesystem.DeclaredFunction;
-import org.eclipse.xpand3.staticTypesystem.DeclaredType;
-
-/**
- * @author Sven Efftinge
- *
- */
-public interface DeclarationsContributor {
-
- /**
- * this method is invoked during setup of this contributor.
- * @param the type system to be used for resolving type and function references.
- */
- void setTypeSystemFactory(TypeSystemFactory tsf);
-
- /**
- * if this contributor has a type with the declared name, the respective DeclareTpye should be returned.
- * This method is only invoked once per name, so caching is done by the framework
- * @param name
- * @return the declared type with the given name or null if there is no such type declared in this Resource.
- */
- DeclaredType typeForName(String name);
-
- /**
- * if this contributor has a type with the declared name, the respective DeclareTpye should be returned.
- * This method is only invoked once per name.
- * @param name
- * @return
- */
- DeclaredFunction functionForName(String name, AbstractTypeReference...parameterTypes);
-}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/GenericsUtil.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/GenericsUtil.java
new file mode 100644
index 0000000..7b1c050
--- /dev/null
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/GenericsUtil.java
@@ -0,0 +1,73 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2002-2007 itemis AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * itemis AG - Initial API and implementation
+ *
+ * </copyright>
+ *
+ */
+package org.eclipse.xpand3.analyzation;
+
+import java.util.Arrays;
+
+import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
+import org.eclipse.xpand3.staticTypesystem.DeclaredFunction;
+import org.eclipse.xpand3.staticTypesystem.DeclaredType;
+import org.eclipse.xpand3.staticTypesystem.FunctionType;
+import org.eclipse.xpand3.staticTypesystem.StaticTypesystemFactory;
+import org.eclipse.xpand3.staticTypesystem.Type;
+import org.eclipse.xpand3.staticTypesystem.VoidType;
+import org.eclipse.xpand3.staticTypesystem.WildcardType;
+
+/**
+ * @author Sven Efftinge
+ *
+ */
+public class GenericsUtil {
+ private final static StaticTypesystemFactory FACTORY = StaticTypesystemFactory.eINSTANCE;
+ /**
+ *
+ */
+ private static final VoidType VOID_TYPE = FACTORY.createVoidType();
+
+ public static WildcardType wildCardWithLower(AbstractTypeReference... lowerBounds) {
+ WildcardType wildCard = wildCard();
+ for (AbstractTypeReference abstractTypeReference : lowerBounds) {
+ wildCard.getLowerBounds().add(abstractTypeReference);
+ }
+ return wildCard;
+ }
+
+ public static WildcardType wildCard(AbstractTypeReference... upperBounds) {
+ WildcardType wildCard = FACTORY.createWildcardType();
+ for (AbstractTypeReference abstractTypeReference : upperBounds) {
+ wildCard.getUpperBounds().add(abstractTypeReference);
+ }
+ return wildCard;
+ }
+
+ public static Type typeRef(DeclaredType declaredType, AbstractTypeReference... actualArguments) {
+ Type type = FACTORY.createType();
+ type.setDeclaredType(declaredType);
+ type.getActualTypeArguments().addAll(Arrays.asList(actualArguments));
+ return type;
+ }
+
+ public static VoidType voidType() {
+ return VOID_TYPE;
+ }
+
+ public static FunctionType functionTypeRef(DeclaredFunction declaredFunction, AbstractTypeReference... actualArguments) {
+ FunctionType type = FACTORY.createFunctionType();
+ type.setDeclaredFunction(declaredFunction);
+ type.getActualTypeArguments().addAll(Arrays.asList(actualArguments));
+ return type;
+ }
+}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/LanguageSpecificDeclarationContributorFactory.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/LanguageSpecificTypeSystemFactory.java
index 3dbabc5..a8ee915 100644
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/LanguageSpecificDeclarationContributorFactory.java
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/LanguageSpecificTypeSystemFactory.java
@@ -20,7 +20,7 @@ package org.eclipse.xpand3.analyzation;
* @author Sven Efftinge
*
*/
-public interface LanguageSpecificDeclarationContributorFactory {
+public interface LanguageSpecificTypeSystemFactory {
/**
* gives the middle end a way to declare if it can and wants to handle a given
* source file / resource. If and only if it returns true, it will be asked for
@@ -33,5 +33,5 @@ public interface LanguageSpecificDeclarationContributorFactory {
* @param resourceName
* @return
*/
- DeclarationsContributor createDeclarationContributor(String resourceName);
+ TypeSystem createTypeSystem(String resourceName);
}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/TypeSystem.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/TypeSystem.java
index 0975569..264014b 100644
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/TypeSystem.java
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/TypeSystem.java
@@ -15,13 +15,10 @@
*/
package org.eclipse.xpand3.analyzation;
-import java.util.List;
-
import org.eclipse.xpand3.analyzation.typesystem.builtin.BuiltinTypeSystem;
import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
-import org.eclipse.xpand3.staticTypesystem.FunctionType;
-import org.eclipse.xpand3.staticTypesystem.Type;
-import org.eclipse.xpand3.staticTypesystem.WildcardType;
+import org.eclipse.xpand3.staticTypesystem.DeclaredFunction;
+import org.eclipse.xpand3.staticTypesystem.DeclaredType;
/**
* @author Sven Efftinge
@@ -35,6 +32,7 @@ public interface TypeSystem {
final String COLLECTION = "Collection";
final String SET = "Set";
final String LIST = "List";
+ final String MAP = "Map";
// Datatypes
final String BOOLEAN = "Boolean";
@@ -45,18 +43,32 @@ public interface TypeSystem {
// reflection layer types
final String FEATURE = "Feature";
final String TYPE = "Type";
- final String OPERATION = "Operation";
+ final String FUNCTION = "Function";
final String PROPERTY = "Property";
final String STATIC_PROPERTY = "StaticProperty";
- final TypeSystem BUILTIN_TYPESYSTEM = new BuiltinTypeSystem(null);
+ final BuiltinTypeSystem BUILTIN_TYPESYSTEM = new BuiltinTypeSystem();
- WildcardType wildCard(AbstractTypeReference...upperBounds);
- WildcardType wildCardWithLower(AbstractTypeReference...lowerBounds);
+ /**
+ * if this contributor has a type with the declared name, the respective DeclareTpye should be returned.
+ * This method is only invoked once per name, so caching is done by the framework
+ * @param name
+ * @return the declared type with the given name or null if there is no such type declared in this Resource.
+ */
+ DeclaredType typeForName(String name);
- Type typeForName(String name, AbstractTypeReference...typeArguments);
+ /**
+ * if this contributor has a type with the declared name, the respective DeclareTpye should be returned.
+ * This method is only invoked once per name.
+ * @param name
+ * @return
+ */
+ DeclaredFunction functionForName(String name, AbstractTypeReference...parameterTypes);
- FunctionType functionForNameAndParameterTypes(String name, AbstractTypeReference...parameterTypes);
- FunctionType functionForName(String name, List<AbstractTypeReference> parameterTypes, AbstractTypeReference...typeArguments);
+ /**
+ * this method is invoked during setup of this contributor.
+ * @param the type system to be used for resolving type and function references.
+ */
+ void setTypeSystemFactory(TypeSystemFactory tsf);
}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/TypeSystemFactory.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/TypeSystemFactory.java
index f9247bc..6ebe79c 100644
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/TypeSystemFactory.java
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/TypeSystemFactory.java
@@ -18,7 +18,6 @@ package org.eclipse.xpand3.analyzation;
import java.util.HashSet;
import java.util.Set;
-import org.eclipse.xpand3.analyzation.typesystem.TypeSystemImpl;
import org.eclipse.xpand3.analyzation.typesystem.builtin.BuiltinTypeSystem;
import org.eclipse.xtend.backend.util.Cache;
@@ -30,36 +29,31 @@ import org.eclipse.xtend.backend.util.Cache;
*/
public class TypeSystemFactory {
- private final static Set<LanguageSpecificDeclarationContributorFactory> factories = new HashSet<LanguageSpecificDeclarationContributorFactory>();
- public static void registerLanguageSpecificFactory(LanguageSpecificDeclarationContributorFactory factory) {
+ private static Set<LanguageSpecificTypeSystemFactory> factories = new HashSet<LanguageSpecificTypeSystemFactory>();
+
+ public void registerLanguageSpecificFactory(LanguageSpecificTypeSystemFactory factory) {
factories.add(factory);
}
private final Cache<String, TypeSystem> cache = new Cache<String, TypeSystem>() {
@Override
- protected TypeSystem create(String key) {
- DeclarationsContributor decl = createDeclarationContributor(key);
- decl.setTypeSystemFactory(TypeSystemFactory.this);
- TypeSystem ts = new TypeSystemImpl(decl);
- return ts;
+ protected TypeSystem create(String namespace) {
+ for (LanguageSpecificTypeSystemFactory factory : factories) {
+ if (factory.canHandle(namespace)) {
+ TypeSystem typeSystem = factory.createTypeSystem(namespace);
+ typeSystem.setTypeSystemFactory(TypeSystemFactory.this);
+ return typeSystem;
+ }
+ }
+ return null;
}};
public TypeSystem getTypeSystem(String resourceName) {
return cache.get(resourceName);
}
- public TypeSystem getBuiltInTypeSystem() {
+ public BuiltinTypeSystem getBuiltInTypeSystem() {
return BuiltinTypeSystem.BUILTIN_TYPESYSTEM;
}
- public DeclarationsContributor createDeclarationContributor(String namespace) {
- for (LanguageSpecificDeclarationContributorFactory factory : factories) {
- if (factory.canHandle(namespace)) {
- return factory.createDeclarationContributor(namespace);
- }
- }
- return null;
- }
-
-
}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/AbstractTypeSystemImpl.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/AbstractTypeSystemImpl.java
deleted file mode 100644
index de33f8f..0000000
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/AbstractTypeSystemImpl.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/**
- * <copyright>
- *
- * Copyright (c) 2002-2007 itemis AG and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * itemis AG - Initial API and implementation
- *
- * </copyright>
- *
- */
-package org.eclipse.xpand3.analyzation.typesystem;
-
-import org.eclipse.xpand3.analyzation.TypeSystem;
-import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
-import org.eclipse.xpand3.staticTypesystem.StaticTypesystemFactory;
-import org.eclipse.xpand3.staticTypesystem.WildcardType;
-
-/**
- * @author Sven Efftinge
- *
- */
-public abstract class AbstractTypeSystemImpl implements TypeSystem {
-
- protected StaticTypesystemFactory FACTORY = StaticTypesystemFactory.eINSTANCE;
- /*
- * (non-Javadoc)
- * @see org.eclipse.xand3.analyzation.TypeSystem#wildCardWithLower(org.eclipse.xpand3.staticTypesystem.AbstractTypeReference[])
- */
- public WildcardType wildCardWithLower(AbstractTypeReference... lowerBounds) {
- WildcardType wildCard = wildCard();
- for (AbstractTypeReference abstractTypeReference : lowerBounds) {
- wildCard.getLowerBounds().add(abstractTypeReference);
- }
- return wildCard;
- }
-
- /*
- * (non-Javadoc)
- * @see org.eclipse.xand3.analyzation.TypeSystem#wildCard(org.eclipse.xpand3.staticTypesystem.AbstractTypeReference[])
- */
- public WildcardType wildCard(AbstractTypeReference... upperBounds) {
- WildcardType wildCard = FACTORY.createWildcardType();
- for (AbstractTypeReference abstractTypeReference : upperBounds) {
- wildCard.getLowerBounds().add(abstractTypeReference);
- }
- return wildCard;
- }
-}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/CompositeTypeSystemImpl.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/CompositeTypeSystemImpl.java
deleted file mode 100644
index 6e7d017..0000000
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/CompositeTypeSystemImpl.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/**
- * <copyright>
- *
- * Copyright (c) 2002-2007 itemis AG and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * itemis AG - Initial API and implementation
- *
- * </copyright>
- *
- */
-package org.eclipse.xpand3.analyzation.typesystem;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import org.eclipse.xpand3.analyzation.TypeSystem;
-import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
-import org.eclipse.xpand3.staticTypesystem.FunctionType;
-import org.eclipse.xpand3.staticTypesystem.Type;
-
-/**
- * @author Sven Efftinge
- *
- */
-public class CompositeTypeSystemImpl extends AbstractTypeSystemImpl implements TypeSystem {
- private List<TypeSystem> delegates = null;
-
- public CompositeTypeSystemImpl() {
- }
-
- public void addTypeSystem(TypeSystem ts) {
- this.delegates.add(ts);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.xand3.analyzation.TypeSystem#functionForName(java.lang.String,
- * java.util.List,
- * org.eclipse.xpand3.staticTypesystem.AbstractTypeReference[])
- */
- public FunctionType functionForName(String name,
- List<AbstractTypeReference> parameterTypes,
- AbstractTypeReference... typeArguments) {
- List<FunctionType> possibilities = new ArrayList<FunctionType>();
- for (TypeSystem ts : delegates) {
- FunctionType functionType = ts.functionForName(name, parameterTypes, typeArguments);
- if (functionType!=null)
- possibilities.add(functionType);
- }
- if (possibilities.isEmpty())
- return null;
- //TODO sort by specialization
- return possibilities.get(0);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.xand3.analyzation.TypeSystem#functionForNameAndParameterTypes(java.lang.String,
- * org.eclipse.xpand3.staticTypesystem.AbstractTypeReference[])
- */
- public FunctionType functionForNameAndParameterTypes(String name,
- AbstractTypeReference... parameterTypes) {
- return functionForName(name, Arrays.asList(parameterTypes));
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.xand3.analyzation.TypeSystem#typeForName(java.lang.String,
- * org.eclipse.xpand3.staticTypesystem.AbstractTypeReference[])
- */
- public Type typeForName(String name, AbstractTypeReference... typeArguments) {
- for (TypeSystem ts : delegates) {
- Type type = ts.typeForName(name, typeArguments);
- if (type!=null)
- return type;
- }
- return null;
- }
-
-}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/TypeSystemImpl.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/TypeSystemImpl.java
deleted file mode 100644
index 0917923..0000000
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/TypeSystemImpl.java
+++ /dev/null
@@ -1,98 +0,0 @@
-/**
- * <copyright>
- *
- * Copyright (c) 2002-2007 itemis AG and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * itemis AG - Initial API and implementation
- *
- * </copyright>
- *
- */
-package org.eclipse.xpand3.analyzation.typesystem;
-
-import java.util.Arrays;
-import java.util.List;
-
-import org.eclipse.xpand3.analyzation.DeclarationsContributor;
-import org.eclipse.xpand3.analyzation.TypeSystem;
-import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
-import org.eclipse.xpand3.staticTypesystem.DeclaredFunction;
-import org.eclipse.xpand3.staticTypesystem.DeclaredType;
-import org.eclipse.xpand3.staticTypesystem.FunctionType;
-import org.eclipse.xpand3.staticTypesystem.Type;
-
-/**
- * @author Sven Efftinge
- *
- */
-public class TypeSystemImpl extends AbstractTypeSystemImpl implements TypeSystem {
-
-
- private DeclarationsContributor contr = null;
-
- /**
- *
- */
- public TypeSystemImpl(DeclarationsContributor contributor) {
- if (contributor==null)
- throw new NullPointerException("contributor was null");
- this.contr = contributor;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.xand3.analyzation.TypeSystem#typeForName(java.lang.String,
- * org.eclipse.xpand3.staticTypesystem.AbstractTypeReference[])
- */
- public Type typeForName(String name, AbstractTypeReference... typeArguments) {
- DeclaredType dt = contr.typeForName(name);
- if (dt != null) {
- //TODO CACHING
- Type t = FACTORY.createType();
- t.setDeclaredType(dt);
- t.getActualTypeArguments().addAll(Arrays.asList(typeArguments));
- return t;
- }
- return null;
- }
-
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.xand3.analyzation.TypeSystem#functionForName(java.lang.String,
- * java.util.List,
- * org.eclipse.xpand3.staticTypesystem.AbstractTypeReference[])
- */
- public FunctionType functionForName(String name,
- List<AbstractTypeReference> parameterTypes,
- AbstractTypeReference... typeArguments) {
- DeclaredFunction func = contr.functionForName(name, parameterTypes.toArray(new AbstractTypeReference[parameterTypes.size()]));
- if (func == null) {
- //TODO CACHING
- FunctionType funcType = FACTORY.createFunctionType();
- funcType.setDeclaredFunction(func);
- funcType.getActualTypeArguments().addAll(Arrays.asList(typeArguments));
- return funcType;
- }
- return null;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.xand3.analyzation.TypeSystem#functionForNameAndParameterTypes(java.lang.String,
- * org.eclipse.xpand3.staticTypesystem.AbstractTypeReference[])
- */
- public FunctionType functionForNameAndParameterTypes(String name,
- AbstractTypeReference... parameterTypes) {
- return functionForName(name, Arrays.asList(parameterTypes));
- }
-
-}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/builtin/BuiltinTypeSystem.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/builtin/BuiltinTypeSystem.java
index 3d93977..0e04953 100644
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/builtin/BuiltinTypeSystem.java
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/builtin/BuiltinTypeSystem.java
@@ -17,30 +17,35 @@ package org.eclipse.xpand3.analyzation.typesystem.builtin;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Collection;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
+import java.util.Set;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
-import org.eclipse.xpand3.analyzation.DeclarationsContributor;
import org.eclipse.xpand3.analyzation.TypeSystem;
import org.eclipse.xpand3.analyzation.TypeSystemFactory;
-import org.eclipse.xpand3.analyzation.typesystem.TypeSystemImpl;
import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
import org.eclipse.xpand3.staticTypesystem.DeclaredFunction;
import org.eclipse.xpand3.staticTypesystem.DeclaredType;
import org.eclipse.xpand3.staticTypesystem.Model;
import org.eclipse.xpand3.staticTypesystem.StaticTypesystemPackage;
import org.eclipse.xpand3.util.LoaderFactory;
+import org.eclipse.xtend.backend.common.BackendType;
+import org.eclipse.xtend.backend.common.Function;
+import org.eclipse.xtend.backend.common.Property;
+import org.eclipse.xtend.backend.common.StaticProperty;
/**
* @author Sven Efftinge
*
*/
-public class BuiltinTypeSystem extends TypeSystemImpl implements TypeSystem {
+public class BuiltinTypeSystem implements TypeSystem {
private static Map<String, DeclaredType> types = new HashMap<String, DeclaredType>();
static {
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
@@ -59,25 +64,62 @@ public class BuiltinTypeSystem extends TypeSystemImpl implements TypeSystem {
throw new RuntimeException(e);
}
}
- /**
- * @param contributor
- */
- public BuiltinTypeSystem(DeclarationsContributor contributor) {
- super(new DeclarationsContributor() {
+
+ public DeclaredType getBuiltinTypeForJavaClass(Class<?> cls) {
+ if (List.class.isAssignableFrom (cls))
+ return types.get(LIST);
+ if (Set.class.isAssignableFrom(cls))
+ return types.get(SET);
+ if (Collection.class.isAssignableFrom(cls))
+ return types.get(COLLECTION);
+
+ if (Map.class.isAssignableFrom(cls))
+ return types.get(MAP);
+
+ if (CharSequence.class.isAssignableFrom(cls))
+ return types.get(STRING);
+
+ if (cls == Boolean.class || cls == Boolean.TYPE)
+ return types.get(BOOLEAN);
+
+ if (cls == Long.class || cls == Long.TYPE)
+ return types.get(INTEGER);
+ if (cls == Double.class || cls == Double.TYPE)
+ return types.get(REAL);
- public DeclaredFunction functionForName(String name,
- AbstractTypeReference... parameterTypes) {
- return null;
- }
+ if (Function.class.isAssignableFrom(cls))
+ return types.get(FUNCTION);
+
+ if (BackendType.class.isAssignableFrom(cls))
+ return types.get(TYPE);
+ if (Property.class.isAssignableFrom(cls))
+ return types.get(PROPERTY);
+ if (StaticProperty.class.isAssignableFrom(cls))
+ return types.get(STATIC_PROPERTY);
+ if (Object.class.equals(cls))
+ return types.get(OBJECT);
+ return null;
+ }
- public void setTypeSystemFactory(TypeSystemFactory ts) {
- }
+ /* (non-Javadoc)
+ * @see org.eclipse.xpand3.analyzation.TypeSystem#functionForName(java.lang.String, org.eclipse.xpand3.staticTypesystem.AbstractTypeReference[])
+ */
+ public DeclaredFunction functionForName(String name,
+ AbstractTypeReference... parameterTypes) {
+ return null;
+ }
- public DeclaredType typeForName(String name) {
- return types.get(name);
- }
-
- });
+ /* (non-Javadoc)
+ * @see org.eclipse.xpand3.analyzation.TypeSystem#setTypeSystemFactory(org.eclipse.xpand3.analyzation.TypeSystemFactory)
+ */
+ public void setTypeSystemFactory(TypeSystemFactory tsf) {
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.xpand3.analyzation.TypeSystem#typeForName(java.lang.String)
+ */
+ public DeclaredType typeForName(String name) {
+ return types.get(name);
}
}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaDeclarationsContributor.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaDeclarationsContributor.java
deleted file mode 100644
index 7dbbf91..0000000
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaDeclarationsContributor.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/**
- * <copyright>
- *
- * Copyright (c) 2002-2007 itemis AG and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * itemis AG - Initial API and implementation
- *
- * </copyright>
- *
- */
-package org.eclipse.xpand3.analyzation.typesystem.java;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.eclipse.xpand3.analyzation.DeclarationsContributor;
-import org.eclipse.xpand3.analyzation.TypeSystem;
-import org.eclipse.xpand3.analyzation.TypeSystemFactory;
-import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
-import org.eclipse.xpand3.staticTypesystem.DeclaredFunction;
-import org.eclipse.xpand3.staticTypesystem.DeclaredType;
-import org.eclipse.xpand3.util.LoaderFactory;
-
-/**
- * @author Sven Efftinge
- *
- */
-public class JavaDeclarationsContributor implements DeclarationsContributor {
-
- private Map<String, DeclaredType> types = new HashMap<String, DeclaredType>();
-
- private TypeSystemFactory typeSystemFactory = null;
-
- private Class<?> cls;
-
- /**
- *
- */
- public JavaDeclarationsContributor(String importedNamespace) {
- this.cls = LoaderFactory.getClassLoader(this).loadClass(importedNamespace);
- if (cls==null) {
- throw new IllegalArgumentException("Couldn't find Java Class '"+importedNamespace+"'");
- }
- }
-
- /*
- * (non-Javadoc)
- * @see org.eclipse.xand3.analyzation.typesystem.Xpand3DeclarationsResource#setTypeSystem(org.eclipse.xand3.analyzation.TypeSystem)
- */
- public void setTypeSystemFactory(TypeSystemFactory tsf) {
- this.typeSystemFactory = tsf;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.xand3.analyzation.typesystem.Xpand3DeclarationsResource#getReferencedResources()
- */
- public String[] getReferencedContributors() {
- return null;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.xand3.analyzation.typesystem.DeclarationsContributor#typeForName(java.lang.String)
- */
- public DeclaredType typeForName(String name) {
- return null;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.xand3.analyzation.typesystem.DeclarationsContributor#functionForName(java.lang.String, org.eclipse.xpand3.staticTypesystem.AbstractTypeReference[])
- */
- public DeclaredFunction functionForName(String name,
- AbstractTypeReference... parameterTypes) {
- return null;
- }
-
-}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystem.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystem.java
new file mode 100644
index 0000000..1bbee26
--- /dev/null
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystem.java
@@ -0,0 +1,295 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2002-2007 itemis AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * itemis AG - Initial API and implementation
+ *
+ * </copyright>
+ *
+ */
+package org.eclipse.xpand3.analyzation.typesystem.java;
+
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.TypeVariable;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.xpand3.analyzation.GenericsUtil;
+import org.eclipse.xpand3.analyzation.TypeSystem;
+import org.eclipse.xpand3.analyzation.TypeSystemFactory;
+import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
+import org.eclipse.xpand3.staticTypesystem.DeclaredFunction;
+import org.eclipse.xpand3.staticTypesystem.DeclaredProperty;
+import org.eclipse.xpand3.staticTypesystem.DeclaredStaticProperty;
+import org.eclipse.xpand3.staticTypesystem.DeclaredType;
+import org.eclipse.xpand3.staticTypesystem.DeclaredTypeParameter;
+import org.eclipse.xpand3.staticTypesystem.StaticTypesystemFactory;
+import org.eclipse.xpand3.staticTypesystem.Type;
+import org.eclipse.xpand3.util.CreateCache;
+import org.eclipse.xtend.backend.util.ErrorHandler;
+import org.eclipse.xtend.middleend.javaannotations.M2tNoFunction;
+
+/**
+ * @author Sven Efftinge
+ *
+ */
+public class JavaTypeSystem implements TypeSystem {
+
+ private CreateCache<String, DeclaredType> types = new CreateCache<String, DeclaredType>() {
+
+ @Override
+ protected DeclaredType create(String key) {
+ if (key.equals(cls.getSimpleName())) {
+ return StaticTypesystemFactory.eINSTANCE.createDeclaredType();
+ }
+ return null;
+ }
+
+ @Override
+ protected void initialize(String key, DeclaredType value) {
+ initializeDeclaredType(value, key);
+ }
+
+ };
+
+ private TypeSystemFactory typeSystemFactory = null;
+ private Class<?> cls;
+
+ /**
+ *
+ */
+ public JavaTypeSystem(Class<?> javaClass) {
+ if (javaClass == null) {
+ throw new IllegalArgumentException("javaClass");
+ }
+ this.cls = javaClass;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.xand3.analyzation.typesystem.Xpand3DeclarationsResource#setTypeSystem(org.eclipse.xand3.analyzation.TypeSystem)
+ */
+ public void setTypeSystemFactory(TypeSystemFactory tsf) {
+ this.typeSystemFactory = tsf;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.xand3.analyzation.typesystem.DeclarationsContributor#typeForName(java.lang.String)
+ */
+ public DeclaredType typeForName(String name) {
+ return types.get(name);
+ }
+
+ /**
+ * @param name
+ * @return
+ */
+ private DeclaredType initializeDeclaredType(DeclaredType dt, String name) {
+ dt.setName(name);
+ TypeVariable<?>[] typeParameters = cls.getTypeParameters();
+ for (TypeVariable<?> typeVariable : typeParameters) {
+ DeclaredTypeParameter dtp = StaticTypesystemFactory.eINSTANCE
+ .createDeclaredTypeParameter();
+ dtp.setName(typeVariable.getName());
+ dtp.getUpperBounds().addAll(toTypeRefs(typeVariable.getBounds()));
+ dt.getDeclaredTypeParameters().add(dtp);
+ }
+
+ // supertypes
+ if (cls.getGenericSuperclass() != null && !cls.getSuperclass().equals(Object.class))
+ dt.getSuperTypes().add(toTypeRef(cls.getGenericSuperclass()));
+ dt.getSuperTypes().addAll(toTypeRefs(cls.getGenericInterfaces()));
+ if (dt.getSuperTypes().isEmpty()) {
+ DeclaredType objectType = typeSystemFactory.getBuiltInTypeSystem()
+ .typeForName(TypeSystem.OBJECT);
+ dt.getSuperTypes().add(GenericsUtil.typeRef(objectType));
+ }
+
+ // properties
+ try {
+ PropertyDescriptor[] propertyDescriptors = Introspector
+ .getBeanInfo(cls).getPropertyDescriptors();
+ for (PropertyDescriptor pd : propertyDescriptors) {
+ if (getDeclaringClass(pd) != cls)
+ continue;
+ DeclaredProperty dp = StaticTypesystemFactory.eINSTANCE
+ .createDeclaredProperty();
+ dp.setName(pd.getName());
+ dp.setType(toTypeRef(getGenericType(pd)));
+ dt.getProperties().add(dp);
+ }
+ } catch (IntrospectionException e) {
+ ErrorHandler.handle(e);
+ return null;
+ }
+
+ // static properties
+ Field[] fields = cls.getFields();
+ for (Field field : fields) {
+ if (Modifier.isPublic(field.getModifiers())) {
+ if (Modifier.isStatic(field.getModifiers())) {
+ DeclaredStaticProperty dsp = StaticTypesystemFactory.eINSTANCE
+ .createDeclaredStaticProperty();
+ dsp.setName(field.getName());
+ dsp.setType((Type) toTypeRef(field.getGenericType()));
+ dt.getStaticProperties().add(dsp);
+ } else {
+ // DeclaredProperty dp = StaticTypesystemFactory.eINSTANCE
+ // .createDeclaredProperty();
+ // dp.setName(field.getName());
+ // dp.setType(toTypeRef(field.getGenericType()));
+ // dt.getProperties().add(dp);
+ }
+ }
+ }
+ return dt;
+ }
+
+ /**
+ * @param pd
+ * @return
+ */
+ private java.lang.reflect.Type getGenericType(PropertyDescriptor pd) {
+ if (pd.getReadMethod() != null)
+ return pd.getReadMethod().getGenericReturnType();
+
+ return pd.getWriteMethod().getGenericParameterTypes()[0];
+ }
+
+ private Class<?> getDeclaringClass(PropertyDescriptor pd) {
+ if (pd.getReadMethod() != null)
+ return pd.getReadMethod().getDeclaringClass();
+
+ return pd.getWriteMethod().getDeclaringClass();
+ }
+
+ /**
+ * @param bounds
+ * @return
+ */
+ private List<? extends AbstractTypeReference> toTypeRefs(
+ java.lang.reflect.Type[] bounds) {
+ List<AbstractTypeReference> result = new ArrayList<AbstractTypeReference>();
+ for (java.lang.reflect.Type jt : bounds) {
+ result.add(toTypeRef(jt));
+ }
+ return result;
+ }
+
+ /**
+ * @param genericSuperclass
+ * @return
+ */
+ private AbstractTypeReference toTypeRef(java.lang.reflect.Type type) {
+ if (type instanceof Class<?>) {
+ Class<?> c = (Class<?>) type;
+ DeclaredType dt = typeSystemFactory.getBuiltInTypeSystem()
+ .getBuiltinTypeForJavaClass((Class<?>) type);
+ if (dt != null) {
+ DeclaredType declaredType = typeSystemFactory
+ .getBuiltInTypeSystem().typeForName(dt.getName());
+ return GenericsUtil.typeRef(declaredType);
+ }
+ TypeSystem typeSystem = typeSystemFactory
+ .getTypeSystem(c.getName());
+ DeclaredType declaredType = typeSystem.typeForName(c
+ .getSimpleName());
+ return GenericsUtil.typeRef(declaredType);
+ } else if (type instanceof TypeVariable) {
+ TypeVariable<?> tv = (TypeVariable<?>) type;
+ Object genericDeclaration = tv.getGenericDeclaration();
+ if (genericDeclaration instanceof Class) {
+ Class<?> c = (Class<?>) genericDeclaration;
+ DeclaredType dt = getDeclaredTypeForJClass(c);
+ EList<DeclaredTypeParameter> parameters = dt
+ .getDeclaredTypeParameters();
+ for (DeclaredTypeParameter declaredTypeParameter : parameters) {
+ if (declaredTypeParameter.getName().equals(tv.getName())) {
+ org.eclipse.xpand3.staticTypesystem.TypeVariable newTv = StaticTypesystemFactory.eINSTANCE
+ .createTypeVariable();
+ newTv.setDeclaredTypeParameter(declaredTypeParameter);
+ return newTv;
+ }
+ }
+ }
+ throw new IllegalStateException(
+ "Couldn't find declaration for type variable "
+ + tv.getName());
+ } else if (type instanceof java.lang.reflect.ParameterizedType) {
+ ParameterizedType pt = (ParameterizedType) type;
+ java.lang.reflect.Type rawType = pt.getRawType();
+ Type t = StaticTypesystemFactory.eINSTANCE.createType();
+ if (rawType instanceof Class) {
+ DeclaredType dt = getDeclaredTypeForJClass((Class<?>) rawType);
+ t.setDeclaredType(dt);
+ } else {
+ throw new IllegalStateException("missing implementation for "
+ + rawType.getClass());
+ }
+ t.getActualTypeArguments().addAll(
+ toTypeRefs(pt.getActualTypeArguments()));
+ return t;
+ }
+ throw new IllegalArgumentException("toTypeRef not implemented for "
+ + type.getClass().getSimpleName() + " - " + type);
+ }
+
+ /**
+ * @param c
+ * @return
+ */
+ private DeclaredType getDeclaredTypeForJClass(Class<?> c) {
+ DeclaredType declaredType = typeSystemFactory.getBuiltInTypeSystem()
+ .getBuiltinTypeForJavaClass(c);
+ if (declaredType != null) {
+ return declaredType;
+ }
+ return typeSystemFactory.getTypeSystem(c.getName()).typeForName(
+ c.getSimpleName());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.xand3.analyzation.typesystem.DeclarationsContributor#functionForName(java.lang.String,
+ * org.eclipse.xpand3.staticTypesystem.AbstractTypeReference[])
+ */
+ public DeclaredFunction functionForName(String name,
+ AbstractTypeReference... parameterTypes) {
+ Method[] methods = cls.getDeclaredMethods();
+ for (Method method : methods) {
+ if (method.getAnnotation(M2tNoFunction.class) == null) {
+ DeclaredFunction f = StaticTypesystemFactory.eINSTANCE
+ .createDeclaredFunction();
+ f.setName(method.getName());
+ // parameters
+ if (!Modifier.isStatic(method.getModifiers())) {
+ org.eclipse.xpand3.staticTypesystem.DeclaredParameter dp = StaticTypesystemFactory.eINSTANCE
+ .createDeclaredParameter();
+ dp.setName("this");
+ dp.setType(toTypeRef(method.getDeclaringClass()));
+ f.getDeclaredParameters().add(dp);
+ }
+ f.setReturnType(toTypeRef(method.getGenericReturnType()));
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystemFactory.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystemFactory.java
new file mode 100644
index 0000000..291b765
--- /dev/null
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystemFactory.java
@@ -0,0 +1,59 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2002-2007 itemis AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * itemis AG - Initial API and implementation
+ *
+ * </copyright>
+ *
+ */
+package org.eclipse.xpand3.analyzation.typesystem.java;
+
+import org.eclipse.xpand3.analyzation.LanguageSpecificTypeSystemFactory;
+import org.eclipse.xpand3.analyzation.TypeSystem;
+import org.eclipse.xpand3.util.LoaderFactory;
+import org.eclipse.xtend.backend.util.Cache;
+
+/**
+ * @author Sven Efftinge
+ *
+ */
+public class JavaTypeSystemFactory implements LanguageSpecificTypeSystemFactory {
+
+ private Object ctx;
+ private Cache<String,JavaTypeSystem> typeSystems = new Cache<String, JavaTypeSystem>(){
+
+ @Override
+ protected JavaTypeSystem create(String key) {
+ Class<?> class1 = LoaderFactory.getClassLoader(ctx).loadClass(key);
+ if (class1!=null) {
+ return new JavaTypeSystem(class1);
+ }
+ return null;
+ }};
+
+ public JavaTypeSystemFactory(Object ctx) {
+ this.ctx = ctx;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.xpand3.analyzation.LanguageSpecificTypeSystemFactory#canHandle(java.lang.String)
+ */
+ public boolean canHandle(String resourceName) {
+ return LoaderFactory.getClassLoader(ctx).loadClass(resourceName)!=null;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.xpand3.analyzation.LanguageSpecificTypeSystemFactory#createTypeSystem(java.lang.String)
+ */
+ public TypeSystem createTypeSystem(String resourceName) {
+ return typeSystems.get(resourceName);
+ }
+
+}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/xpand3/Xpand3ResourceDeclarationsContributor.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/xpand3/Xpand3ResourceDeclarationsContributor.java
index 3dc3c69..0574225 100644
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/xpand3/Xpand3ResourceDeclarationsContributor.java
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/xpand3/Xpand3ResourceDeclarationsContributor.java
@@ -19,23 +19,20 @@ import org.eclipse.xpand3.DeclaredParameter;
import org.eclipse.xpand3.File;
import org.eclipse.xpand3.Identifier;
import org.eclipse.xpand3.ImportStatement;
-import org.eclipse.xpand3.analyzation.DeclarationsContributor;
import org.eclipse.xpand3.analyzation.TypeSystem;
import org.eclipse.xpand3.analyzation.TypeSystemFactory;
-import org.eclipse.xpand3.analyzation.typesystem.CompositeTypeSystemImpl;
import org.eclipse.xpand3.analyzation.typesystem.util.TypeList;
import org.eclipse.xpand3.ast.AstUtil;
import org.eclipse.xpand3.declaration.Definition;
import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
import org.eclipse.xpand3.staticTypesystem.DeclaredFunction;
import org.eclipse.xpand3.staticTypesystem.DeclaredType;
-import org.eclipse.xtend.backend.util.Cache;
/**
* @author Sven Efftinge
*
*/
public class Xpand3ResourceDeclarationsContributor implements
- DeclarationsContributor {
+ TypeSystem {
private File file = null;
private TypeSystemFactory typeSystemFactory;
@@ -70,35 +67,10 @@ public class Xpand3ResourceDeclarationsContributor implements
AbstractTypeReference[] types = new AbstractTypeReference[def.getParams().size()];
for (int i=0;i<types.length;i++) {
DeclaredParameter declaredParameter = def.getParams().get(i);
- types[i] = getTypeRef(declaredParameter.getType());
}
return types;
}
- /**
- * @param type
- * @return
- */
- private AbstractTypeReference getTypeRef(Identifier type) {
- //TODO type Args
- return getTypeSystem().typeForName(AstUtil.toString(type));
- }
-
- private CompositeTypeSystemImpl ts = null;
- /**
- * @return
- */
- private TypeSystem getTypeSystem() {
- if (ts==null) {
- ts = new CompositeTypeSystemImpl();
- ts.addTypeSystem(typeSystemFactory.getTypeSystem(getResourceName()));
- for (ImportStatement is : file.getImports()) {
- ts.addTypeSystem(typeSystemFactory.getTypeSystem(is.getImportedId().getValue()));
- }
- ts.addTypeSystem(typeSystemFactory.getBuiltInTypeSystem());
- }
- return ts;
- }
/**
* @return
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/util/CreateCache.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/util/CreateCache.java
new file mode 100644
index 0000000..0864d58
--- /dev/null
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/util/CreateCache.java
@@ -0,0 +1,46 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2002-2007 itemis AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * itemis AG - Initial API and implementation
+ *
+ * </copyright>
+ *
+ */
+package org.eclipse.xpand3.util;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Sven Efftinge
+ *
+ */
+public abstract class CreateCache<K, V> {
+ private final Map<K, V> _cache = new HashMap<K, V>();
+
+ public V get(K key) {
+ if (_cache.containsKey(key))
+ return _cache.get(key);
+
+ final V result = create(key);
+ _cache.put(key, result);
+ if (result != null)
+ initialize(key, result);
+ return result;
+ }
+
+ protected abstract V create(K key);
+
+ protected abstract void initialize(K key, V value);
+
+ public Map<K, V> getMap() {
+ return _cache;
+ }
+} \ No newline at end of file
diff --git a/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/TypeSystemTest.java b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/TypeSystemTest.java
index ea6f653..a0380ed 100644
--- a/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/TypeSystemTest.java
+++ b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/TypeSystemTest.java
@@ -15,18 +15,28 @@
*/
package org.eclipse.xpand3.analyzation;
-import org.eclipse.xpand3.staticTypesystem.Type;
-
import junit.framework.TestCase;
+import org.eclipse.xpand3.staticTypesystem.DeclaredType;
+import org.eclipse.xpand3.staticTypesystem.Type;
+import org.eclipse.xpand3.staticTypesystem.WildcardType;
+
/**
* @author Sven Efftinge
*
*/
public class TypeSystemTest extends TestCase {
+
public void testSimple() throws Exception {
TypeSystem ts = TypeSystem.BUILTIN_TYPESYSTEM;
- Type typeForName = ts.typeForName("Map", ts.typeForName("String"), ts.typeForName("Object"));
- assertNotNull(typeForName);
+ DeclaredType map = ts.typeForName("Map");
+ DeclaredType string = ts.typeForName("String");
+ DeclaredType object = ts.typeForName("Object");
+ Type typeRef = GenericsUtil.typeRef(map,GenericsUtil.wildCard(GenericsUtil.typeRef(string)), GenericsUtil.typeRef(object));
+ assertNotNull(typeRef);
+ WildcardType wct = (WildcardType) typeRef.getActualTypeArguments().get(0);
+ assertTrue(string == ((Type)wct.getUpperBounds().get(0)).getDeclaredType());
}
+
+
}
diff --git a/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/typesystem/java/GenericsBean.java b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/typesystem/java/GenericsBean.java
new file mode 100644
index 0000000..cb70ab3
--- /dev/null
+++ b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/typesystem/java/GenericsBean.java
@@ -0,0 +1,32 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2002-2007 itemis AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * itemis AG - Initial API and implementation
+ *
+ * </copyright>
+ *
+ */
+package org.eclipse.xpand3.analyzation.typesystem.java;
+
+/**
+ * @author Sven Efftinge
+ *
+ */
+public class GenericsBean<T, L extends Comparable<T>> implements Comparable<L>{
+
+ public int compareTo(L o) {
+ return 0;
+ }
+
+ public static <X extends GenericsBean<String, String>> X doStuff(String a, X b) {
+ return null;
+ }
+
+}
diff --git a/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystemTest.java b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystemTest.java
new file mode 100644
index 0000000..e870141
--- /dev/null
+++ b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystemTest.java
@@ -0,0 +1,77 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2002-2007 itemis AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * itemis AG - Initial API and implementation
+ *
+ * </copyright>
+ *
+ */
+package org.eclipse.xpand3.analyzation.typesystem.java;
+
+import junit.framework.TestCase;
+
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.xpand3.analyzation.TypeSystem;
+import org.eclipse.xpand3.analyzation.TypeSystemFactory;
+import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
+import org.eclipse.xpand3.staticTypesystem.DeclaredProperty;
+import org.eclipse.xpand3.staticTypesystem.DeclaredStaticProperty;
+import org.eclipse.xpand3.staticTypesystem.DeclaredType;
+import org.eclipse.xpand3.staticTypesystem.Type;
+
+/**
+ * @author Sven Efftinge
+ *
+ */
+public class JavaTypeSystemTest extends TestCase {
+ private TypeSystemFactory factory;
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see junit.framework.TestCase#setUp()
+ */
+ @Override
+ protected void setUp() throws Exception {
+ factory = new TypeSystemFactory();
+ factory
+ .registerLanguageSpecificFactory(new JavaTypeSystemFactory(this));
+ }
+
+ public void testSimpleBean() throws Exception {
+ TypeSystem typeSystem = factory.getTypeSystem(SimpleBean.class
+ .getName());
+ DeclaredType declaredType = typeSystem.typeForName(SimpleBean.class
+ .getSimpleName());
+ assertNotNull(declaredType);
+
+ // property
+ DeclaredProperty property = declaredType.getProperties().get(0);
+ assertEquals("foo", property.getName());
+ assertEquals(factory.getBuiltInTypeSystem().typeForName(
+ TypeSystem.STRING), ((Type) property.getType())
+ .getDeclaredType());
+
+ // check static property
+ DeclaredStaticProperty staticProperty = declaredType.getStaticProperties().get(0);
+ assertEquals("INSTANCE", staticProperty.getName());
+ assertEquals(declaredType, staticProperty.getType().getDeclaredType());
+ }
+
+ public void testGenerics() throws Exception {
+ TypeSystem typeSystem = factory.getTypeSystem(GenericsBean.class
+ .getName());
+ DeclaredType declaredType = typeSystem.typeForName(GenericsBean.class
+ .getSimpleName());
+ assertNotNull(declaredType);
+ EList<AbstractTypeReference> superTypes = declaredType.getSuperTypes();
+ assertEquals(1,superTypes.size());
+ }
+}
diff --git a/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/typesystem/java/SimpleBean.java b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/typesystem/java/SimpleBean.java
new file mode 100644
index 0000000..cf0e488
--- /dev/null
+++ b/tests/org.eclipse.xpand3.tests/src/org/eclipse/xpand3/analyzation/typesystem/java/SimpleBean.java
@@ -0,0 +1,33 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2002-2007 itemis AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * itemis AG - Initial API and implementation
+ *
+ * </copyright>
+ *
+ */
+package org.eclipse.xpand3.analyzation.typesystem.java;
+
+
+/**
+ * @author Sven Efftinge
+ *
+ */
+public class SimpleBean {
+ private String foo;
+ /**
+ * @return the foo
+ */
+ public String getFoo() {
+ return foo;
+ }
+
+ public static SimpleBean INSTANCE = new SimpleBean();
+}