Skip to main content
summaryrefslogtreecommitdiffstats
blob: 376febc0a17c5c9b825af886516c6c57473a501a (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
/*
 * <copyright>
 *
 * Copyright (c) 2005-2006 Sven Efftinge 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:
 *     Sven Efftinge - Initial API and implementation
 *
 * </copyright>
 */
package org.eclipse.internal.xtend.type.impl.java;

import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.xtend.typesystem.AbstractTypeImpl;
import org.eclipse.xtend.typesystem.Feature;
import org.eclipse.xtend.typesystem.Type;

/**
 * @author Sven Efftinge
 * @author Arno Haase
 */
public class JavaTypeImpl extends AbstractTypeImpl implements Type {

    private final static Log log = LogFactory.getLog(JavaTypeImpl.class);

    private Class<?> clazz;

    private Set<Type> superTypes = null;

    private Feature[] features = null;

    private JavaTypeStrategy strategy = null;

    private JavaMetaModel metamodel = null;

    public JavaTypeImpl(final JavaMetaModel meta, final Class<?> clazz, final String name, final JavaTypeStrategy strategy) {
        super(meta.getTypeSystem(), name);
        this.clazz = clazz;
        metamodel = meta;
        this.strategy = strategy;
    }

    @Override
    public Feature[] getContributedFeatures() {
        if (features == null) {
            features = strategy.getFeatures(metamodel, clazz, this);
        }
        return features;
    }

	@Override
    public Set<Type> getSuperTypes() {
        if (superTypes == null) {
            final Set<Type> result = new HashSet<Type>();
            if (clazz.getSuperclass() != null && !clazz.getSuperclass().equals(Object.class)) {
                final Type beanType = metamodel.builtinAwareGetTypeForClass(clazz.getSuperclass());
                if (beanType != null) {
                    result.add(beanType);
                }
            }
            final Class[] interfaces = clazz.getInterfaces();
            for (int i = 0; i < interfaces.length; i++) {
                final Type beanType = metamodel.builtinAwareGetTypeForClass(interfaces[i]);
                if (beanType != null) {
                    result.add(beanType);
                }
            }
            if (result.isEmpty()) {
                result.add(metamodel.getTypeSystem().getObjectType());
            }
            superTypes = result;
        }
        return superTypes;
    }

    public boolean isInstance(final Object o) {
        return clazz.isInstance(o);
    }

    @Override
    protected boolean internalIsAssignableFrom(final Type t) {
        if (t instanceof JavaTypeImpl)
            return clazz.isAssignableFrom(((JavaTypeImpl) t).clazz);
        return false;
    }

    public Object newInstance() {
        try {
            return clazz.newInstance();
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean isAbstract() {
        try {
            return clazz.getConstructor(new Class[0]) == null || Modifier.isAbstract (clazz.getModifiers());
        } catch (final SecurityException e) {
            log.error(e.getMessage(), e);
        } catch (final NoSuchMethodException e) {
            log.error(e.getMessage(), e);
        }
        return true;
    }

}

Back to the top