• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 220

22 Nov 2023 12:57PM UTC coverage: 62.564% (-0.04%) from 62.602%
220

Pull #1081

circleci

Frotty
Update MapRequest.java
Pull Request #1081: More performance improvements

17307 of 27663 relevant lines covered (62.56%)

0.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.29
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/jassinterpreter/ReflectionBasedNativeProvider.java
1
package de.peeeq.wurstio.jassinterpreter;
2

3
import de.peeeq.wurstscript.WLogger;
4
import de.peeeq.wurstscript.intermediatelang.ILconst;
5
import de.peeeq.wurstscript.intermediatelang.interpreter.NativesProvider;
6
import de.peeeq.wurstscript.intermediatelang.interpreter.NoSuchNativeException;
7
import de.peeeq.wurstscript.utils.Pair;
8

9
import java.io.PrintStream;
10
import java.lang.reflect.InvocationTargetException;
11
import java.lang.reflect.Method;
12
import java.util.Arrays;
13
import java.util.HashMap;
14
import java.util.stream.Collectors;
15

16
public abstract class ReflectionBasedNativeProvider implements NativesProvider {
17

18
    protected PrintStream outStream = System.err;
1✔
19

20
    private final HashMap<Pair<String, Integer>, Method> methodMap = new HashMap<>();
1✔
21

22
    public ReflectionBasedNativeProvider() {
1✔
23
        for (Method method : this.getClass().getMethods()) {
1✔
24
            Pair<String, Integer> keyPair = Pair.create(method.getName(), method.getParameterTypes().length);
1✔
25
            if (methodMap.containsKey(keyPair)) {
1✔
26
                throw new RuntimeException("native entry already exists");
×
27
            }
28
            methodMap.put(keyPair, method);
1✔
29
        }
30
    }
1✔
31

32
    @Override
33
    public ILconst invoke(String funcname, ILconst[] args) throws NoSuchNativeException {
34
        Method method = methodMap.get(Pair.create(funcname, args.length));
1✔
35
        if (method == null) {
1✔
36
            String msg = "Calling method " + funcname + "(" +
1✔
37
                Arrays.stream(args).map(Object::toString).collect(Collectors.joining(", ")) + ")";
1✔
38
            msg += "\nwith types " + funcname + "(" +
1✔
39
                Arrays.stream(args).map(o -> o.getClass().getSimpleName()).collect(Collectors.joining(", ")) + ")";
1✔
40
//        if (candidate != null) {
41
//            msg += "\nDid you mean " + funcname + "(" +
42
//                Arrays.stream(candidate.getParameterTypes()).map(Class::getSimpleName).collect(Collectors.joining(", ")) + ")?";
43
//        }
44
            throw new NoSuchNativeException(msg);
1✔
45
        }
46
        try {
47
            return (ILconst) method.invoke(this, (Object[]) args);
1✔
48
        } catch (IllegalAccessException | IllegalArgumentException e) {
×
49
            WLogger.severe(e);
×
50
            throw new Error(e);
×
51
        } catch (InvocationTargetException e) {
×
52
            if (e.getCause() instanceof InterpreterException) {
×
53
                throw (InterpreterException) e.getCause();
×
54
            }
55
            if (e.getCause() instanceof Error) {
×
56
                throw (Error) e.getCause();
×
57
            }
58
            throw new Error(e.getCause());
×
59
        }
60

61
    }
62

63
    @Override
64
    public void setOutStream(PrintStream outStream) {
65
        this.outStream = outStream;
1✔
66
    }
1✔
67

68
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc