Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » return type String[](how can i return a String array without references)
return type String[] [message #842243] Thu, 12 April 2012 05:42 Go to next message
Ludwig Moser is currently offline Ludwig MoserFriend
Messages: 476
Registered: July 2009
Senior Member
hello!

how can i define return type of String (or any other) array?
eg. if i have a list of objects (reference to other class) which is always a list (i know i could change this to array) and i have another EOperation defined, which gains me (Eg.) the names of those objects.

Class stringArrayClass = Array.newInstance(String.class, 0).getClass();

thats how i can gain the class, but referencing Ljava.lang.String won't work, as it won't work with reflection either - for sure.

thanks in advance
ludwig
Re: return type String[] [message #845614 is a reply to message #842243] Sun, 15 April 2012 08:12 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Ludwig,

Comments below.

On 12/04/2012 7:42 AM, Ludwig Moser wrote:
> hello!
>
> how can i define return type of String (or any other) array?
You have to define an EDataType but like we've done for EByteArray which
wraps byte[] and you have to implement the to and from string conversion
methods in the factory.
> eg. if i have a list of objects (reference to other class) which is
> always a list (i know i could change this to array) and i have another
> EOperation defined, which gains me (Eg.) the names of those objects.
>
> Class stringArrayClass = Array.newInstance(String.class, 0).getClass();
> thats how i can gain the class, but referencing Ljava.lang.String
> won't work, as it won't work with reflection either - for sure.
>
> thanks in advance
> ludwig


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: return type String[] [message #849424 is a reply to message #845614] Thu, 19 April 2012 06:11 Go to previous message
Ludwig Moser is currently offline Ludwig MoserFriend
Messages: 476
Registered: July 2009
Senior Member
so for anyone trying to do the same and no clue what to do

1) create a new DataType
set Name to: EStringArray
set instance Class Name to: java.lang.String[]
check isSerializable

2) build the model
go to yourPackage.ABCFactoryImpl and put these lines in the create/convert-EStringArrayFrom/ToString
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public String[] createEStringArrayFromString(EDataType eDataType, String initialValue) {
		return initialValue.split("\n");
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public String convertEStringArrayToString(EDataType eDataType, Object instanceValue) {
		String[] array = (String[]) instanceValue;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < array.length; i++) {
			sb.append(array[i]);
			if (i<array.length-1) {
				sb.append("\n");
			}
		}
		return sb.toString();
	}

note: you can replace "\n" by anything you are NOT using in the strings...
i used "\n" as my data always contains single line elements, so "\n" is not possible in my case.
finally use your datatype in EMF

thanks ed! works like charm
Previous Topic:Unique ID
Next Topic:how to define variable argument methods
Goto Forum:
  


Current Time: Wed Apr 24 23:28:06 GMT 2024

Powered by FUDForum. Page generated in 0.02874 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top