JavaBean Introspection with Optional return types












1















java.beans.Introspector#getBeanInfo


compiles an incomplete PropertyDescriptor when changing the getter return type to com.google.common.base.Optional.



I'm using Java 7 and thus have to use Guava's Optional. I'd like to use it as return types in my JavaBeans.
I've prepared these two small unit tests to outline the problem:



import static org.junit.Assert.*;    
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import org.junit.Test;
import com.google.common.base.Optional;

public class BeanOptionalTest {

class SimpleBean {
private String foo;
public String getFoo() { return foo; }
public void setFoo(String foo) { this.foo = foo; }
}

@Test
public void test_SimpleBean() throws Exception {
assertFooProperty(SimpleBean.class);
}

class OptionalBean {
private String foo;
public Optional<String> getFoo() { return Optional.fromNullable(foo); }
public void setFoo(String foo) { this.foo = foo; }
}

@Test
public void test_OptionalBean() throws Exception {
assertFooProperty(OptionalBean.class);
}

private void assertFooProperty(Class<?> beanClass) throws IntrospectionException {
BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
assertEquals(2, beanInfo.getPropertyDescriptors().length);
PropertyDescriptor fooDescriptor = beanInfo.getPropertyDescriptors()[1];
assertNotNull(fooDescriptor.getReadMethod());
assertEquals("getFoo", fooDescriptor.getReadMethod().getName());
assertNotNull(fooDescriptor.getWriteMethod());
assertEquals("setFoo", fooDescriptor.getWriteMethod().getName());
}
}


test_OptionalBean fails because the write method is null. Presumably the Introspector matches foo to the type Optional instead of String.



How can this behaviour be changed in order to receive a complete PropertyDescriptor despite changing the return type to Optional?










