Exception while using sortby method

96 views
Skip to first unread message

Hemal Bavishi

unread,
Dec 3, 2014, 5:52:35 AM12/3/14
to xtend...@googlegroups.com
Hello,

I am trying to sort the instances based on the handle IDs but I am getting an exception as soon as I use sortby method. I am not sure the exception is caused due to java class I am using or due to some other problem. 

#CANSM_COMM_FIRST_NETWORK_INDEX «instanceCanSM.canSMConfiguration.canSMManagerNetworks.sortBy[e|e.canSMComMNetworkHandleRef.comMChannelId].get(0).canSMComMNetworkHandleRef.comMChannelId»

Any clue?

Best Regards,
Hemal Bavishi


 


Hemal Bavishi

unread,
Dec 3, 2014, 6:06:23 AM12/3/14
to xtend...@googlegroups.com
java.lang.ClassCastException: autosar40.ecucdescription.impl.EcucContainerValueImpl cannot be cast to org.artop.ecuc.autosar421.accessors.CanSM$CanSMConfiguration$CanSMManagerNetwork
at org.artop.ecuc.examples.autosar40.transform.xtend.generator.BSWGenerator$8.apply(BSWGenerator.java:1)
at org.eclipse.xtext.xbase.lib.internal.KeyComparator.compare(KeyComparator.java:40)
at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.ArrayList.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at org.eclipse.xtext.xbase.lib.ListExtensions.sortInplaceBy(ListExtensions.java:82)
at org.eclipse.xtext.xbase.lib.IterableExtensions.sortBy(IterableExtensions.java:699)
at org.artop.ecuc.examples.autosar40.transform.xtend.generator.BSWGenerator.null(Unknown Source)
at org.artop.ecuc.examples.autosar40.transform.xtend.generator.BSWGenerator.bswGenerate(Unknown Source)
at org.avin.taskbar.autosar40.codegen.xpand.ui.handlers.SampleHandler.generateCodeForResource(SampleHandler.java:173)
at org.avin.taskbar.autosar40.codegen.xpand.ui.handlers.SampleHandler.execute(SampleHandler.java:79)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:294)
at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:55)
... 38 more

Sven Efftinge

unread,
Dec 3, 2014, 6:07:10 AM12/3/14
to xtend...@googlegroups.com
What kind of exception is it?
Stack trace?

--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian Dietrich

unread,
Dec 3, 2014, 9:15:28 AM12/3/14
to xtend...@googlegroups.com
Hi,

are you sure your input model is correct?  you could debug org.artop.ecuc.autosar421.accessors.CanSM.CanSMConfiguration.getCanSMManagerNetworks()

Hemal Bavishi

unread,
Dec 3, 2014, 9:27:38 AM12/3/14
to xtend...@googlegroups.com
I am not 100% sure but I am able to access parameters using other method e.g. get

instanceCanSM.canSMConfiguration.canSMManagerNetworks.get(0).canSMComMNetworkHandleRef.comMChannelId 

I am getting an exception only when I try to use with "sortby" method

Sven Efftinge

unread,
Dec 3, 2014, 9:29:06 AM12/3/14
to xtend...@googlegroups.com
You should debug that code.
A "Java Exception Breakpoint" on ClassCastException is probably a good start.

Christian Dietrich

unread,
Dec 3, 2014, 10:05:24 AM12/3/14
to xtend...@googlegroups.com
maybe the bad guy is the second or third in the list

Hemal Bavishi

unread,
Dec 3, 2014, 10:44:50 AM12/3/14
to xtend...@googlegroups.com
Hello,

I tried to debug to figure out the problem. It seems that I am not getting the key value correctly from xtext library(import org.eclipse.xtext.xbase.lib.Functions.Function1).  

I am getting an exception when I try to execute the following line of code:

List<CanSM.CanSMConfiguration.CanSMManagerNetwork> _sortBy = IterableExtensions.<CanSM.CanSMConfiguration.CanSMManagerNetwork, Integer>sortBy(_canSMManagerNetworks_8, _function_4);

on further investigation I found that key value which is part of _function_4 variable is not getting generated correctly. 

the value of _function_4 "org.artop.ecuc.examples.autosar40.transform.xtend.generator.BSWGenerator$8@200bdba4"

This value (_function_4 ) is being fetched from standard library function called Function1 which is part of xtext library. 

