Skip to main content
summaryrefslogtreecommitdiffstats
blob: c02b999ba6c5da94108632bf0f7037da9cd98387 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright (c) 2008 Arno Haase.
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:
    Arno Haase - initial API and implementation
 */
package org.eclipse.xtend.backend.types.java.internal;

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.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.xtend.backend.common.BackendType;
import org.eclipse.xtend.backend.common.BackendTypesystem;
import org.eclipse.xtend.backend.common.ExecutionContext;
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.functions.java.internal.JavaBuiltinConverterFactory;
import org.eclipse.xtend.backend.types.builtin.VoidType;
import org.eclipse.xtend.backend.util.ErrorHandler;


/**
 * 
 * @author Arno Haase (http://www.haase-consulting.com)
 */
public final class JavaBeansType implements BackendType {
    private final Class<?> _javaClass;
    private final List<NamedFunction> _operations = new ArrayList<NamedFunction>();
    private final Map<String, JavaBeansProperty> _properties = new HashMap<String, JavaBeansProperty> ();
    private final Map<String, StaticProperty> _staticProperties = new HashMap<String, StaticProperty> ();
    
    private Collection<BackendType> _superTypes;
    
    public JavaBeansType (Class<?> cls) {
        _javaClass = cls;
    }

    /** 
     * the actual initialization is separated to deal with circular dependencies of operations and/or 
     *  properties referring to this very same type.
     */
    void init (BackendTypesystem ts) throws IntrospectionException {
        _superTypes = Collections.singleton (ts.getRootTypesystem().findType (_javaClass.getSuperclass()));

        for (Method mtd: _javaClass.getMethods()) {
            if (mtd.getDeclaringClass() == Object.class) // toString is added as a syslib function
                continue;
            
            final List<BackendType> paramTypes = new ArrayList<BackendType> ();
            
            paramTypes.add (this); // first parameter is the object on which the method is called
            for (Class<?> cls: mtd.getParameterTypes()) {
                paramTypes.add (ts.getRootTypesystem().findType(cls));
            }
            
            _operations.add (new NamedFunction (mtd.getName(), new JavaOperation (mtd, paramTypes, null)));
        }
        
        for (PropertyDescriptor pd: Introspector.getBeanInfo(_javaClass).getPropertyDescriptors()) {
            if (pd.getReadMethod().getDeclaringClass() == Object.class)
                continue;
            
            _properties.put (pd.getName(), new JavaBeansProperty (pd, this, ts.getRootTypesystem().findType (pd.getPropertyType()), JavaBuiltinConverterFactory.getConverter (pd.getPropertyType ())));
        }
        
        // static properties
        for (Field field: _javaClass.getFields()) {
            final int mod = field.getModifiers();
            if (Modifier.isPublic(mod) && Modifier.isStatic(mod) && Modifier.isFinal(mod)) {
                try {
                    _staticProperties.put (field.getName(), new JavaBeansStaticProperty (field, this, ts.getRootTypesystem().findType (field.getType()), JavaBuiltinConverterFactory.getConverter (field.getType())));
                } catch (Exception e) {
                    ErrorHandler.handle (e);
                }
            }
        }

        // Java 5 enums
        final Object[] enumValues = _javaClass.getEnumConstants();
        if (enumValues != null) {
            for (Object o : enumValues) {
                final Enum<?> curEnum = (Enum<?>) o;
                _staticProperties.put (curEnum.name(), new JavaBeansStaticProperty (this, ts.getRootTypesystem().findType(curEnum), curEnum.name(), curEnum));
            }
        }
    }
    
    public Object create () {
        try {
            return _javaClass.newInstance();
        } catch (Exception e) {
            ErrorHandler.handle (e);
            return null; // to make the compiler happy - this is never executed
        }
    }

    public List<NamedFunction> getBuiltinOperations () {
        return _operations;
    }

    public Object getProperty (ExecutionContext ctx, Object o, String name) {
        return findProperty(name).get (ctx, o);
    }

    private Property findProperty (String name) {
        final Property result = _properties.get (name);
        if (result == null)
            throw new IllegalArgumentException (" no property " + name + " for type " + getName());
        
        return result;
    }
    
    public void setProperty (ExecutionContext ctx, Object o, String name, Object value) {
        findProperty(name).set (ctx, o, value);
    }

    public boolean isAssignableFrom (BackendType other) {
        if (other == VoidType.INSTANCE)
            return true;
        
        if (! (other instanceof JavaBeansType))
            return false;
        
        final JavaBeansType jbt = (JavaBeansType) other;
        return _javaClass.isAssignableFrom(jbt._javaClass);
    }

    public String getName () {
        return _javaClass.getCanonicalName().replace(".", "::");
    }

    public Map<String, ? extends Property> getProperties () {
        return _properties;
    }

    public Map<String, ? extends StaticProperty> getStaticProperties () {
        return _staticProperties;
    }

    public Collection<BackendType> getSuperTypes () {
        return _superTypes;
    }
    
    @Override
    public String toString () {
        return "JavaBeansType[" + _javaClass.getName()  + "]";
    }
}


Back to the top