org.apache.groovy.swing.binding.ClosureTriggerBinding Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of groovy-swing Show documentation
Show all versions of groovy-swing Show documentation
Groovy: A powerful multi-faceted language for the JVM
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.groovy.swing.binding;
import groovy.lang.Closure;
import groovy.lang.GroovyObjectSupport;
import groovy.lang.Reference;
import org.codehaus.groovy.reflection.ReflectionUtils;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class ClosureTriggerBinding implements TriggerBinding, SourceBinding {
private static final BindPath[] EMPTY_BINDPATH_ARRAY = new BindPath[0];
Map syntheticBindings;
Closure closure;
public ClosureTriggerBinding(Map syntheticBindings) {
this.syntheticBindings = syntheticBindings;
}
public Closure getClosure() {
return closure;
}
public void setClosure(Closure closure) {
this.closure = closure;
}
private BindPath createBindPath(String propertyName, BindPathSnooper snooper) {
BindPath bp = new BindPath();
bp.propertyName = propertyName;
bp.updateLocalSyntheticProperties(syntheticBindings);
List childPaths = new ArrayList<>();
for (Map.Entry entry : snooper.fields.entrySet()) {
childPaths.add(createBindPath(entry.getKey(), entry.getValue()));
}
bp.children = childPaths.toArray(EMPTY_BINDPATH_ARRAY);
return bp;
}
@Override
public FullBinding createBinding(SourceBinding source, TargetBinding target) {
if (source != this) {
throw new RuntimeException("Source binding must the Trigger Binding as well");
}
final BindPathSnooper delegate = new BindPathSnooper();
try {
// create our own local copy of the closure
final Class closureClass = closure.getClass();
// do in privileged block since we may be looking at private stuff
Closure closureLocalCopy = doPrivileged(new PrivilegedAction() {
@Override
public Closure run() {
// assume closures have only 1 constructor, of the form (Object, Reference*)
Constructor constructor = closureClass.getConstructors()[0];
int paramCount = constructor.getParameterTypes().length;
Object[] args = new Object[paramCount];
args[0] = delegate;
for (int i = 1; i < paramCount; i++) {
args[i] = new Reference
© 2015 - 2024 Weber Informatics LLC | Privacy Policy