I have a large library of Hibernate-annotated bean classes with inconsistent variable naming. For example, in one class, a variable might be named "acceptanceDate" but in another it might be named "dateAccepted". Each class has a get/set method as well.
I am wondering if there is an easy way to rename these variables across the board AS WELL AS rename their accessor methods. (And even better: open projects in eclipse would update their own references to use the new names).
This is probably a long shot, but I figured it couldn't hurt to ask.
Such a functionality simply does not exist in Eclipse. If I were you, I would write a utility class that would scan the given directory recursively for .java files and then parse them one by one to rename the getter / setter methods and their fields as per the convention needed. Parsing does not have to be full-fledged as you are interested in only renaming the fields and their getter setters that match a specific criteria.
Other option is to manually refactor each instance variable and its getter setter using the Refactor --> Rename dialog of Eclipse.
Related
In MATLAB, a class folder is represented by foo/#bar/ and a package folder is represented by foo/+bar. In my hierarchy, I have classes that define methods in separate files, so the #bar/ convention is necessary for their containing folders. However, I also have methods that get somewhat complex in their implementation, and would like to have them packaged into... well, packages using the +bar/ convention, like so:
foo/#classfolder/MyClass.m
foo/#classfolder/method1.m
foo/#classfolder/method2.m
foo/#classfolder/+othermethodstuff/method2helper.m
foo/#classfolder/+othermethodstuff/mexmethod_formethod2helper.m
foo/#classfolder/+othermethodstuff/mexfiles/
I want to do this because methods in my actual code that are represented here by method2.m rely on some heavy computations from MEX files that I would prefer to reside in their own folder, with the package system used by MATLAB keeping it clear when I am calling those methods (and from where).
Is this possible? If not, is my only other option dropping the # class folder convention and sticking everything into package (+) folders?
You should put those private implementation files in a subdirectory private. That is the traditional location for them. If you want to create some obvious hierarchy to organize code, I recommend long file names.
For example:
foo/#classfolder/MyClass.m
foo/#classfolder/method1.m
foo/#classfolder/method2.m
foo/#classfolder/private/physicssimulation_function1.m
foo/#classfolder/private/physicssimulation_function2.m
foo/#classfolder/private/physicssimulation_mexfile.mex
foo/#classfolder/private/uihelper_functionA.m
foo/#classfolder/private/uihelper_functionB.m
M-files and MEX-files in the private directory can be called from any function in the #classfolder directory, as if they were on the path (i.e. you don’t use private when calling them). But they are private to that directory, and not visible from outside.
The above recommendation assumes multiple class methods use the same private functionality. If only one method uses physicssimulation, then all of its functions should be inside that method’s M-file. It’s the better way of keeping code together.
In SugarCRM, you can create your custom modules (e.g. MyModule) and they are kept in /modules just like stock objects, with any default metadata, views, language files, etc. For a custom module MyModule, you might have something like:
/modules/MyModule/MyModule.class.php
/modules/MyModule/MyModule.php
/modules/MyModule/language/
/modules/MyModule/metadata
And so on, so that everything is nicely defined and all the modules are kept together. The module becomes registered with the system by a file such as /custom/Extension/application/Include/MyModule.php with contents something like:
<?php
$beanList['MyModule'] = 'MyModule';
$beanFiles['MyModule'] = 'modules/MyModule/MyModule.php';
$moduleList[] = 'MyModule';
Obviously, the $beanFiles array references where we can find the base module's class, usually an extension of the SugarBean object. Recently I was advised that we can adjust that file's location for the sake of customization, and it makes sense to a degree. Setting it like $beanFiles['MyModule'] = 'custom/modules/MyModule/MyModule.php'; would allow us to access the base class via Module Loader even if the security scan tool prevents core file changes, and this would also allow us to not exactly extend, but replace stock modules like Accounts or Calls, without modifying core files and having system upgrades to wipe out the changes.
So here's my question: what is the best practice here? I've been working with SugarCRM pretty intensely for several years and this is the first time I've ever been tempted to modify the $beanFiles array. My concern is that I'm deviating from best practice here, and also that somehow both files modules/MyModule/MyModule.php and custom/modules/MyModule/MyModule.php could be loaded which would cause a class name conflict in PHP (i.e. because both classes are named MyModule...). Obviously, any references to the class would need to be updated (e.g. an entryPoint that works with this module), but am I missing any potential ramifications?
Technically it should be fine, but I can see how it could be possible that both the core version and your version could conflict if both are referenced. It all depends on the scenario, but I prefer to extend the core bean and find somewhere in the stack where I can have my custom version used in place of the core bean. I wrote up an example a couple of years ago here: https://www.sugaroutfitters.com/blog/safely-customizing-a-core-bean-in-sugarcrm
For most use-cases, there's a way to hijack Sugar to use your bean at a given point.
If you can't get around it you can always grep to see where the core module is explicitly being included to ensure that there won't be conflict down the road.
Is one forced to place all get and set functions in the class definition file in Matlab ?
I'm asking since this really makes the file a bit messy and defeats the purpose of having a class definition folder.
Yes, if you use property set and get access methods (in fact any method with a dot in the name), you must include them within the classdef file, not in separate files. See the documentation.
However, if you have have a special reason to want to put as much as possible in separate files, you can define methods getMyProp and setMyProp in separate files, and then within the classdef file have the get.myProp and set.myProp functions call them.
If you use them then you need to define them. but you can also define your variables as public.
Im currently doing a lot of testing with JPA entities, where i have to keep calling the setter methods on the entity that looks something like this :
myEntity.setXXX(value);
myEntity.setYYY(value);
myEntity.setZZZ(value);
Is there any magic shortcut or menu in eclipse IDE to generate all the setter-method-calls that starts with "set", like those displayed in the ctrl-space (auto completion) popup (i think the inherited methods from Object are not being shown at popup) ?
So im imagining something like :
i type myEntity.set
and myEntity.set* are generated right away
Im a lazy programmer and currently using Eclipse Helios IDE.
Thank you !
Edit
Im not looking for source -> generate getter and setter, because that would helps me in generating the methods itself. Generating the method calls is what i want to achieve.
I have found the answer (I was always searching for this thing)...
The easiest way is to expand the class members in the "Package Explorer", sort them by name, multi-select all the setters, and then you have in the clipboard all the method names...
;-)
I like #Oscar's answer. It does lead to some cleanup though.
When I paste from the clipboard, I get something that looks like this:
setOne(int)
setTwo(String)
In order to clean this up, I first add semicolons with this search/replace regexp:
search = (.)$
replace = \1;
Then I add the getter calls (assuming incoming data object is named "data"):
search = s(et.*)\(.*
replace = s\1(data.g\1());
This doesn't handle multiple arguments in a method call...
you can use the outline at right side. There you can sort alphabetically or by declaration order using the toolbar button of the view.
and then you can filter out non required this.
From here also you can copy..all setter functions or getters functions names...
There is eclipse plugin to do that. The name of the plugin is **
FastCode
**. There are so many templates. Among those there is template to generate code for create object of the class and all setters method.
Source --> Generate Getters and Setters...
You can also get at it via the Quick Fix command (Ctrl+1) when the cursor is on a property.
EDIT
If you are simply looking for a faster way to copy properties from one object to another I suggest that you look at using reflection. I think this path would be much easier long term then generating the same-looking code over-and-over.
Commons BeanUtils can take away some of the pain in writing pure reflection code. For example, copyProperties takes a destination bean and either another bean or a Map as the source.
eclipse supports naming conventions for fields, parameters and local variables. For each variable type it is possible to configure a list of prefix or suffix or both. eclipse respects this configuration when generating methods or getters/setters.
is there a similar configuration option in netbeans? is there another way to achieve the same thing: i want to get parameters with prefixes, when generating implementations for abstract methods and i want the prefix to be removed, when generating getters/setters (example: for _myVar it should generate getMyVar and setMyVar).
You can use Alt + Insert to generate some feature you need like getter and setter and constructors and ... . when you change something you can use re-factor.