Skip to main content
summaryrefslogtreecommitdiffstats
blob: a96717c17abf42b3eab4d93de5b08f93187c3494 (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
/*
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.util.HashMap;
import java.util.Map;

import org.eclipse.xtend.backend.common.BackendType;
import org.eclipse.xtend.backend.common.BackendTypesystem;
import org.eclipse.xtend.backend.functions.java.internal.JavaBuiltinConverterFactory;
import org.eclipse.xtend.backend.util.ErrorHandler;


/**
 * 
 * @author Arno Haase (http://www.haase-consulting.com)
 */
public abstract class AbstractJavaBeansTypesystem implements BackendTypesystem {
    private BackendTypesystem _rootTypesystem = null;
    private Map<Class<?>, JavaBeansType> _cache = new HashMap<Class<?>, JavaBeansType>(); // this is necessary to avoid endless recursion!
    
    public static final String UNIQUE_REPRESENTATION_PREFIX = "{javabean}";
    

    public BackendType findType (Class<?> cls) {
        if (cls == Object.class)
            return null;

        final BackendType nonstandardBuiltin = JavaBuiltinConverterFactory.getTypeForAdditionalBuiltin (cls);
        if (nonstandardBuiltin != null)
            return nonstandardBuiltin;
        
        if (! matchesScope (cls))
            return null;

        JavaBeansType result = _cache.get (cls);
        if (result != null)
            return result;
        
        result = new JavaBeansType (cls);
        _cache.put (cls, result);
        
        try {
            result.init (this);
        } catch (IntrospectionException e) {
            ErrorHandler.handle(e);
        }
        return result;
    }
    
    public BackendType findType (Object o) {
        if (o == null)
            return null;
        
        return findType (o.getClass());
    }

    public BackendType findType (String uniqueRepresentation) {
        if (! uniqueRepresentation.startsWith(UNIQUE_REPRESENTATION_PREFIX))
            return null;
        
        try {
            return findType (Class.forName (uniqueRepresentation.substring (UNIQUE_REPRESENTATION_PREFIX.length())));
        } catch (ClassNotFoundException e) {
            ErrorHandler.handle (e);
            return null; //just to make the compiler happy
        }
    }
    
    /**
     * This operation allows support for type system instances that are selective, e.g. by package, as
     *  to which classes they feel responsible for. 
     */
    public abstract boolean matchesScope (Class<?> cls);


    public BackendTypesystem getRootTypesystem () {
        return _rootTypesystem;
    }

    public void setRootTypesystem (BackendTypesystem ts) {
        _rootTypesystem = ts;
    }
}

Back to the top