final Function1<CanSM.CanSMConfiguration.CanSMManagerNetwork, Integer> _function_4 = new Function1<CanSM.CanSMConfiguration.CanSMManagerNetwork, Integer>() {
      public Integer apply(final CanSM.CanSMConfiguration.CanSMManagerNetwork e) {
        ComM.ComMConfigSet.ComMChannel _canSMComMNetworkHandleRef = e.getCanSMComMNetworkHandleRef();
        return _canSMComMNetworkHandleRef.getComMChannelId();
      }
    };

However method "apply" which is being called by sort eventually is part of bswGenerateHeader in a class called BswGenerator. IMHO, I should get _function_4 value as "org.artop.ecuc.examples.autosar40.transform.xtend.generator.BSWGenerator.bswGenerateHeader$8@200bdba4 but I am not sure.

Best Regards,
Hemal Bavishi

Sven Efftinge

unread,
Dec 3, 2014, 10:49:22 AM12/3/14
to xtend...@googlegroups.com
For me it is really hard to follow, given all the cryptic names and the unclear context.
If you like us to help, you should come up with a small reproducible scenario.

Cheers,
Sven

Hemal Bavishi

unread,
Dec 3, 2014, 11:20:24 AM12/3/14
to xtend...@googlegroups.com, efft...@itemis.de
Hmmmm.. I tried my best to explain you. Well, I must admit that I am not an expert in Java + Xtend.

Let me try once again. 

I am getting an exception when following line of code is executed in the program. 

List<CanSM.CanSMConfiguration.CanSMManagerNetwork> _sortBy = IterableExtensions.<CanSM.CanSMConfiguration.CanSMManagerNetwork, Integer>sortBy(_canSMManagerNetworks_8, _function_4);

Basically above functionality invokes a method listed below which is part of Xtext library

public static <T, C extends Comparable<? super C>> List<T> sortBy(Iterable<T> iterable,
final Functions.Function1<? super T, C> key) {
return ListExtensions.sortInplaceBy(Lists.newArrayList(iterable), key);
}

sortBy method has two parameters: First one is "Iterable" and Second one is "key". IMHO, the value of the "key" is not getting generated correctly. If I trace back, the value of the key is passed by _function_4 variable in java file. 

