Skip to main content
summaryrefslogtreecommitdiffstats
blob: 68dce8c0fb0ab22c0b8923601e8cd3ea1cdd28cc (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
/*
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;

import org.eclipse.xtend.backend.common.BackendType;
import org.eclipse.xtend.backend.common.ExecutionContext;
import org.eclipse.xtend.backend.common.Property;
import org.eclipse.xtend.backend.functions.java.internal.JavaBuiltinConverter;
import org.eclipse.xtend.backend.functions.java.internal.JavaBuiltinConverterFactory;


/**
 * 
 * @author Arno Haase (http://www.haase-consulting.com)
 */
public abstract class AbstractProperty implements Property {
    protected final BackendType _owner;
    protected final BackendType _type;
    protected final String _name;
    protected final boolean _isWritable;
    protected final JavaBuiltinConverter _converter;

    public AbstractProperty (BackendType owner, BackendType type, Class<?> javaType, String name, boolean isWritable) {
        if (owner == null)
            throw new IllegalArgumentException ();
        if (type == null)
            throw new IllegalArgumentException ();
        
        _owner = owner;
        _type = type;
        _name = name;
        _isWritable = isWritable;
        
        _converter = JavaBuiltinConverterFactory.getConverter (javaType);
    }

    public String getName () {
        return _name;
    }
    
    public BackendType getOwner () {
        return _owner;
    }
    
    public BackendType getType () {
        return _type;
    }
    
    public final Object get (ExecutionContext ctx, Object o) {
        return _converter.javaToBackend (getRaw (ctx, o));
    }

    
    public final void set (ExecutionContext ctx, Object o, Object newValue) {
        setRaw (ctx, o, _converter.backendToJava (newValue));
    }

    protected abstract Object getRaw (ExecutionContext ctx, Object o);
    
    @SuppressWarnings("unused")
    public void setRaw (ExecutionContext ctx,  Object o, Object newValue) {
        throw new IllegalStateException ("property " + _name + " of type " + _owner.getName() + " can not be set");
    }
    
    public boolean isWritable () {
        return _isWritable;
    }
}

Back to the top