Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Running JavaCode on-the-fly from String(Abstraction level : in the between....)
icon5.gif  Running JavaCode on-the-fly from String [message #1690728] Mon, 30 March 2015 16:43 Go to next message
Eclipse UserFriend
Hi folks,

I'm trying to execute JavaCode from a Java execution - working with JDK 1.8 and EMF APIs : manipulating Ecore metamodels and Ecore-XMI instances.

Before you redirect me on another topic, I'd like to precise that I don't want to use Shell or mathematical expression. I can't actually for I'm trying to build an Ecore containing in its attributes JavaCode to be executed (onto other instances to be precise).

Exemple :
to put a value "length > 4" in an attribute called "name" to check its size.
It could also be a "this.name.length >4" or even more precisely targetted...


Any idea ?

Thanks for your attention.
Hope to read from you very soon.
Edoaurd

PS : oh, and the next question will be : How to execute :
if(name.length <= 4)
   name = name + "______";

To change the structure on the fly...
This is just a very small idea, no matter the meaning of the example, please.

Re: Running JavaCode on-the-fly from String [message #1690754 is a reply to message #1690728] Mon, 30 March 2015 23:46 Go to previous messageGo to next message
Eclipse UserFriend
Comments below.


On 30/03/2015 10:43 PM, Edoaurd Batot wrote:
> Hi folks,
>
> I'm trying to execute JavaCode from a Java execution - working with
> JDK 1.8 and EMF APIs : manipulating Ecore metamodels and Ecore-XMI
> instances.
So you want to interpret the contents of a String as Java code?
>
> Before you redirect me on another topic, I'd like to precise that I
> don't want to use Shell or mathematical expression. I can't actually
> for I'm trying to build an Ecore containing in its attributes JavaCode
> to be executed (onto other instances to be precise).
Perhaps Xcore supports what you need. https://wiki.eclipse.org/Xcore
>
> Exemple :
> to put a value "length > 4" in an attribute called "name" to check its
> size.
> It could also be a "this.name.length >4" or even more precisely
> targetted...
>
>
> Any idea ?
It's really not entirely clear what you're after...
>
> Thanks for your attention.
> Hope to read from you very soon. Edoaurd
>
> PS : oh, and the next question will be : How to execute :
> if(name.length <= 4)
> name = name + "______";
> To change the structure on the fly... This is just a very small idea,
> no matter the meaning of the example, please.
>
>
Re: Running JavaCode on-the-fly from String [message #1690813 is a reply to message #1690754] Tue, 31 March 2015 09:00 Go to previous messageGo to next message
Eclipse UserFriend
So, the way I'm reading this, it seems as though the OP wants to add validation constraints to targets (EStructuralElements? or values thereof? Not sure which...). Said validation constraints will be expressed as Strings. I would assume there could be more than one constraint per target. But it goes beyond that...

There also needs to be some mechanism for correcting validation failures by executing code (contained in another String?). So it's like he's thinking of some micro-language embedded in Strings in the Ecore, with some execution engine invoked by his (generated?) code.

Is that about right, Edoaurd?
Re: Running JavaCode on-the-fly from String [message #1690859 is a reply to message #1690813] Tue, 31 March 2015 11:54 Go to previous messageGo to next message
Eclipse UserFriend
That's pretty much what I'm looking for yes, an embbeded code into String attributes of an Ecore I want to execute for comparing models (or parts of models).

I'm gonna look into XCore, thanks Ed, and I'll turn on post-notifications !

THanks for your replies guys.

[Updated on: Tue, 31 March 2015 11:54] by Moderator

Re: Running JavaCode on-the-fly from String [message #1690864 is a reply to message #1690859] Tue, 31 March 2015 12:15 Go to previous messageGo to next message
Eclipse UserFriend
Well XCore doesn't fit as it seems I'll need months to get the meaning of that new tech.
I really want to do the simple check :
"this.name.length > 4"
which is embedded in a String, and will be executed as-it-is. (we imagine the target properly built, and the code properly written - no exceptions).
Well, if I can't, I'll build a DSL etc. But it won't be much generic then.

Executing this test on a dynamic instance is the final goal : e.g. I'm playing with DynamicEObjectImpl and their EStructuralFeature to go up and down the meta-hierarchy.

Writing this, I wonder whether it is possible without changing the whole structure (as with XCore :s).

See you, and thanks again !
Re: Running JavaCode on-the-fly from String [message #1690875 is a reply to message #1690864] Tue, 31 March 2015 13:50 Go to previous messageGo to next message
Eclipse UserFriend
Why not use JavaScript as the check language?

Tom

On 31.03.15 18:15, Edoaurd Batot wrote:
> Well XCore doesn't fit as it seems I'll need months to get the meaning
> of that new tech.
> I really want to do the simple check :
> "this.name.length > 4" which is embedded in a String, and will be
> executed as-it-is. (we imagine the target properly built, and the code
> properly written - no exceptions).
> Well, if I can't, I'll build a DSL etc. But it won't be much generic then.
>
> Executing this test on a dynamic instance is the final goal : e.g. I'm
> playing with DynamicEObjectImpl and their EStructuralFeature to go up
> and down the meta-hierarchy.
>
> Writing this, I wonder whether it is possible without changing the whole
> structure (as with XCore :s).
>
> See you, and thanks again !
Re: Running JavaCode on-the-fly from String [message #1690879 is a reply to message #1690864] Tue, 31 March 2015 14:04 Go to previous messageGo to next message
Eclipse UserFriend
Comments below.

On 31/03/2015 6:15 PM, Edoaurd Batot wrote:
> Well XCore doesn't fit as it seems I'll need months to get the meaning
> of that new tech.
Well of course you have to learn something new, and there's always the
feeling that you'll better understand what you do from scratch than some
complex thing someone else did.
> I really want to do the simple check :
> "this.name.length > 4" which is embedded in a String, and will be
> executed as-it-is.
In Xcore that would be an operation
> (we imagine the target properly built, and the code properly written -
> no exceptions).
> Well, if I can't, I'll build a DSL etc.
Using Xtext. That will be a few months too. Or from scratch, also
something that will grow until you wish you'd used Xtext.
> But it won't be much generic then.
class Thing
{
String name

op boolean hasValidName()
{
name.length > 4
}
}
>
> Executing this test on a dynamic instance is the final goal : e.g. I'm
> playing with DynamicEObjectImpl and their EStructuralFeature to go up
> and down the meta-hierarchy.
Xcore supports creating dynamic instance and you can use eInvoke to
invoke the operations.
>
> Writing this, I wonder whether it is possible without changing the
> whole structure (as with XCore :s).
>
> See you, and thanks again !
Re: Running JavaCode on-the-fly from String [message #1690894 is a reply to message #1690879] Tue, 31 March 2015 15:38 Go to previous messageGo to next message
Eclipse UserFriend
May I plug the few Xcore features you are talking about in a Java project without rebuilding the entire project ?
Otherwise, you have to understand I can't use it.

class Thing{
String name
op boolean hasValidName(){
name.length > 4
}
}

What's this ? I mean, I undertand it's Xcore code, and I understand it calculates the length of the name of the things. But well, what is it here for ?
I'm using Ecore instances upon which I'd like to execute the code. If the next input is "super.getSurname.startsWith("prefix")", how do I generate the "class Things etc" ?
THe name is also an example, as the type of the name. THe structure containing the checks is a "ramified" one (relax-augment-modify) but the application will act on the real instance : a name could be a class in itself, a reference, a supertype... I can't plug a pseudo class so easy... But I would love you to show me I'm wrong Smile


About JS, it is the same thing : how do I execute the semantics of JS lines into Java ? The "length<4" could be a "this.somethingelse instanceof AnotherClass".
I've never tried to mix java and JS, I'll have a look but from where I am, it seems tricky.

Abstraction is a nasty thing.
Thanks for your help.

[Updated on: Tue, 31 March 2015 15:40] by Moderator

Re: Running JavaCode on-the-fly from String [message #1690896 is a reply to message #1690894] Tue, 31 March 2015 15:44 Go to previous messageGo to next message
Eclipse UserFriend
It is totally easy to execute JavaScript inside Java and access
informations from Java to JavaScript and the other way round.

Same goes for groovy, jython, jruby they are all accessible through the
ScriptEngineManager which is JSRed, JavaScript is shipped with your VM
by default.

Tom

On 31.03.15 21:38, Edoaurd Batot wrote:
> XCore is a factory, I would need a hammer.
>
> May I plug the few lines you are talking about in a Java project without
> rebuilding the entire project ? Otherwise, you have to understand I
> can't use it.
>
> class Thing{
> String name
> op boolean hasValidName(){
> name.length > 4
> }
> }
>
> What's this ? I mean, I undertand it's Xcore code, and I understand it
> calculates the length of the name of the things. But well, what is it
> here for ? I'm using Ecore instances upon which I'd like to execute the
> code. If the next input is "super.getSurname.startsWith("prefix")", how
> do I generate the "class Things etc" ? THe name is also an example, as
> the type of the name. THe structure containing the checks is a
> "ramified" one (relax-augment-modify) but the application will act on
> the real instance : a name could be a class in itself, a reference, a
> supertype... I can't plug a pseudo class so easy... But I would love you
> to show me I'm wrong :s
>
>
> About JS, it is the same thing : how do I execute the semantics of JS
> lines into Java ? The "length<4" could be a "this.somethingelse
> instanceof AnotherClass".
> I've never tried to mix java and JS, I'll have a look but from where I
> am, it seems tricky.
>
> Abstraction is a nasty thing.
> Thanks for your help.
>
Re: Running JavaCode on-the-fly from String [message #1690936 is a reply to message #1690894] Wed, 01 April 2015 01:45 Go to previous message
Eclipse UserFriend
Comments below.

On 31/03/2015 9:38 PM, Edoaurd Batot wrote:
> XCore is a factory, I would need a hammer.
:-)
>
> May I plug the few lines you are talking about in a Java project
> without rebuilding the entire project ? Otherwise, you have to
> understand I can't use it.
>
> class Thing{
> String name
> op boolean hasValidName(){
> name.length > 4
> }
> }
>
> What's this ?
It's an XClass, an EClass, and a GenClass. Open a *.xcore file in the
reflective editor and you'll see it's all three
> I mean, I undertand it's Xcore code, and I understand it calculates
> the length of the name of the things. But well, what is it here for ?
It induces an EOperation that you can EObject.eInvoke.
> I'm using Ecore instances upon which I'd like to execute the code.
Dynamic ones as I understand it...
> If the next input is "super.getSurname.startsWith("prefix")", how do I
> generate the "class Things etc" ? THe name is also an example, as the
> type of the name. THe structure containing the checks is a "ramified"
> one (relax-augment-modify) but the application will act on the real
> instance : a name could be a class in itself, a reference, a
> supertype... I can't plug a pseudo class so easy... But I would love
> you to show me I'm wrong :s
Well, I certainly don't want to do your work for you, but I imagine it
helps to know that a DSL for Ecore has been built with Xtext and the
expression language it uses (Xbase) is as rich as (or arguably richer
then) Java...
>
>
> About JS, it is the same thing : how do I execute the semantics of JS
> lines into Java ? The "length<4" could be a "this.somethingelse
> instanceof AnotherClass".
> I've never tried to mix java and JS, I'll have a look but from where I
> am, it seems tricky.
Yes, that's an interesting idea as well.
>
> Abstraction is a nasty thing.
> Thanks for your help.
>
Previous Topic:Eclipse Wildfly runtime classpath
Next Topic:browse .wsdl extension file.
Goto Forum:
  


Current Time: Sun Apr 20 16:41:44 EDT 2025

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

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

Back to the top