Skip to content

Commit b0948bf

Browse files
committed
ti: allow extending DefaultMetaType
1 parent 3f4dc22 commit b0948bf

File tree

5 files changed

+76
-69
lines changed

5 files changed

+76
-69
lines changed

plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/javascript/typeinfo/DefaultMetaType.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@
1313

1414
import org.eclipse.dltk.javascript.typeinfo.model.Type;
1515

16-
public enum DefaultMetaType implements MetaType {
16+
public abstract class DefaultMetaType implements MetaType {
1717

18-
DEFAULT;
18+
public static final MetaType DEFAULT = new Instance();
1919

20-
public String getId() {
21-
return MetaType.class.getName() + "." + name();
20+
static class Instance extends DefaultMetaType {
21+
public String getId() {
22+
return MetaType.class.getName() + ".DEFAULT";
23+
}
2224
}
2325

2426
public IRType toRType(ITypeSystem typeSystem, Type type) {
25-
// TODO (alex) compatibility
26-
for (IRTypeFactory factory : TypeInfoManager.getRTypeFactories()) {
27-
final IRType runtimeType = factory.create(type);
28-
if (runtimeType != null) {
29-
return runtimeType;
30-
}
31-
}
3227
return new RSimpleType(typeSystem, type);
3328
}
3429

3530
public IRType toRType(ITypeSystem typeSystem, IRTypeDeclaration declaration) {
3631
return new RSimpleType(typeSystem, declaration);
3732
}
3833

34+
@Override
35+
public String toString() {
36+
return getId();
37+
}
38+
3939
}

plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/javascript/typeinfo/IRTypeFactory.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,13 @@
1313

1414
import org.eclipse.dltk.annotations.ConfigurationElement;
1515
import org.eclipse.dltk.javascript.typeinfo.model.JSType;
16-
import org.eclipse.dltk.javascript.typeinfo.model.Type;
1716

1817
/**
1918
* Factory to create runtime type instance for the specified model object
2019
*/
2120
@ConfigurationElement("runtimeTypeFactory")
2221
public interface IRTypeFactory {
2322

24-
/**
25-
* Converts the specified {@link Type} to the corresponding {@link IRType}.
26-
* This method is deprecated, use {@link MetaType} instead.
27-
*
28-
* @param type
29-
* @return
30-
*/
31-
@Deprecated
32-
IRType create(Type type);
33-
3423
IRType create(ITypeSystem typeSystem, JSType type);
3524

3625
}

plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/javascript/typeinfo/MetaTypeFactory.java

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
*******************************************************************************/
1212
package org.eclipse.dltk.javascript.typeinfo;
1313

14+
import java.lang.reflect.Field;
15+
import java.lang.reflect.Modifier;
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
1419
import org.eclipse.core.runtime.CoreException;
1520
import org.eclipse.core.runtime.IConfigurationElement;
1621
import org.eclipse.core.runtime.IExecutableExtension;
@@ -63,13 +68,23 @@ public Object create() throws CoreException {
6368
try {
6469
final Class<?> clazz = bundle
6570
.loadClass(className.substring(0, dot));
71+
final String name = className.substring(dot + 1);
6672
if (!clazz.isEnum()) {
67-
throw new CoreException(new Status(IStatus.ERROR,
68-
JavaScriptPlugin.PLUGIN_ID, 0, "Class "
69-
+ clazz.getName() + " must be enum for "
70-
+ getClass(), null));
73+
final Field field = clazz.getDeclaredField(name);
74+
if (isConstField(field)) {
75+
try {
76+
return field.get(null);
77+
} catch (IllegalAccessException e) {
78+
throw new CoreException(new Status(IStatus.ERROR,
79+
JavaScriptPlugin.PLUGIN_ID, 0, "Error reading "
80+
+ field, e));
81+
}
82+
} else {
83+
throw new CoreException(new Status(IStatus.ERROR,
84+
JavaScriptPlugin.PLUGIN_ID, 0, "Field " + field
85+
+ " must be public static final", null));
86+
}
7187
}
72-
final String name = className.substring(dot + 1);
7388
final Enum<?>[] entries = (Enum<?>[]) clazz.getEnumConstants();
7489
for (Enum<?> entry : entries) {
7590
if (name.equals(entry.name())) {
@@ -81,14 +96,32 @@ public Object create() throws CoreException {
8196
+ " for " + getClass(), null));
8297
} catch (ClassNotFoundException e) {
8398
// fall-through and try the whole string
99+
} catch (NoSuchFieldException e1) {
100+
// fall-through and try the whole string
84101
}
85102
try {
86103
final Class<?> clazz = bundle.loadClass(className);
87104
if (!clazz.isEnum()) {
88-
throw new CoreException(new Status(IStatus.ERROR,
89-
JavaScriptPlugin.PLUGIN_ID, 0, "Class "
90-
+ clazz.getName() + " must be enum for "
91-
+ getClass(), null));
105+
final List<Field> fields = new ArrayList<Field>();
106+
for (Field field : clazz.getDeclaredFields()) {
107+
if (isConstField(field)) {
108+
fields.add(field);
109+
}
110+
}
111+
if (fields.size() != 1) {
112+
throw new CoreException(new Status(IStatus.ERROR,
113+
JavaScriptPlugin.PLUGIN_ID, 0,
114+
"Single public static final field expected in "
115+
+ clazz.getName() + " for " + getClass(),
116+
null));
117+
}
118+
try {
119+
return fields.get(0).get(null);
120+
} catch (IllegalAccessException e) {
121+
throw new CoreException(new Status(IStatus.ERROR,
122+
JavaScriptPlugin.PLUGIN_ID, 0, "Error reading "
123+
+ fields.get(0), e));
124+
}
92125
}
93126
final Object[] entries = clazz.getEnumConstants();
94127
if (entries.length != 1) {
@@ -104,4 +137,9 @@ public Object create() throws CoreException {
104137
+ " not found for " + getClass(), e));
105138
}
106139
}
140+
141+
private boolean isConstField(Field field) {
142+
return (field.getModifiers() & (Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL)) == (Modifier.PUBLIC
143+
| Modifier.STATIC | Modifier.FINAL);
144+
}
107145
}

tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/typeinfo/MetaTypes.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,21 @@
1212
package org.eclipse.dltk.javascript.core.tests.typeinfo;
1313

1414
import org.eclipse.dltk.javascript.typeinfo.DefaultMetaType;
15-
import org.eclipse.dltk.javascript.typeinfo.IRType;
16-
import org.eclipse.dltk.javascript.typeinfo.IRTypeDeclaration;
17-
import org.eclipse.dltk.javascript.typeinfo.ITypeSystem;
18-
import org.eclipse.dltk.javascript.typeinfo.MetaType;
19-
import org.eclipse.dltk.javascript.typeinfo.model.Type;
2015

21-
public enum MetaTypes implements MetaType {
16+
public abstract class MetaTypes extends DefaultMetaType {
2217

23-
ONE, TWO;
18+
public static final MetaTypes ONE = new MetaTypes() {
19+
@Override
20+
public String getId() {
21+
return MetaTypes.class.getName() + ".ONE";
22+
}
23+
};
2424

25-
@Override
26-
public String getId() {
27-
return getClass().getName() + "." + name();
28-
}
29-
30-
@Override
31-
public IRType toRType(ITypeSystem typeSystem, Type type) {
32-
return DefaultMetaType.DEFAULT.toRType(typeSystem, type);
33-
}
34-
35-
@Override
36-
public IRType toRType(ITypeSystem typeSystem, IRTypeDeclaration declaration) {
37-
return DefaultMetaType.DEFAULT.toRType(typeSystem, declaration);
38-
}
25+
public static final MetaTypes TWO = new MetaTypes() {
26+
@Override
27+
public String getId() {
28+
return MetaTypes.class.getName() + ".TWO";
29+
}
30+
};
3931

4032
}

tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/typeinfo/TestMetaType.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,17 @@
1212
package org.eclipse.dltk.javascript.core.tests.typeinfo;
1313

1414
import org.eclipse.dltk.javascript.typeinfo.DefaultMetaType;
15-
import org.eclipse.dltk.javascript.typeinfo.IRType;
16-
import org.eclipse.dltk.javascript.typeinfo.IRTypeDeclaration;
17-
import org.eclipse.dltk.javascript.typeinfo.ITypeSystem;
18-
import org.eclipse.dltk.javascript.typeinfo.MetaType;
19-
import org.eclipse.dltk.javascript.typeinfo.model.Type;
2015

21-
public enum TestMetaType implements MetaType {
16+
public class TestMetaType extends DefaultMetaType {
2217

23-
INSTANCE;
18+
public static final TestMetaType INSTANCE = new TestMetaType();
2419

25-
@Override
26-
public String getId() {
27-
return getClass().getName() + "." + name();
28-
}
29-
30-
@Override
31-
public IRType toRType(ITypeSystem typeSystem, Type type) {
32-
return DefaultMetaType.DEFAULT.toRType(typeSystem, type);
20+
private TestMetaType() {
3321
}
3422

3523
@Override
36-
public IRType toRType(ITypeSystem typeSystem, IRTypeDeclaration declaration) {
37-
return DefaultMetaType.DEFAULT.toRType(typeSystem, declaration);
24+
public String getId() {
25+
return TestMetaType.class.getName() + ".INSTANCE";
3826
}
3927

4028
}

0 commit comments

Comments
 (0)