Eclipse code formatter customization - eclipse

I've recently configured my Eclipse Formatter and I'm pretty happy with how it works, except for one thing. I'd like to set it up so my member variables are sorted into 3 distinct group with a blank line between each of the groups. I want the first group to be static final variables (constants), the second to be regular static variables, and in the third I want all the non-static variables (or again split final and non-final ones, I don't mind either way).
For example, I'd like my class to look like this:
public class Foo {
public static final String PATH_TO_BAR = "C:/Drunken/Clam/Bar";
public static final int N_BARS = 42;
public static BufferedWriter logger;
public int foobar;
public String barfoo;
private int lengthOfBarFoo;
...
}
but right now it formats it as
public class Foo {
public static final String PATH_TO_BAR = "C:/Drunken/Clam/Bar";
public static BufferedWriter logger;
private int lengthOfBarFoo;
public String barfoo;
public static final int N_BARS = 42;
public int foobar;
...
}
Is this possible to set up somehow?

Eclipse save actions might come handy here, however not to such of a detail you've been asking for.

i think you can do that via below :
in eclipse you can sort "Members Sort Order" preference:
"Window -> Preferences -> Java -> Appearance -> Members Sort Order"
you can up and down the choice according to your need.
if you have any query or doubt, comment below.

Related

What are the "built-in" filter options for ag-grid?

I see a reference to a built-in filter for "empty" in the doc below. Where can I find documentation for all built-in filter options?
https://www.ag-grid.com/javascript-grid-filtering/#adding-custom-filter-options
I couldn't find it in the documentation either.
However, you can look at the definition of the BaseFilter of the ag-grid here on the GitHub and get all the built-in filters.
export abstract class BaseFilter<T, P extends IFilterParams, M> extends Component implements IFilterComp {
public static EMPTY = 'empty';
public static EQUALS = 'equals';
public static NOT_EQUAL = 'notEqual';
public static LESS_THAN = 'lessThan';
public static LESS_THAN_OR_EQUAL = 'lessThanOrEqual';
public static GREATER_THAN = 'greaterThan';
public static GREATER_THAN_OR_EQUAL = 'greaterThanOrEqual';
public static IN_RANGE = 'inRange';
public static CONTAINS = 'contains'; //1;
public static NOT_CONTAINS = 'notContains'; //1;
public static STARTS_WITH = 'startsWith'; //4;
public static ENDS_WITH = 'endsWith'; //5;
// .....
}

Way to find all affected locations by variable

I want to find code lines, where a certain property can get.
Consider I have property holder class and in java editor look to some setter method. In code is property readed and setted to entity. And before save to database, do validation on server side. So I want see from client.propertyHolder.setter to server.entityValidator.getter
Simple example
public class Main{
public static void main(String[] args) {
Holder h = new Holder();
h.setHolder("Something"); // Here i want use plugin
Entity e = new Entity();
e.setName(h.getHolder());
// Consider this is on server side in validator and call only if annotation validation is OK
Assert.assertTrue(e.getName().length() > 0)
}
}
public class Holder{
public String holder = null;
public void setHolder(String holder){this.holder = holder}
public void getHolder(){return holder}
}
public class Entity{
#javax.validation.constraints.NotNull
private String name = null;
public void setName(String name){this.name = name}
public void getName(){return name}
}
So i should get lines
e.setName(h.getHolder());
Assert.assertTrue(e.getName().length() > 0)
and be nice if NotNull anotations too
Is there a way, how to achive this? Some good free eclipse-plugin maybye?
Thanks for help. Pavel

Test cases in maven and eclipse

I use maven for managing dependencies in my project.
I have seen while writing test cases that some of them pass in eclipse while fail on maven build. I debugged it and found that there are static final members of classes being initialized once retain their values throughout the build. It is a multi-module project.
Is it possible to change the value of those final members for different test cases?
Please ask me if you want more clarification.
Any links/hints or ideas may be helpful.
Thanks.
The static final members are the Java way of expressing constants. First try to modify your test in such a way that it works with the values of these constants. If this is not possible, you can add a second constructor for testing purpose that overrides these values. See the following example:
Existing code:
public class SomeClass {
private static final int LIMIT = 30;
public SomeClass() {
...
}
public void doSomething() {
... //the code that uses LIMIT.
}
}
Add a second constructor that is used by the test:
public class SomeClass {
private static final int DEFAULT_LIMIT = 30;
private final limit
public SomeClass() {
this(DEFAULT_LIMIT);
}
public SomeClass(int limit) {
this.limit = limit;
...
}
public void doSomething() {
... //the code uses limit now.
}
}

How to ignore a specific warning in PyDev?

