开发者

Transform java.net.URI to org.eclipse.emf.common.util.URI

开发者 https://www.devze.com 2023-03-11 22:56 出处:网络
There are at least two types开发者_运维知识库 of URIs in Java: java.net.URI org.eclipse.emf.common.util.URI

There are at least two types开发者_运维知识库 of URIs in Java:

  • java.net.URI
  • org.eclipse.emf.common.util.URI

I have java.net.URI and need to use the EMF URI. How can I convert the former to the latter?

If I try new URI uri = profileUrl.toURI() I will get a message like this:

Type mismatch: cannot convert from java.net.URI to org.eclipse.emf.common.util.URI

I tried also some kind of workaround that will create a string from the java.net.URI and with the new string a new EMF URI... this lead to a file-not-found exception.


First things first, there are not "two types of URIs in Java": there is only one in Java, and one in EMF which is a framework built for modeling. They have their own implementation of URI because this reflects one of their own concepts, or they needed more than Java's URI allows, or... there can be a number of reasons for such a choice, and many frameworks provide their own version of such or such class (in Eclipse, use ctrl + shift + T and type "List" or "Array" for examples).

As for the actual question, there is no way to go directly from a java.net.URI to org.eclipse.emf.common.util.URI: you need to convert the Java URI to a string, then create a new URI around this string. Something like this:

java.net.URI javaURI = profileUrl.toURI();
org.eclipse.emf.common.util.URI emfURI = org.eclipse.emf.common.util.URI.createURI(javaURI.toString());

You need to use the fully qualified name of at least one of the two URIs: the one you did not import in your Java class. Judging by your question, I'd say you have imported org.eclipse.emf.common.util.URI, and thus can simply use this:

java.net.URI javaURI = profileUrl.toURI();
URI emfURI = URI.createURI(javaURI.toString());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号