Page 1 of 1

how to add method body programmatically?

PostPosted: Sun Dec 04, 2005 1:33 pm
by Luciana Alvite
Hi,

I'm writing a plugin to create some methods when a action is fired
(getters/setters, foe example). it's working ok, but when I generate the
code I get this kind of getters and setters:

private boolean state = true;
private String name = null;

public void setState( boolean state )
{

}

public boolean isState( )
{
return false;
}

public void setName( String name )
{

}

public String getName( )
{
return null;
}

I'd like to know if it's possible to programmatically edit a method
body, so that in this case the code would have real getters and setters:

private boolean state = true;
private String name = null;

public void setState( boolean state )
{
this.state = state;
}

public boolean isState( )
{
return state;
}

public void setName( String name )
{
this.name = name;
}

public String getName( )
{
return name;
}

I've searched the API Guide and the docs, but did not find anything
plausible.

Thanks in advance,

Luciana Alvite

RE: how to add method body programmatically?

PostPosted: Mon Dec 05, 2005 3:49 am
by Tomas Pugzlys
Hello,

You need to create "getter/setter" tagged values for your methods.
This way you tell code generation that method is setter or getter.

MagicDraw user can specify tagged values through specification dialogs.

In the plugin code you can do it in the following way:

SessionManager.getInstance().createSession("setter/getter");

//class with attribute 'name' and getName/setName methods.
com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Class c = ....;
Property nameAttr = (Property) c.getOwnedAttribute().get(0);

Operation nameGetter = (Operation) c.getOwnedOperation().get(0);
Operation nameSetter = (Operation) c.getOwnedOperation().get(1);

Project project = c.getProject();
Stereotype stereotype = StereotypesHelper.getStereotype(project,
UML2Constants.UML_SETTER_STEREOTYPE);
StereotypesHelper.addStereotype(nameSetter, stereotype);
StereotypesHelper.setStereotypePropertyValue(nameSetter, stereotype,
UML2Constants.GETTER_SETTER_FOR_ATTRIBUTE, nameAttr);

stereotype = StereotypesHelper.getStereotype(project,
UML2Constants.UML_GETTER_STEREOTYPE);
StereotypesHelper.addStereotype(nameGetter, stereotype);
StereotypesHelper.setStereotypePropertyValue(nameGetter, stereotype,
UML2Constants.GETTER_SETTER_FOR_ATTRIBUTE, nameAttr);

SessionManager.getInstance().closeSession();

Regards,
--
Tomas Pugzlys
Programmer
No Magic Lithuanian Development Center
Savanoriu pr. 363, LT-49425, Kaunas
P.O. box 2166, LT-44011, Kaunas
Phone: +370 37 324032 Fax: +370 37 320670
e-mail : tomasp
WWW: http://www.magicdraw.com


Re: how to add method body programmatically?

PostPosted: Thu Dec 08, 2005 10:27 am
by Luciana Alvite
Hi,

I tried setting the stereotype to setter/getter. it works on the
diagram, the operations are shown as <<setter>> <<getter>>. but when I
geneate the code, all I get is an empty class! (only package
declaration). sth like this:

/**
* @(#) MyTest.java
*/

package tests;

but the diagram is showing the methods! I tried also adding other
methods to the class, but when I regenerate the file I get the same
empty file. I deleted the previous one just to be sure a new one was
being created.
and actually this is also the result when I use the built in "Create
Setters/Getters"

what I do is the following: I first iterate through the tree to get all
selected classes and then put them on an Arraylist. the I iterate
through the selected classes and for all private attributes I generate a
new operations (get.. and set..) and set their stereotypes as u
recommended. this is what its done to all selected classes:

private void generateAccessors(Class ci) {
// list of operations to be created
ArrayList created = new ArrayList();

// new session....
SessionManager.getInstance().createSession(
"Create setters and getters for private variables");

try
{
Iterator it = ModelHelper.attributes(ci);
while (it.hasNext()) {
Property at = (Property) it.next();
if (at.getVisibility().equals(VisibilityKindEnum.PRIVATE)) {
String attName = at.getName();

// uppercase the first letter of attribute name.
attName = attName.toUpperCase().substring(0, 1)
+ attName.substring(1, attName.length());

// create setter
// setter name.
String setterName = "set" + attName;

ElementsFactory elementsFactory =
ci.getProject().getElementsFactory();
Operation op = elementsFactory.createOperationInstance();
op.setName(setterName);

// create new parameter of attribute type and name.
Parameter par = elementsFactory.createParameterInstance();
par.setName(at.getName());
par.setType(at.getType());

//set operation type to setter
Stereotype stereotype =
StereotypesHelper.getStereotype(ci.getProject(),
UML2Constants.UML_SETTER_STEREOTYPE);
StereotypesHelper.addStereotype(op, stereotype);
StereotypesHelper.setStereotypePropertyValue(op, stereotype,
UML2Constants.GETTER_SETTER_FOR_ATTRIBUTE, at);


// add parameter into operation.
ModelElementsManager.getInstance().addElement(par, op);

// checking for already existing operations.
boolean exists = false;
Iterator opit = ModelHelper.operations(ci);
while (opit.hasNext() && !exists) {
Operation operation = (Operation) opit.next();
// use this method to compare two operations.
exists = exists || ModelHelper.isOperationEqual(op, operation);
}

// add new operation only if such one do not exists.
if (!exists) {
created.add(op);
} else {
op.dispose();
}
...............................
then sth similar for the getters and at the end:
// add all operations into class.
for (Iterator ite = created.iterator(); ite.hasNext();) {
Operation operation = (Operation) ite.next();
ModelElementsManager.getInstance().addElement(operation, ci);
}

any ideas what the problem is? does it have anything to do with the fact
that the version I'm using is just a demo version? it's enterprise
edition though.

Thanks and regards,

Luciana Alvite

RE: how to add method body programmatically?

PostPosted: Fri Dec 09, 2005 7:37 am
by Tomas Pugzlys
Hello,

I was able to reproduce your reported problem using "Tools -> Create
Setters/Getters" functionality in MagicDraw 10.0 version.

However I was unable to reproduce this problem in MagicDraw 10.5 version.
So, one of the possible solutions how this problem could be solved, is to
download newly released MagicDraw 10.5 version.

Another solution: try to use Java profile in your project (from main menu
select File -> Use Profile/Module ->Java Profile.xml).

Regards,
--
Tomas Pugzlys
Programmer
No Magic Lithuanian Development Center
Savanoriu pr. 363, LT-49425, Kaunas
P.O. box 2166, LT-44011, Kaunas
Phone: +370 37 324032 Fax: +370 37 320670
e-mail : tomasp
WWW: http://www.magicdraw.com