How do I ignore a specific warning in Eclipse?
I am doing ZetCode's PyQt4 tutorial LPTHW style (yes, I'm using PyDev), and adding helpful comments so I can use it as a reference. Eclipse is bugging me about an unused variable. (It is being used, because the initialization function automatically runs the code. Just to be clear.)
I don't want to turn it off for the whole file, because that is actually handy in most situations. I just want to ignore that warning.
For suppressing warnings on a specific line, your only option is to add a comment such as ##UnusedVariable to the line:
def foo():
x = 5 # #UnusedVariable
return 10
To suppress a different type of warning, see the list of suppression string constants in the PyDev source code:
public static final String MSG_TO_IGNORE_TYPE_UNUSED_IMPORT = "#UnusedImport";
public static final String MSG_TO_IGNORE_TYPE_UNUSED_WILD_IMPORT = "#UnusedWildImport";
public static final String MSG_TO_IGNORE_TYPE_UNUSED_VARIABLE = "#UnusedVariable";
public static final String MSG_TO_IGNORE_TYPE_UNDEFINED_VARIABLE = "#UndefinedVariable";
public static final String MSG_TO_IGNORE_TYPE_DUPLICATED_SIGNATURE = "#DuplicatedSignature";
public static final String MSG_TO_IGNORE_TYPE_REIMPORT = "#Reimport";
public static final String MSG_TO_IGNORE_TYPE_UNRESOLVED_IMPORT = "#UnresolvedImport";
public static final String MSG_TO_IGNORE_TYPE_NO_SELF = "#NoSelf";
public static final String MSG_TO_IGNORE_TYPE_UNDEFINED_IMPORT_VARIABLE = "#UndefinedVariable";
public static final String MSG_TO_IGNORE_TYPE_UNUSED_PARAMETER = "#UnusedVariable";
public static final String MSG_TO_IGNORE_TYPE_NO_EFFECT_STMT = "#NoEffect";
public static final String MSG_TO_IGNORE_TYPE_INDENTATION_PROBLEM = "#IndentOk";
public static final String MSG_TO_IGNORE_TYPE_ASSIGNMENT_TO_BUILT_IN_SYMBOL = "#ReservedAssignment";
public static final String MSG_TO_IGNORE_TYPE_PEP8 = "#IgnorePep8";
public static final String MSG_TO_IGNORE_TYPE_ARGUMENTS_MISATCH = "#ArgumentMismatch";
For example:
def get_answer(format='string'): # #ReservedAssignment
answer = 42.0
if format == 'string':
return str(answer)
elif format == 'int':
return int(answer)
else:
return answer

How should I design a game character class with a large number of attributes?

I am a student who is currently learning Java and trying to write a small text-based RPG game with it. The first problem I encountered in the design of the game is the 'character' class, which represents all the playable heroes and enemy characters, and is now implemented by myself as the following:
class RPGActor {
private String name;
private int HP; // hit points
private int MP; // mana
private int AP; // attack
private int DP; // defense
... // followed by tens of other attributes.
public Actor(int actorID)
{
... // Reads all attributes from a file based on the 'actorID'.
}
public void printStatus()
{
System.out.println(name);
System.out.println("HP :" + HP);
System.out.println("MP :" + MP);
... // And print all the attributes one by one.
}
public void setHP(int newHP)
{
HP = newHP;
}
public int getHP()
{
return HP;
}
public void setMP(...)
{
...
}
// And tens of accessors and mutators for each attribute
}
The problem I see with this design is that there are too many things that needs to be hand-coded: There are some 20-30 attributes in the class, and a separate accessor/mutator needs to be implemented for each of them. And the function for displaying the current status of the hero, printStatus, must output each attribute separately even though every line of output follows the exact same format. This makes the class definition tediously long.
Plus, if later I want to add one more attribute to the game, then I must remember to modify 'printStatus', and add a pair of accessor/mutator for it.
So my question is: is there a way to design the class so that I can use ONE pair of set/get functions to set all attributes. Something like:
public void set(String attribName, int attribVal)
{
...
}
and print the attributes iteratively like this:
public void printStatus()
{
System.out.println(name);
for (...)
System.out.println(curAttribName + ": " + curAttribVal);
}
Thank you very much!
I'll show you a solution, to set all attributes with one method, but you shouldn't use it, I'll tell you the reasons afterwards.
class RPGActor {
private static final String NAME = "Name";
private static final String HP = "HP";
private static final String MP = "MP";
private static final String AP = "AP";
private static final String DP = "DP";
// ... followed by tens of other attributes.
private Map<String, Object> attributes = new HashMap<String, Object>();
public RPGActor(int actorID) {
this.attributes.put(NAME, nameFromFile);
// ... Reads all attributes from a file based on the 'actorID'.
}
public void setAttribute(String attributeName, Object value) {
this.attributes.put(attributeName, value);
}
public int getAttribute(String attributeName) {
return this.attributes.get(attributeName);
}
}
This has several disadvantages:
no code-completion for setting specific attributes
less readable
...
BETTER:
Although you are using classes and objects, this isn't very object-oriented. Especially you're violating the encapsulation paradigm.
You shouldn't set the HP explicitly from outside the class itself. Only in rare use cases that is needed. Instead you should think about what the actor really does: attacking, defending, casting spells.
Therefore it should look more like this:
class RPGActor {
private String name;
private int HP; // hit points
private int MP; // mana
private int AP; // attack
private int DP; // defense
// ... followed by tens of other attributes.
private Map<String, Object> attributes = new HashMap<String, Object>();
public RPGActor(int actorID) {
// ... Reads all attributes from a file based on the 'actorID'.
}
public void attacks(RPGActor defender) {
defender.defend(this.getAttack());
}
public void defend(Attack attack) {
switch (attack.getType()) {
case PHYSICAL:
// This actor is resistant against physical attacks.
return;
case MAGICAL:
this.HP = this.HP - attack.getStrength();
break;
}
}
public void castSpell(Spell spell, Set<Target> targets) {
// targets could be other actors, equipment or chickens dependent on the spell
// ...
}
}
With inheritance or more advanced design patterns like the Strategy pattern you can make each actor react differently on attacks.