Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to get the java.lang.reflect.Field of (Eg. Object.field1), but my program is going to be obfuscated. I can't use the name of the field, but I just want to get it by that reference. I also don't want to cycle through the array of fields and find the one I want.
Sorry if I worded things wrong, I don't know what I'm supposed to call that.
You have painted yourself into a corner.
There is no way to get a field (or Field) using reflection if you don't know the field's obfuscated name AND you don't want to (or can't) cycle through all of the fields to identify the correct one.
I have a couple of suggestions:
Alter the obfuscation rules; e.g. don't obfuscate this class, or the field names for this class, or this specific field name.
Add a method for accessing this field so that you don't need to use reflection.
You could also figure out what the obfuscated name of the field is going to be and hard-wire it into your reflective code, but this is a bad idea. Small changes to your code are liable to make the obfuscated field name change. Then your code breaks.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Lets say I have class Report and I want to add a functionality printReport(...) and shouldBePrinted(...). Printing it requires GeneralPrinter and LanguageTranslator which are given from outside. Furthermore, I should add members to make the shouldBePrintable method more optimized.
The way I see it there are three ways of doing it:
The simplest is to just add the members and functions to the Report class.
Create PrintableReport which extends Report and adds those members and functions.
Use the decorator pattern to add the needed functionality. (Not sure about that one. Please correct me if this is not the correct way to use a decorator.)
Am I missing some and which is the correct method to do it?
Consider: Separation of concerns
At a HIGH level...
While it's not clear exactly what role Report fills, one might surmise it represents information organized in some fashion.
Rendering is a separate concern. Often you'll want multiple ways to render: Generate PDF, HTML, XML, and/or print (postscript, other...).
So, perhaps you have multiple classes to work with Report, GeneralPrinter, ReportPrinter, ...
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm working on a project where several classes have the same error checking in one of their methods. Should I extract this error checking into a different class, even though it's only under 10 lines of code?
As an example, the method in all the classes take in the same object and does something different with it, I'm checking initially if that object is null and then getting a list from the object to see if it's empty. I was thinking of extracting these checks because I've heard each method should only do one thing.
As a general rule of thumb, you should always extract identical functionality in your codebase and reuse them instead of copying & pasting over. If you have to change the validation logic you'll have to do a shotgun surgery if it is appearing in multiple methods.
There are some battle-tested open-source libraries available for null checking and determining if collections are empty. Look around before implementing your own. Here is an example from the Guava library.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have just had a not let me sleep doubt about how to name a local variable which stores the text entered by an user (in a text box).
Should I name it 'enteredText' or 'textEntered'?
My main concern is about what comes first? participleSubject or subjectParticiple.
There are some accepted patterns as in events, where it is always onSubjectVerb, e.g. onItemClick ...
I am not sure which is the styling rule for my case (entering text in a TextBox'
I have made some searches on different code repositories and both names are used?
Any idea?
Thanks!
Miguel Ángel
This problem is one of the two hardest problems in computer science, naming things. I think that both are equally non descriptive of what the text that has been entered describes.
For example: what does this text field provide the use the ability to enter? Their name? Then call it userName
More of a matter of taste, I think.
Anyway for me it is a matter of context: if I'm writing a complex class or method with lots of variables and where e.g. there's text entered, text computed and text fixed (i.e. a label), I tend to name them textEntered, textComputed and textFixed, so when I need to access some kind of text the IDE auto completion features easily show me all texts available.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I recently started a project in Java, that contains a class called System. This class (Luckily) contains methods for output management, so in the rare cases where I need to use the System. methods (Or the System object in general) I just reference it as java.lang.System.. I believe that this could be looked down upon, as System could be looked at as a reserved name. I currently am in the beginning stages of this program, and could change it accordingly quickly, as there are little calls to the class itself.
While it's not illegal, you don't want to do this. If I were the next person working on your code, the first thing I would do is try to remove "java.lang" from "java.lang.System" and then get miffed when it wouldn't compile.
The idea is to go toward brevity and only write what you need to write, while making sense of it all for the next person. It's more an art than a science.
You could always name it something like ProjectnamehereSystem or OutputManager or something to that effect.
I would not create something so similarly named as an important class. While everything is easy to edit, you may be able to keep up with all the changes you are making.
But when the project evolves things will get messy and complex. I would suggest naming it something else that can be easily distinguished.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Like in Scala? Is there any pattern in Java to avoid having all the boilerplate setter/getter without using 3rd party jars? Thanks
update:
my aim is to avoid having too many ghost methods for Dtos, thanks
If all you want is to avoid having to type, use auto generation feature of IDEs like some others mentioned.
If you want to avoid seeing getter/setter in your classes, use a library called lombok which can generate the getters/setters behind the scene
If the above options are not OK for you and you need to set the value only once, you can declare all your fields as public final and have a constructor setting the values.
CAUTION: I am not suggesting this third option as a good practice as it breaks the Javabeans convention. Also it exposes your class' internal structure, but honestly, even with prolific use of getters, you are exposing class fields to the client.
There are a lot of IDE's out there for java that will help you with this problem.
In Eclipse, when you right click on a variable, you can choose Source -> Generate Getters and Setters.
I never used Scala but this should do.
in idea intellij u can create as AlT+ insert and then choose getter+setter