Skip to main content
summaryrefslogtreecommitdiffstats
blob: 127452c8388db8e9f3dff77d538f740a4cf57a3a (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
/*
 * <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.Method;
import java.util.Arrays;

import org.eclipse.internal.xtend.type.baseimpl.OperationImpl;
import org.eclipse.xtend.typesystem.Operation;
import org.eclipse.xtend.typesystem.Type;

/**
 * @author Sven Efftinge
 * @author Arno Haase
 */
public class JavaOperationImpl extends OperationImpl implements Operation {

    private Method method;

    public JavaOperationImpl(final Type owner, final String name, final Type type, final Type[] parameterTypes,
            final Method method) {
        super(owner, name, type, parameterTypes);
        this.method = method;
    }

    @Override
    public Object evaluateInternal(Object target, final Object[] params) {
        try {
            target = getOwner().convert(target, method.getDeclaringClass());
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    params[i] = getParameterTypes().get(i).convert(params[i], method.getParameterTypes()[i]);
                }
            }
            final Object resultRaw = method.invoke(target, params);
            if (resultRaw != null && resultRaw.getClass().isArray()) {
                return Arrays.asList ((Object[]) resultRaw);
            }
            return resultRaw;
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }

}

Back to the top