1 package org.simantics.scl.commands.internal.checker;
\r
3 import java.util.Map;
\r
5 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
\r
6 import org.simantics.scl.compiler.top.ValueNotFound;
\r
7 import org.simantics.scl.compiler.types.Type;
\r
8 import org.simantics.scl.compiler.types.Types;
\r
9 import org.simantics.scl.compiler.types.exceptions.MatchException;
\r
10 import org.simantics.scl.compiler.types.util.MultiFunction;
\r
11 import org.simantics.scl.osgi.SCLOsgi;
\r
14 * Checks given parameters
\r
16 * @author Hannu Niemistö
\r
18 public class Checker {
\r
22 private Checker(Object check, CheckItem item) {
\r
27 public boolean check(Object[] parameters) {
\r
28 return item.check(check, parameters, 0);
\r
31 private static CheckItem checkItemForType(Type type) throws MatchException {
\r
32 if(type == Types.BOOLEAN)
\r
33 return BooleanCheckItem.INSTANCE;
\r
34 if(Types.isFunction(type)) {
\r
35 MultiFunction mfun = Types.matchFunction(type, 1);
\r
36 return new FunctionCheckItem(mfun.parameterTypes[0], checkItemForType(mfun.returnType));
\r
38 else if(Types.isApply(Types.MAYBE, 1, type)) {
\r
39 Type componentType = Types.matchApply(Types.MAYBE, type);
\r
40 return new MaybeCheckItem(checkItemForType(componentType));
\r
43 throw new MatchException();
\r
46 public static Checker create(String name) {
\r
48 SCLValue checkRef = SCLOsgi.MODULE_REPOSITORY.getValueRef(name);
\r
49 Object check = SCLOsgi.MODULE_REPOSITORY.getValue(name);
\r
50 return new Checker(check, checkItemForType(checkRef.getType()));
\r
51 } catch(ValueNotFound e) {
\r
52 // If we don't find a check, it always succeeds
\r
53 return new Checker(Boolean.TRUE, BooleanCheckItem.INSTANCE);
\r
54 } catch (MatchException e) {
\r
55 // Should not happens
\r
56 e.printStackTrace();
\r
57 return new Checker(Boolean.FALSE, BooleanCheckItem.INSTANCE);
\r