IntelliJ create inner class for selected fields/methods - java

I have a big class containing a lot of definitions.
Is there a way to move/create/extract a structure using IntelliJs build in features?
So i can keep the references to those fields without having to add qualifiers for all usages?
Current is case:
public static class Tables {
public final static String METADATA_CREATE_TS = "create_ts";
public final static String TABLE_1_ID;
public final static String TABLE_1_NAME;
public final static String TABLE_1_INTERNAL_ID;
public final static String TABLE_1_CUSTOMER_ID;
.
.
.
public final static String TABLE_N_ID;
}
How it should look like:
public static class Tables {
public final static String METADATA_CREATE_TS = "create_ts";
public static class Table1 {
public final static String TABLE_1_ID;
public final static String TABLE_1_NAME;
public final static String TABLE_1_INTERNAL_ID;
public final static String TABLE_1_CUSTOMER_ID;
}
}

Create the inner class.
Go to class Tables --> right click --> move members
Mark the fields that you want to move.
In To write the inner class name (...Tables.Table1)
Click Refactor

This is doable with a little bit of trickery.
You can use F6 in IntelliJ to move a set number of fields to a new class. After you created the new class you can than select all of the newly created classes and press F6 again to make all of those classes an inner class of a different class, which solved my overall goal.

Related

Is it possible to initialize a static final variable from a configuration file in Java?

I'd like to initialize a static final variable from configuration file, in Java. The variable is such that:
public static final String SCHEMA = "my_schema";
Since the schema can change, I'd like to read it from configuration file. However, I must use a final variable because there's no other way in JPA to inizialize "schema" attribute in "Table" annotation. The point is that because of past choices each environment has been located in the same DB with different schema names.
UPD - edited for a better solution
The only work-around I can think of is by having another not final private String variable and a static init block before the line with your final variable. For example the following code will yield different final variable after initialization
public class TestSmth {
private static String yourCustomSchema;
static {
yourCustomSchema = Math.random() > 0.5 ? "my_schema" : "another_schema";
}
public static final String SCHEMA = yourCustomSchema;
public static void main(String[] args) {
System.out.println(SCHEMA);
}
}
You should implement config file loading in the static block
This way after the class is loaded you have your final variable initialized and have no access to the non-final private variable.

how can i get properties in static class

static class PushConfig{
#Value("${jpush.mq.appKey}")
private String appKeyMQ;
}
like this, I want to get config in inner Class,it's not work
Create a new class with this code:
public static anyName = yourClass
Then you can use anyName.anyMethod(...) to use a method

Two different .jar with same Package name and same Class name. How to get the one?

In my project I have standard java system library with rt.jar that have class.
package javax.xml;
public final class XMLConstants {
private XMLConstants() {
}
public static final String NULL_NS_URI = "";
public static final String DEFAULT_NS_PREFIX = "";
...
}
And also included is stax-api-1.0.jar with class
package javax.xml;
public class XMLConstants {
public static final String DEFAULT_NS_PREFIX = "";
public static final String XML_NS_PREFIX = "xml";
public static final String XML_NS_URI = "http://www.w3.org/XML/1998/namespace";
public static final String XMLNS_ATTRIBUTE = "xmlns";
public static final String XMLNS_ATTRIBUTE_NS_URI = "http://www.w3.org/2000/xmlns/";
}
And in third class I need to get NULL_NS_URI that looks like this
import javax.xml.XMLConstants;
public myClass(){
doSomethin() {
...
XMLConstants.NULL_NS_URI
...
}
}
It gives error
NULL_NS_URI cannot be resolved or is not a field
And when I ctrl + click on XMLConstants in myClass eclipse takes me to class stax-api.jar.
When I do same thing on colleague machine eclipse take him to rt.jar with no error because NULL_NS_URI is defined in that class.
Check your build class path order. If the rt.jar resides above the stax-api-1.0.jar, the problem should get resolved. Because, while compilation, the compiler looks up the jars in the build class path order.

How do I load an variable from a java class to a different class

I'm trying to load a String from topicRNG to changeXML. I've loaded variables between classes before but can't get it to work now.
Firstly I have my code where I try to load it.
package XMLTest;
public class ModifyTTXML {
public static void main(String args[]){
TopicRNG.main();
String something = TopicRNG.topicFinal;
...
And then the code where I try to load it,
import java.util.Random;
public final class TopicRNG {
public static final void main(String... aArgs){
String lastTopic = "empty";
int lastTopicNumber; //genre ska importeras från GameSetup screenen
Random randomGenerator = new Random();
...
if(GenreDefiner.genre<=1){
System.out.println(topicName[lastTopicNumber]);
topicFinal = topicName[lastTopicNumber]; }
When I loaded the int from GenreDefiner I had it set up like this,
public class GenreDefiner {
public static int genre = 1;
}
I tried "putting public static String topicFinal" and it gave me an error, when I instead put it outside of the "public static void main(String args[]){}" it worked fine. So I'm guessing the public static in "public static void main(String args[]){" is the thing messing it up. What should I do?
What you should do is pass values as arguments to methods and try to minimize using static variables except as global constants.
You can't declare a static variable inside a method, it has to be within the class declaration but outside of any method declaration.

Initialization on demand for multiple static variables without static nested classes?

I want to have a class with multiple static variables that will only be initialized on demand.
public class Messages {
public static final String message1 = init1();
public static final String message2 = init2();
}
So when somewhere in the code I reference Messages.message1 I want only init1() to be called. If later I access Messages.message2 then only at that time should init2() be called.
I know it is possible to do this with the Initialization-on-demand holder idiom, but this is cumbersome if you have lots of fields.
Is there another way?
Most common way for lazy initialization is initialization in getter method:
public class Messages {
private static String message1;
public static String getMessage1() {
if (message1 == null)
message1 = init1();
return message1;
}
}
If you want exactly public final static fields then there is no way to achieve separate initialization for them in Java. All class members are initialized together.

Categories

Resources