share|improve this question



























    1















    java.beans.Introspector#getBeanInfo


    compiles an incomplete PropertyDescriptor when changing the getter return type to com.google.common.base.Optional.



    I'm using Java 7 and thus have to use Guava's Optional. I'd like to use it as return types in my JavaBeans.
    I've prepared these two small unit tests to outline the problem:



    import static org.junit.Assert.*;    
    import java.beans.BeanInfo;
    import java.beans.IntrospectionException;
    import java.beans.Introspector;
    import java.beans.PropertyDescriptor;
    import org.junit.Test;
    import com.google.common.base.Optional;

    public class BeanOptionalTest {

    class SimpleBean {
    private String foo;
    public String getFoo() { return foo; }
    public void setFoo(String foo) { this.foo = foo; }
    }

    @Test
    public void test_SimpleBean() throws Exception {
    assertFooProperty(SimpleBean.class);
    }

    class OptionalBean {
    private String foo;
    public Optional<String> getFoo() { return Optional.fromNullable(foo); }
    public void setFoo(String foo) { this.foo = foo; }
    }

    @Test
    public void test_OptionalBean() throws Exception {
    assertFooProperty(OptionalBean.class);
    }

    private void assertFooProperty(Class<?> beanClass) throws IntrospectionException {
    BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
    assertEquals(2, beanInfo.getPropertyDescriptors().length);
    PropertyDescriptor fooDescriptor = beanInfo.getPropertyDescriptors()[1];
    assertNotNull(fooDescriptor.getReadMethod());
    assertEquals("getFoo", fooDescriptor.getReadMethod().getName());
    assertNotNull(fooDescriptor.getWriteMethod());
    assertEquals("setFoo", fooDescriptor.getWriteMethod().getName());
    }
    }


    test_OptionalBean fails because the write method is null. Presumably the Introspector matches foo to the type Optional instead of String.



    How can this behaviour be changed in order to receive a complete PropertyDescriptor despite changing the return type to Optional?










    share|improve this question

























      1












      1








      1








      java.beans.Introspector#getBeanInfo


      compiles an incomplete PropertyDescriptor when changing the getter return type to com.google.common.base.Optional.



      I'm using Java 7 and thus have to use Guava's Optional. I'd like to use it as return types in my JavaBeans.
      I've prepared these two small unit tests to outline the problem:



      import static org.junit.Assert.*;    
      import java.beans.BeanInfo;
      import java.beans.IntrospectionException;
      import java.beans.Introspector;
      import java.beans.PropertyDescriptor;
      import org.junit.Test;
      import com.google.common.base.Optional;

      public class BeanOptionalTest {

      class SimpleBean {
      private String foo;
      public String getFoo() { return foo; }
      public void setFoo(String foo) { this.foo = foo; }
      }

      @Test
      public void test_SimpleBean() throws Exception {
      assertFooProperty(SimpleBean.class);
      }

      class OptionalBean {
      private String foo;
      public Optional<String> getFoo() { return Optional.fromNullable(foo); }
      public void setFoo(String foo) { this.foo = foo; }
      }

      @Test
      public void test_OptionalBean() throws Exception {
      assertFooProperty(OptionalBean.class);
      }

      private void assertFooProperty(Class<?> beanClass) throws IntrospectionException {
      BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
      assertEquals(2, beanInfo.getPropertyDescriptors().length);
      PropertyDescriptor fooDescriptor = beanInfo.getPropertyDescriptors()[1];
      assertNotNull(fooDescriptor.getReadMethod());
      assertEquals("getFoo", fooDescriptor.getReadMethod().getName());
      assertNotNull(fooDescriptor.getWriteMethod());
      assertEquals("setFoo", fooDescriptor.getWriteMethod().getName());
      }
      }


      test_OptionalBean fails because the write method is null. Presumably the Introspector matches foo to the type Optional instead of String.



      How can this behaviour be changed in order to receive a complete PropertyDescriptor despite changing the return type to Optional?










      share|improve this question














      java.beans.Introspector#getBeanInfo


      compiles an incomplete PropertyDescriptor when changing the getter return type to com.google.common.base.Optional.



      I'm using Java 7 and thus have to use Guava's Optional. I'd like to use it as return types in my JavaBeans.
      I've prepared these two small unit tests to outline the problem:



      import static org.junit.Assert.*;    
      import java.beans.BeanInfo;
      import java.beans.IntrospectionException;
      import java.beans.Introspector;
      import java.beans.PropertyDescriptor;
      import org.junit.Test;
      import com.google.common.base.Optional;

      public class BeanOptionalTest {

      class SimpleBean {
      private String foo;
      public String getFoo() { return foo; }
      public void setFoo(String foo) { this.foo = foo; }
      }

      @Test
      public void test_SimpleBean() throws Exception {
      assertFooProperty(SimpleBean.class);
      }

      class OptionalBean {
      private String foo;
      public Optional<String> getFoo() { return Optional.fromNullable(foo); }
      public void setFoo(String foo) { this.foo = foo; }
      }

      @Test
      public void test_OptionalBean() throws Exception {
      assertFooProperty(OptionalBean.class);
      }

      private void assertFooProperty(Class<?> beanClass) throws IntrospectionException {
      BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
      assertEquals(2, beanInfo.getPropertyDescriptors().length);
      PropertyDescriptor fooDescriptor = beanInfo.getPropertyDescriptors()[1];
      assertNotNull(fooDescriptor.getReadMethod());
      assertEquals("getFoo", fooDescriptor.getReadMethod().getName());
      assertNotNull(fooDescriptor.getWriteMethod());
      assertEquals("setFoo", fooDescriptor.getWriteMethod().getName());
      }
      }


      test_OptionalBean fails because the write method is null. Presumably the Introspector matches foo to the type Optional instead of String.



      How can this behaviour be changed in order to receive a complete PropertyDescriptor despite changing the return type to Optional?







      java javabeans guava optional introspection






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 20 at 14:14









      MorrowMorrow

      89112




      89112
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Your test breaks because the type of the parameter of OptionalBean::setFoo doesn't match the return type of OptionalBean::getFoo. A PropertyDescriptor is specific to a type and here, Optional<String> and String are not the same type, even if conceptually they are strongly linked.



          With the current implementation of JavaBeans, you can't do what you want to do. What you could do is write your own BeanInfo-like object that does the introspection for you but doesn't implement BeanInfo.






          share|improve this answer
























          • Thanks very much for your answer. I was hoping for an easier solution or some extension point of the Introspector, but apparently I have to go all the way.

            – Morrow
            Jan 21 at 15:26











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277319%2fjavabean-introspection-with-optional-return-types%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Your test breaks because the type of the parameter of OptionalBean::setFoo doesn't match the return type of OptionalBean::getFoo. A PropertyDescriptor is specific to a type and here, Optional<String> and String are not the same type, even if conceptually they are strongly linked.



          With the current implementation of JavaBeans, you can't do what you want to do. What you could do is write your own BeanInfo-like object that does the introspection for you but doesn't implement BeanInfo.






          share|improve this answer
























          • Thanks very much for your answer. I was hoping for an easier solution or some extension point of the Introspector, but apparently I have to go all the way.

            – Morrow
            Jan 21 at 15:26
















          2














          Your test breaks because the type of the parameter of OptionalBean::setFoo doesn't match the return type of OptionalBean::getFoo. A PropertyDescriptor is specific to a type and here, Optional<String> and String are not the same type, even if conceptually they are strongly linked.



          With the current implementation of JavaBeans, you can't do what you want to do. What you could do is write your own BeanInfo-like object that does the introspection for you but doesn't implement BeanInfo.






          share|improve this answer
























          • Thanks very much for your answer. I was hoping for an easier solution or some extension point of the Introspector, but apparently I have to go all the way.

            – Morrow
            Jan 21 at 15:26














          2












          2








          2







          Your test breaks because the type of the parameter of OptionalBean::setFoo doesn't match the return type of OptionalBean::getFoo. A PropertyDescriptor is specific to a type and here, Optional<String> and String are not the same type, even if conceptually they are strongly linked.



          With the current implementation of JavaBeans, you can't do what you want to do. What you could do is write your own BeanInfo-like object that does the introspection for you but doesn't implement BeanInfo.






          share|improve this answer













          Your test breaks because the type of the parameter of OptionalBean::setFoo doesn't match the return type of OptionalBean::getFoo. A PropertyDescriptor is specific to a type and here, Optional<String> and String are not the same type, even if conceptually they are strongly linked.



          With the current implementation of JavaBeans, you can't do what you want to do. What you could do is write your own BeanInfo-like object that does the introspection for you but doesn't implement BeanInfo.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 21 at 9:05









          Olivier GrégoireOlivier Grégoire

          15.2k1663104




          15.2k1663104













          • Thanks very much for your answer. I was hoping for an easier solution or some extension point of the Introspector, but apparently I have to go all the way.

            – Morrow
            Jan 21 at 15:26



















          • Thanks very much for your answer. I was hoping for an easier solution or some extension point of the Introspector, but apparently I have to go all the way.

            – Morrow
            Jan 21 at 15:26

















          Thanks very much for your answer. I was hoping for an easier solution or some extension point of the Introspector, but apparently I have to go all the way.

          – Morrow
          Jan 21 at 15:26





          Thanks very much for your answer. I was hoping for an easier solution or some extension point of the Introspector, but apparently I have to go all the way.

          – Morrow
          Jan 21 at 15:26




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277319%2fjavabean-introspection-with-optional-return-types%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre