You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2022/05/27 06:54:13 UTC

[GitHub] [netbeans] sdedic commented on a diff in pull request #4157: Added support for properties backed up by functional interfaces

sdedic commented on code in PR #4157:
URL: https://github.com/apache/netbeans/pull/4157#discussion_r883325457


##########
platform/openide.nodes/src/org/openide/nodes/PropertySupport.java:
##########
@@ -87,6 +92,162 @@ static <T> T cast(Class<T> c, Object o) {
         }
     }
 
+    /**
+     * Creates a "virtual" property where getter and setter are backed by the
+     * provided {@link Supplier} and {@link Consumer} functional interfaces.
+     * @param <T> the type of the property
+     * @param name the name of the property
+     * @param valueType the type of the property
+     * @param displayName the display name of the property, can be {@code null}.
+     * @param shortDescription the short description (used in tooltip) of the property, can be {@code null}.
+     * @param supplier the getter functional interface, can be {@code null} for write-only properties.
+     * @param consumer the setter functional interface, can be {@code null} for read-only properties.
+     *
+     * @since 7.62
+     * @return a {@link PropertySupport} instance where getter and setter are
+     *         backed by the provided functional interfaces.
+     */
+    public static <T> PropertySupport<T> readWrite(String name, Class<T> valueType, String displayName, String shortDescription, Supplier<T> supplier, Consumer<T> consumer) {
+        return new FunctionalProperty<>(name, valueType, displayName, shortDescription, supplier, consumer);
+    }
+
+    /**
+     * Creates a "virtual" property where getter and setter are backed by the
+     * provided {@link Supplier} and {@link Consumer} functional interfaces.
+     * @param <T> the type of the property
+     * @param name the name of the property
+     * @param valueType the type of the property
+     * @param displayName the display name of the property, can be {@code null}.
+     * @param supplier the getter functional interface, can be {@code null} for write-only properties.
+     * @param consumer the setter functional interface, can be {@code null} for read-only properties.
+     *
+     * @since 7.62
+     * @return a {@link PropertySupport} instance where getter and setter are
+     *         backed by the provided functional interfaces.
+     */
+    public static <T> PropertySupport<T> readWrite(String name, Class<T> valueType, String displayName, Supplier<T> supplier, Consumer<T> consumer) {

Review Comment:
   Not a review objection, rather a thought to consider.
   
   There are 3 overloads for each flavour (readOnly, readWrite) that serve rather as a convenience methods that do not require the caller to use `null` as argument (displayName, sdescription). Is that necessary ? I mean given that the result is `PropertySupport` = `Node.Property` = `FeatureDescriptor`, then anyone can call `setShortDescription` or `setDisplayName` afterwards (since it's in FeatureDescriptor). Or just pass `null` to the most-args factory method.
   The `null`-passing technique is used to make Functional propsupport readonly or  writeonly anyway.
   
   Another alternative, to make the construction fluent - and with an effect to other potential PropertySupport subclasses would be to add 
   `public final PropertySupport withShortDescription(String s) { setShortDescription(s); return this;}` and the same for display name ... number of characters typed is +- same with the current state, but can be configured in one expression.



##########
platform/openide.nodes/src/org/openide/nodes/PropertySupport.java:
##########
@@ -322,14 +485,13 @@ public void setValue(T val)
         * @return the property editor or <CODE>null</CODE> if there should not be
         *    any editor.
         */
+        @Override
         public PropertyEditor getPropertyEditor() {
             if (propertyEditorClass != null) {
                 try {
-                    return propertyEditorClass.newInstance();
-                } catch (InstantiationException ex) {
+                    return propertyEditorClass.getDeclaredConstructor().newInstance();
+                } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException ex) {

Review Comment:
   some of them can be reduced to a `ReflectiveOperationException` ?



##########
platform/openide.nodes/src/org/openide/nodes/PropertySupport.java:
##########
@@ -360,6 +522,35 @@ public ReadWrite(String name, Class<T> type, String displayName, String shortDes
         }
     }
 
+    private static final class FunctionalProperty<T> extends PropertySupport<T> {
+        private final Supplier<T> supplier;
+        private final Consumer<T> consumer;
+
+        public FunctionalProperty(String name, Class<T> type, String displayName, String shortDescription, Supplier<T> supplier, Consumer<T> consumer) {
+            super(name, type, displayName, shortDescription, supplier != null, consumer != null);
+            this.supplier = supplier;
+            this.consumer = consumer;
+        }
+
+        @Override
+        public T getValue() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {

Review Comment:
   I guess there's no need to declare the exceptions, probably a leftover from a reflective op copypaste.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists