Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmarshal proper types for the elements of Collections ? #66

Open
fjf2002 opened this issue Feb 9, 2018 · 1 comment
Open

Unmarshal proper types for the elements of Collections ? #66

fjf2002 opened this issue Feb 9, 2018 · 1 comment

Comments

@fjf2002
Copy link

fjf2002 commented Feb 9, 2018

Is it possible to unmarshal proper types for the elements of Collections ?

Example:
The generated Excel Workbooks interface implements Iterable<Com4jObject> instead of Iterable<_Workbook>. As far as I understand, I have to call .queryInterface(_Workbook.class) on each element manually:

for (final Com4jObject p : excel.getWorkbooks()) {
	System.out.println(p.queryInterface(_Workbook.class).getName());
}

Is it possible to get that conversion automagically? I think of something like the following...

  • I manually change the signatures of class Workbooks and its method iterator to the generic type _Workbook.

  • The above does not seem to be enough: My JVM dumps when I try that. So I think we need at least ComCollection's fetch method to call queryInterface to cast to the proper class?

@fjf2002
Copy link
Author

fjf2002 commented Feb 9, 2018

My current ugly workaround is

for (final _Workbook p : iterableCaster(excel.getWorkbooks(), _Workbook.class)) {
	System.out.println(p.getName());
}

... with the method iterableCaster that simply adds a queryInterface call to the iterator.next call:

public static <T extends Com4jObject> Iterable<T> iterableCaster(
	final Iterable<Com4jObject> comIterable, final Class<T> clazz) {

	return new Iterable<T>() {

		@Override
		public Iterator<T> iterator() {
			return new Iterator<T>() {
				final Iterator<Com4jObject> delegate = comIterable.iterator();

				@Override
				public T next() {
					return delegate.next().queryInterface(clazz);
				}

				@Override
				public boolean hasNext() {
					return delegate.hasNext();
				}
			};
		}
	};
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant