|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSR-223-compliant Jython scripting language plugin. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2020 - 2025 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | +package org.scijava.plugins.scripting.jython; |
| 30 | + |
| 31 | +import org.scijava.code.api.ScriptDialect; |
| 32 | + |
| 33 | +/** |
| 34 | + * How Jython scripts see their script parameters: as the Java objects |
| 35 | + * themselves (primitives boxed), e.g. {@code image: net.imglib2.img.Img}. |
| 36 | + * |
| 37 | + * @author Gabriel Selzer |
| 38 | + */ |
| 39 | +class JythonDialect implements ScriptDialect { |
| 40 | + |
| 41 | + @Override |
| 42 | + public String runtimeType(final Class<?> type) { |
| 43 | + return boxed(type).getName(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public String declare(final String name, final String type) { |
| 48 | + return name + ": " + type; |
| 49 | + } |
| 50 | + |
| 51 | + /** Maps primitive types to their wrappers, whose members can be listed. */ |
| 52 | + private static Class<?> boxed(final Class<?> type) { |
| 53 | + if (!type.isPrimitive()) return type; |
| 54 | + if (type == int.class) return Integer.class; |
| 55 | + if (type == long.class) return Long.class; |
| 56 | + if (type == double.class) return Double.class; |
| 57 | + if (type == float.class) return Float.class; |
| 58 | + if (type == boolean.class) return Boolean.class; |
| 59 | + if (type == char.class) return Character.class; |
| 60 | + if (type == short.class) return Short.class; |
| 61 | + if (type == byte.class) return Byte.class; |
| 62 | + return Object.class; // void |
| 63 | + } |
| 64 | +} |
0 commit comments