final Function1<CanSM.CanSMConfiguration.CanSMManagerNetwork, Integer> _function_4 = new Function1<CanSM.CanSMConfiguration.CanSMManagerNetwork, Integer>() {

Opinion:

I think "key" element is used to call method "apply" to sort Iterable based on the functionality available in the apply method. apply method is actually part of bswGenerateHeader function in the class called BSWGenerator. However the value of the key which is used to invoke method "apply" does not have bswGenerateHeader in the content. I am suspecting that this could be the root cause of the problem I am facing.

I will try to create small example if the problem statement is still not clear. Alternatively I can setup a webex meeting if you are available for few minutes tomorrow. 

Best Regards,
Hemal Bavishi 

Christian Dietrich

unread,
Dec 3, 2014, 11:25:26 AM12/3/14
to xtend...@googlegroups.com, efft...@itemis.de
please change your code to

[Object e| 
if (e instanceof CanSMManagerNetwork) {
 e.canSMComMNetworkHandleRef.comMChannelId
} else {
42
}]

and debug for the else part
.

Hemal Bavishi

unread,
Dec 3, 2014, 12:28:18 PM12/3/14
to xtend...@googlegroups.com, efft...@itemis.de
It does not call apply method at all!!! I am getting an exception when it try to call apply method

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Xtend code 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
«"#define CANSM_COMM_FIRST_NETWORK_INDEX" + instanceCanSM.canSMConfiguration.canSMManagerNetworks.sortBy[Object e| if(e instanceof CanSMManagerNetwork) {e.canSMComMNetworkHandleRef.comMChannelId} else {42}].get(0).canSMComMNetworkHandleRef.comMChannelId»

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Java code
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CanSM.CanSMConfiguration _canSMConfiguration_8 = instanceCanSM.getCanSMConfiguration();
    List<CanSM.CanSMConfiguration.CanSMManagerNetwork> _canSMManagerNetworks_8 = _canSMConfiguration_8.getCanSMManagerNetworks();
    final Function1<Object, Integer> _function_4 = new Function1<Object, Integer>() {
      public Integer apply(final Object e) {
        Integer _xifexpression = null;
        if ((e instanceof CanSM.CanSMConfiguration.CanSMManagerNetwork)) {
          ComM.ComMConfigSet.ComMChannel _canSMComMNetworkHandleRef = ((CanSM.CanSMConfiguration.CanSMManagerNetwork)e).getCanSMComMNetworkHandleRef();
          _xifexpression = _canSMComMNetworkHandleRef.getComMChannelId();
        } else {
          _xifexpression = Integer.valueOf(42);
        }
        return _xifexpression;
      }
    };
    List<CanSM.CanSMConfiguration.CanSMManagerNetwork> _sortBy = IterableExtensions.<CanSM.CanSMConfiguration.CanSMManagerNetwork, Integer>sortBy(_canSMManagerNetworks_8, _function_4);
    CanSM.CanSMConfiguration.CanSMManagerNetwork _get_2 = _sortBy.get(0);
    ComM.ComMConfigSet.ComMChannel _canSMComMNetworkHandleRef = _get_2.getCanSMComMNetworkHandleRef();
    Integer _comMChannelId = _canSMComMNetworkHandleRef.getComMChannelId();
    String _plus = ("#define CANSM_COMM_FIRST_NETWORK_INDEX" + _comMChannelId);
    _builder.append(_plus, "");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    return _builder;
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Look at this line of code C c1 = keyFunction.apply(a); => I am getting exception exactly at this point

At this instance keyFunction value is "org.artop.ecuc.examples.autosar40.transform.xtend.generator.BSWGenerator@3f61f9f0"
the value of "a" is "autosar40.ecucdescription.impl.EcucContainerValueImpl@48ac11cc (shortName: CanSMManagerNetwork_PropulsionCANhs) (index: <unset>)"

when I step in it jumps to first line of java file (probably due to unavailability of the apply function in class BSWGenerator). Further step in, program jumps inside my class "bswGenerate" where exception is handled. 

Best Regards,
Hemal Bavishi

"Dr. Jan Köhnlein"

unread,
Dec 3, 2014, 2:12:05 PM12/3/14
to xtend...@googlegroups.com, efft...@itemis.de
How about adding a newline in front of every dot and debugging line by line?
It doesn’t seem to be related to Xtend.
> --
> You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


Dr. Jan Köhnlein
Senior Software Architekt

Mobile: +49 (0) 151 / 17396687
Telefon: +49 (0) 431 / 99026870
Fax: +49 (0) 431 / 99026872

http://www.itemis.de
jan.ko...@itemis.de


itemis AG
Niederlassung Kiel
Am Germaniahafen 1
24143 Kiel
http://www.itemis.de/

Rechtlicher Hinweis:

Amtsgericht Dortmund, HRB 20621

Vorstand: Jens Wagener (Vors.), Wolfgang Neuhaus, Dr. Georg Pietrek, Jens Trompeter, Sebastian Neus

Aufsichtsrat: Dr. Burkhard Igel (Vors.), Stephan Grollmann, Michael Neuhaus

Christian Dietrich

unread,
Dec 4, 2014, 1:01:46 AM12/4/14
to xtend...@googlegroups.com
But it should not be a ecuccontainervalue. This is why i say your arxml is invalid

Stefan Oehme

unread,
Dec 4, 2014, 3:07:33 AM12/4/14
to xtend...@googlegroups.com
Christian is right, that list contains invalid instances. As a side note: Please don't do sortBy[].get(0). Use the more efficient minBy[]

Christian Dietrich

unread,
Dec 4, 2014, 5:09:17 AM12/4/14
to xtend...@googlegroups.com
i debugged the code. the basic problem is that the 

BasicWrappingEList and the way how xtend generates the sortBy dont work together.

the artop wrapper classes seem to incomplete in their contract.

you may file a bug against artop for that.

i think a possible solution could be to actually wrap the list e.g. by using the following extension

def <T> List<T> wrap(List<T> unwrapped) {

val result = <T>newArrayList

var counter = 0

while(counter < unwrapped.size) {

result.add(unwrapped.get(counter))

counter++

}

result

}


println('''«canSM.canSMConfiguration.canSMManagerNetworks.wrap.sortBy[e|e.canSMComMNetworkHandleRef.comMChannelId].get(0).canSMComMNetworkHandleRef.comMChannelId»''')





Hemal Bavishi

unread,
Dec 4, 2014, 6:18:25 AM12/4/14
to xtend...@googlegroups.com
Before sortby If I convert it into iterator and then list; it generates the output as expected

Thank you all for your extended support to resolve this issue:)

Best Regards,
Hemal Bavishi
Reply all
Reply to author
Forward
0 new messages