@Nullable public static JetTypeInfo getRecordedTypeInfo( @NotNull JetExpression expression, @NotNull BindingContext context) { if (!context.get(BindingContext.PROCESSED, expression)) return null; DataFlowInfo dataFlowInfo = BindingContextUtilPackage.getDataFlowInfo(context, expression); JetType type = context.get(BindingContext.EXPRESSION_TYPE, expression); return JetTypeInfo.create(type, dataFlowInfo); }
@Nullable public static JetTypeInfo getRecordedTypeInfo( @NotNull JetExpression expression, @NotNull BindingContext context) { // noinspection ConstantConditions if (!context.get(BindingContext.PROCESSED, expression)) return null; // NB: should never return null if expression is already processed JetTypeInfo result = context.get(BindingContext.EXPRESSION_TYPE_INFO, expression); return result != null ? result : TypeInfoFactoryPackage.noTypeInfo(DataFlowInfo.EMPTY); }
public static boolean isVarCapturedInClosure( BindingContext bindingContext, DeclarationDescriptor descriptor) { if (!(descriptor instanceof VariableDescriptor) || descriptor instanceof PropertyDescriptor) return false; VariableDescriptor variableDescriptor = (VariableDescriptor) descriptor; return bindingContext.get(CAPTURED_IN_CLOSURE, variableDescriptor) != null && variableDescriptor.isVar(); }
public String getElementName() { IObservable model = context.getModel(); if (elementName == null && model != null && model.getType() == IObservable.OBSERVE_SWT_JFACE) { Object source = model.getSource(); elementName = getWidgetName(source); } return elementName; }
@NotNull public static JetType getTypeNotNull( @NotNull BindingContext bindingContext, @NotNull JetExpression expression) { JetType result = bindingContext.getType(expression); if (result == null) { throw new IllegalStateException("Type must be not null for " + expression); } return result; }
@NotNull public static <K, V> V getNotNull( @NotNull BindingContext bindingContext, @NotNull ReadOnlySlice<K, V> slice, @NotNull K key, @NotNull String messageIfNull) { V value = bindingContext.get(slice, key); if (value == null) { throw new IllegalStateException(messageIfNull); } return value; }
@Nullable public static VariableDescriptor extractVariableDescriptorIfAny( @NotNull BindingContext bindingContext, @Nullable JetElement element, boolean onlyReference) { DeclarationDescriptor descriptor = null; if (!onlyReference && (element instanceof JetVariableDeclaration || element instanceof JetParameter)) { descriptor = bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, element); } else if (element instanceof JetSimpleNameExpression) { descriptor = bindingContext.get(BindingContext.REFERENCE_TARGET, (JetSimpleNameExpression) element); } else if (element instanceof JetQualifiedExpression) { descriptor = extractVariableDescriptorIfAny( bindingContext, ((JetQualifiedExpression) element).getSelectorExpression(), onlyReference); } if (descriptor instanceof VariableDescriptor) { return (VariableDescriptor) descriptor; } return null; }
@NotNull public static DeclarationDescriptor getEnclosingDescriptor( @NotNull BindingContext context, @NotNull JetElement element) { JetNamedDeclaration declaration = PsiTreeUtil.getParentOfType(element, JetNamedDeclaration.class); if (declaration instanceof JetFunctionLiteral) { return getEnclosingDescriptor(context, declaration); } DeclarationDescriptor descriptor = context.get(DECLARATION_TO_DESCRIPTOR, declaration); assert descriptor != null : "No descriptor for named declaration: " + declaration.getText() + "\n(of type " + declaration.getClass() + ")"; return descriptor; }
/** * Invoke the method and return a Publisher for the return value. * * @param exchange the current exchange * @param bindingContext the binding context to use * @param providedArgs optional list of argument values to check by type (via {@code instanceof}) * for resolving method arguments. * @return Publisher that produces a single HandlerResult or an error signal; never throws an * exception */ public Mono<HandlerResult> invokeForRequest( ServerWebExchange exchange, BindingContext bindingContext, Object... providedArgs) { return resolveArguments(exchange, bindingContext, providedArgs) .then( args -> { try { Object value = doInvoke(args); ModelMap model = bindingContext.getModel(); HandlerResult handlerResult = new HandlerResult(this, value, getReturnType(), model); return Mono.just(handlerResult); } catch (InvocationTargetException ex) { return Mono.error(ex.getTargetException()); } catch (Throwable ex) { String s = getInvocationErrorMessage(args); return Mono.error(new IllegalStateException(s)); } }); }
public static FunctionDescriptor getEnclosingFunctionDescriptor( @NotNull BindingContext context, @NotNull JetElement element) { JetFunction function = PsiTreeUtil.getParentOfType(element, JetFunction.class); return (FunctionDescriptor) context.get(DECLARATION_TO_DESCRIPTOR, function); }
public boolean canBinding() { return context.isValid(); }
public Property getTargetProperty() { return context.getTargetProperty(); }
public IObservable getTarget() { return context.getTarget(); }
public Property getModelProperty() { return context.getModelProperty(); }
public IObservable getModel() { return context.getModel(); }
public Command bindWithCommand() { if (!canBinding()) { return null; } CompoundCommand commandList = new CompoundCommand(); Property targetProperty = context.getTargetProperty(); Property modelProperty = context.getModelProperty(); IObservable target = context.getTarget(); IObservable model = context.getModel(); Object targetSource = target.getSource(); Object modelSource = model.getSource(); XamlNode node = null; if (targetSource instanceof Widget) { node = XWTProxy.getModel((Widget) targetSource); } else if (targetSource instanceof XamlElement) { node = (XamlElement) targetSource; } if (node == null) { return UnexecutableCommand.INSTANCE; } // 1. Get attr of targetProperty, such as < text="{}"/> XamlAttribute attribute = node.getAttribute(targetProperty.getName(), IConstants.XWT_NAMESPACE); if (attribute != null) { commandList.add(new DeleteCommand(attribute)); attribute = null; } if (attribute == null) { attribute = XamlFactory.eINSTANCE.createAttribute(targetProperty.getName(), IConstants.XWT_NAMESPACE); } // 2. Binding Node. if (bindingNode == null) { bindingNode = attribute.getChild(BINDING, IConstants.XWT_NAMESPACE); } if (bindingNode == null) { bindingNode = XamlFactory.eINSTANCE.createElement(BINDING, IConstants.XWT_NAMESPACE); } // 3. ElementName int type = model.getType(); if (type == IObservable.OBSERVE_SWT_JFACE) { addAttr( bindingNode, ELEMENT_NAME, IConstants.XWT_NAMESPACE, getCreateName(modelSource, commandList)); } // 4. DataContext else if (IObservable.OBSERVE_JAVA_BAEN == type) { XamlDocument ownerDocument = node.getOwnerDocument(); XamlElement rootElement = ownerDocument.getRootElement(); DataContext dataContext = (DataContext) modelSource; String key = dataContext.getKey(); if (key != null) { boolean canUseDC = getCodeStyles().useDataContext; if (canUseDC) { XamlAttribute contextNode = getAttribute(rootElement, DATA_CONTEXT, IConstants.XWT_NAMESPACE); contextNode.setUseFlatValue(true); XamlElement resourceNode = getElement(contextNode, STATIC_RESOURCE, IConstants.XWT_NAMESPACE); if (!key.equals(resourceNode.getValue())) { if (resourceNode.getValue() != null) { canUseDC = false; } else { resourceNode.setValue(key); commandList.add(new AddNewChildCommand(contextNode, resourceNode)); commandList.add(new AddNewChildCommand(rootElement, contextNode)); } } } if (!canUseDC || !getCodeStyles().useDataContext) { XamlAttribute sourceAttr = getAttribute(bindingNode, SOURCE, IConstants.XWT_NAMESPACE); sourceAttr.setUseFlatValue(true); XamlElement resourceNode = getElement(sourceAttr, STATIC_RESOURCE, IConstants.XWT_NAMESPACE); resourceNode.setValue(key); commandList.add(new AddNewChildCommand(sourceAttr, resourceNode)); commandList.add(new AddNewChildCommand(bindingNode, sourceAttr)); } } } // 5. Path addAttr(bindingNode, PATH, IConstants.XWT_NAMESPACE, modelProperty.toString()); // 6. BindingMode if (bindingMode != null && BindingMode.TwoWay.equals(bindingMode)) { addAttr(bindingNode, MODE, IConstants.XWT_NAMESPACE, bindingMode.name()); } // 7. Converter. if (converter != null) { addAttr(bindingNode, CONVERTER, IConstants.XWT_NAMESPACE, converter); } // 8. UpdateSourceTrigger if (triggerMode != null) { addAttr(bindingNode, UPDATE_SOURCE_TRIGGER, IConstants.XWT_NAMESPACE, triggerMode.name()); } // 9. Code Style. attribute.setUseFlatValue(getCodeStyles().useFlatVlaue); if (!attribute.getChildNodes().contains(bindingNode)) { attribute.getChildNodes().add(bindingNode); } if (!node.getAttributes().contains(attribute)) { commandList.add(new AddNewChildCommand(node, attribute)); } return commandList.unwrap(); }
@Nullable public static ResolvedCall<ConstructorDescriptor> getDelegationConstructorCall( @NotNull BindingContext bindingContext, @NotNull ConstructorDescriptor constructorDescriptor) { return bindingContext.get(CONSTRUCTOR_RESOLVED_DELEGATION_CALL, constructorDescriptor); }
public void execute( XFormsActionInterpreter actionInterpreter, Element actionElement, Scope actionScope, boolean hasOverriddenContext, Item overriddenContext) { final IndentedLogger indentedLogger = actionInterpreter.indentedLogger(); final XFormsContainingDocument containingDocument = actionInterpreter.containingDocument(); final XFormsContextStack contextStack = actionInterpreter.actionXPathContext(); final String atAttribute = actionElement.attributeValue("at"); final String originAttribute = actionElement.attributeValue("origin"); final String contextAttribute = actionElement.attributeValue(XFormsConstants.CONTEXT_QNAME); // Extension: allow position to be an AVT final String resolvedPositionAttribute = actionInterpreter.resolveAVT(actionElement, "position"); // Extension: xxf:default="true" AVT requires that recalculate apply default values on the // inserted nodes. final boolean setRequireDefaultValues = "true" .equals( actionInterpreter.resolveAVT( actionElement, XFormsConstants.XXFORMS_DEFAULTS_QNAME)); // "2. The Node Set Binding node-set is determined." final List<Item> collectionToBeUpdated; { final BindingContext currentBindingContext = contextStack.getCurrentBindingContext(); collectionToBeUpdated = currentBindingContext.newBind() ? currentBindingContext.nodeset() : XFormsConstants.EMPTY_ITEM_LIST; } final boolean isEmptyNodesetBinding = collectionToBeUpdated == null || collectionToBeUpdated.size() == 0; // "1. The insert context is determined." // "The insert action is terminated with no effect if [...] a. The context attribute is not // given and the Node // Set Binding node-set is the empty node-set." if (contextAttribute == null && isEmptyNodesetBinding) { if (indentedLogger.isDebugEnabled()) indentedLogger.logDebug("xf:insert", "context is empty, terminating"); return; } // Handle insert context (with @context attribute) final Item insertContextItem; if (hasOverriddenContext) { // "If the result is an empty nodeset or not a nodeset, then the insert action is terminated // with no effect. " if (overriddenContext == null || !(overriddenContext instanceof NodeInfo)) { if (indentedLogger.isDebugEnabled()) indentedLogger.logDebug( "xf:insert", "overridden context is an empty nodeset or not a nodeset, terminating"); return; } else { insertContextItem = overriddenContext; } } else { insertContextItem = contextStack.getCurrentBindingContext().getSingleItem(); } // "The insert action is terminated with no effect if [...] b. The context attribute is given, // the insert // context does not evaluate to an element node and the Node Set Binding node-set is the empty // node-set." // NOTE: In addition we support inserting into a context which is a document node if (contextAttribute != null && isEmptyNodesetBinding && !DataModel.isElement(insertContextItem) && !DataModel.isDocument(insertContextItem)) { if (indentedLogger.isDebugEnabled()) indentedLogger.logDebug( "xf:insert", "insert context is not an element node and binding node-set is empty, terminating"); return; } // "3. The origin node-set is determined." final List<Item> originObjects; { if (originAttribute == null) { originObjects = null; } else { // There is an @origin attribute // "If the origin attribute is given, the origin node-set is the result of the evaluation of // the // origin attribute in the insert context." originObjects = actionInterpreter.evaluateKeepItems( actionElement, Collections.singletonList(insertContextItem), 1, originAttribute); // "The insert action is terminated with no effect if the origin node-set is the empty // node-set." if (originObjects.size() == 0) { if (indentedLogger.isDebugEnabled()) indentedLogger.logDebug("xf:insert", "origin node-set is empty, terminating"); return; } } } // "4. The insert location node is determined." int insertionIndex; { if (isEmptyNodesetBinding) { // "If the Node Set Binding node-set empty, then this attribute is ignored" insertionIndex = 0; } else if (atAttribute == null) { // "If the attribute is not given, then the default is the size of the Node Set Binding // node-set" insertionIndex = collectionToBeUpdated.size(); } else { // "a. The evaluation context node is the first node in document order from the Node Set // Binding // node-set, the context size is the size of the Node Set Binding node-set, and the context // position is 1." // "b. The return value is processed according to the rules of the XPath function round()" final String insertionIndexString = actionInterpreter.evaluateAsString( actionElement, collectionToBeUpdated, 1, "round(" + atAttribute + ")"); // "c. If the result is in the range 1 to the Node Set Binding node-set size, then the // insert // location is equal to the result. If the result is non-positive, then the insert location // is // 1. Otherwise, the result is NaN or exceeds the Node Set Binding node-set size, so the // insert // location is the Node Set Binding node-set size." // Don't think we will get NaN with XPath 2.0... insertionIndex = "NaN".equals(insertionIndexString) ? collectionToBeUpdated.size() : Integer.parseInt(insertionIndexString); // Adjust index to be in range if (insertionIndex > collectionToBeUpdated.size()) insertionIndex = collectionToBeUpdated.size(); if (insertionIndex < 1) insertionIndex = 1; } } final String normalizedPosition; { if (resolvedPositionAttribute == null) { // Default value normalizedPosition = "after"; } else if ("after".equals(resolvedPositionAttribute) || "before".equals(resolvedPositionAttribute)) { // Specified value normalizedPosition = resolvedPositionAttribute; } else { // Invalid value if (indentedLogger.isInfoEnabled()) indentedLogger.logWarning( "xf:insert", "invalid position attribute, defaulting to \"after\"", "value", resolvedPositionAttribute); normalizedPosition = "after"; } } doInsert( containingDocument, indentedLogger, normalizedPosition, collectionToBeUpdated, (NodeInfo) insertContextItem, originObjects, insertionIndex, true, true, setRequireDefaultValues); }