Netbeans autogenerates the get() function, but my application doen't run if the return doesn't have the this.
Incorrect:
public String getStringVariable(){
return stringVariable;
}
Correct:
public String getStringVariable(){
return this.stringVariable;
}
As I work with many (many) variables its difficult write this in each get. I found I can change App.java but I don't know its location (http://www.fordfrog.com/2010/07/19/modifying-netbeans-generated-getters-and-setters/)
Thank you!
Related
I ran into a very strange problem that I don't know if I'm even allowed to do.
Basically I have two functions witch should have the same name but get different parameter objects which have the same name.
This is because I want to write a plugin for the game Minecraft and this should be compatible with BungeeCord and Bukkit servers.
public static void sendMessage(org.bukkit.command.CommandSender p, String k, Object...i){
//fancy stuff
}
public static void sendMessage(net.md_5.bungee.api.CommandSender p, String k, Object...i){
//fancy stuff
}
If the plugin is loaded by a Bukkit server the plugin it doesn't know anything about net.md_5.bungee.api.CommandSender since this is a class of the BungeeCord server core and the same is for org.bukkit.command.CommandSender where it is used by Bukkit but not by BungeeCore.
I have no problem compiling the code with IntellIJ even dough I'm a bit sceptic because if decompiled it looks like this:
import org.bukkit.command.CommandSender;
public static void sendMessage(CommandSender p, String k, Object...i){
//fancy stuff
}
public static void sendMessage(net.md_5.bungee.api.CommandSender p, String k, Object...i){
//fancy stuff
}
My first question is: Can I even do this, or will this give exceptions since not all Classes are loaded, even dough it will never get accessed?
Now if the first question can be answered by Sure you can then why is there a compilation problem by compiling eigther a Bukkit or a BungeeCord plugin using this sendMessage( function?
Bukkit:
BungeeCord:
Because if this doesn't work I know for sure that you can at least work with Classes that aren't loaded if you put them into your codeblock since this code works just fine and isn't even throwing an exception when not loaded by a server that is using org.bukkit.craftbukkit.v1_13_R2.entity.CraftPlayer aldough it is in the imports:
import org.bukkit.entity.Player;
import org.bukkit.craftbukkit.v1_13_R2.entity.CraftPlayer;
public static int getPing(Player p) {
String version = getVersion(instance.getServer());
if (version.startsWith("v1_8")) {
return ((org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer)p).getHandle().playerConnection.player.ping;
} else if (version.startsWith("v1_9")) {
return ((org.bukkit.craftbukkit.v1_9_R2.entity.CraftPlayer)p).getHandle().playerConnection.player.ping;
} else if (version.startsWith("v1_10")) {
return ((org.bukkit.craftbukkit.v1_10_R1.entity.CraftPlayer)p).getHandle().playerConnection.player.ping;
} else if (version.startsWith("v1_11")) {
return ((org.bukkit.craftbukkit.v1_11_R1.entity.CraftPlayer)p).getHandle().playerConnection.player.ping;
} else if (version.startsWith("v1_12")) {
return ((org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer)p).getHandle().playerConnection.player.ping;
} else {
return ((CraftPlayer)p).getHandle().playerConnection.player.ping;
}
}
So is this really a thing I simply cannot do or is this a problem of the compiler of IntellIJ and if so how can I fix it?
Well my attempt to your idea would be to call methods in sub classes. It might be an issue that the non found class is a parameter. When the class is accessed (just my speculations) the parameters are tried to load to determine which method to use.
So something like the following would be the output.
In the class you access have:
public static void sendMessage(Object player, String k, Object...i){
if(isBukkit())
MyBukkitUtils.sendMessage(player, k, i);
else
MyBungeeUtils.sendMessage(player, k, i);
}
MyBukkitUtils:
public static void sendMessage(Object player, String k, Object...i){
if(!(player instanceOf CommandSender))
return;
CommandSender p = (CommandSender) player;
//fancy stuff
}
Same for MyBungeeUtils just with the BunggeeCommandSender.
I don't know your code, but if you have to have everything seperated you can just code two plugins (one for spigot, one for bungee) and use a include a library in both were the common code is placed.
I'm searching for a concept to forward an object to subobjects.
Example:
I would like to create log files for several main Objects, that include sub objects (imagine a REST server that would log every single connection by ID).
Creating one big log file is simple ( redirect System.out.println, I already encapsulated that)
Example code:
class SubElementA{
public SubElementA(){
Debugger.debug("I am called, too");
}
}
Application.java
package com.dev4ag;
class Application{
private ElementA elA;
private String prefix;
public Application(String name){
this.elA = new ElementA();
this.prefix = name;
}
public void countUp(){
Debugger.debug(this.prefix+": I will now count up");
this.elA.doSomeStuff();
}
}
ElementA.java
package com.dev4ag;
class ElementA{
private int counter;
private SubElementA subElementA;
public void doSomeStuff(){
counter++;
Debugger.debug("Counter is: "+counter);
}
//Constructor
public ElementA(){
subElementA = new SubElementA();
this.counter = 0;
};
}
SubElementA.java
package com.dev4ag;
class SubElementA{
public SubElementA(){
Debugger.debug("I am called, too");
}
}
Debugger.java
package com.dev4ag;
public class Debugger {
public static void debug(String output){
//Just imagine we would write to a file here ;)
System.out.println(output);
}
}
(it was more easy to write system.out.println than to create a file, just imagine, Debugger.debug would write to a file).
Now I am thinking about a solution to create one Debug output target for each App. I could definitely change debug to not being static and create a debug object within Application.
But is there any way to use this object in the sub classes without forwarding the debug object either through Constructor or setter function, which would mean to have to add an object for the debugger to each class?
What would be the most beautiful solution for that?
Note that this solution might decrease performance a lot and it is pretty dirty way, but some loggers include such data.
But you can use Thread.currentThread().getStackTrace() to get stacktrace like in error and get class and method from where your method was called.
If you are using java9+ then you should probably use StackWalker API instead, especially that it have nice filters and other useful features.
So then you could guess app by class/method names on the stack.
This is very strange to me:
1) I have a class
public class Data {
private static String name;
public static String getName() {
return name;
}
public static void setName (String n){
Data.name = n;
}
}
this class variable name is set correctly during my server ( J2EE app in WAS liberty) startup - I wanted to cache this data during the whole server lifecycle.
2) but when I hit an URL that eventually invokes a code that does a getter:
Data.getName(); // this returns null??
Can anyone think of a possible reason? Not be able to debug also, I am very frustrated.
To illustrate my problem more, let me show my project structure:
EAR
CommonJar
WAR1 WAR2
My Data.java is in CommonJar. This Data.java is initialized in WAR1 during the server startup triggered by a Listener registered in web.xml, and from here I saw it is set and I saw the non-null value using Data.getName().-- this is my step 1 in my original post.
Then I have a restful HelloWorld Service in WAR2. And when I call into this HelloWorld service, I see Data.getName() returns null. -- this is my step 2 in my original post. Don't even think anything like syntax type of easy error, because it is not that.
My theory is: it seems that Data.java is loaded by classloader in WAR1 in step 1 and then WAR 2 in my step 2, and Step 2 loaded an empty Data class.
To verify if this theory is correct, I moved Data.java initialization step into the WAR2, (and again I saw it is set and I saw the non-null value using Data.getName()), the difference is that when I call into this HelloWorld service, I saw Data.getName() returns the expected non-null value. This step is what I call my troubleshooting breakthrough because it seems to indicate my theory was correct. (but I can't fix the issue by moving the code in my real problem fix)
Then the question is how to fix and how to visually see which class is loaded by which class loader and when? Also based on WAS Liberty doc, the default class loader will be parentsfirst, which means Data.java should be loaded by the parent ( in this case, EAR), so I shouldn't have my problem the first place.
Anyone, please guide how to further to see the problem and fix it? Thanks.
Your getName() function is declared with no return type, yet attempts to return a string.
try out this one
public class Data {
public static void main(String... args) {
System.out.println("setting name 'Stack Overflow'");
Data.setName("Stack Overflow");
System.out.println("now getting the setted name");
System.out.println(Data.getName());
}
private static String name;
public static String getName() {
return name;
}
public static void setName(String n) {
Data.name = n;
}
}
Output:
setting name 'Stack Overflow'
now getting the setted name
Stack Overflow
In your code, you defined no return types to your methods.
Is there a switch/flag that allows to do this? I spent hours finding those but couldn't find anything that works. The other thing I'm planning to do is intercept the cefRequest by adding my own CefRequestHandler, examine the resource type and if it matches RT_IMAGE, cancel the request. Everything seems easy except the part when I have to cancel a request. How do I stop/block/cancel a cefRequest? I probably should not be doing it this way but it doesn't work anyway:
public class CefClientRequestHandler extends CefRequestHandlerAdapter {
#Override
public boolean onBeforeResourceLoad(CefBrowser cefBrowser, CefFrame cefFrame, CefRequest cefRequest) {
if (cefRequest.getResourceType().equals(CefRequest.ResourceType.RT_IMAGE)) {
cefRequest.setURL("");
}
return false;
}
// more overides
}
Any ideas?
So here's a hack that works. The trick is to change the Request Method to HEAD, and because HEAD requests aren't returned the body, images won't be part of the response.
public class CefClientRequestHandler extends CefRequestHandlerAdapter {
#Override
public boolean onBeforeResourceLoad(CefBrowser cefBrowser, CefFrame cefFrame, CefRequest cefRequest) {
if (cefRequest.getResourceType().equals(RT_IMAGE)) {
cefRequest.setMethod("HEAD");
}
return false;
}
// other overridden methods here...
}
I believe that this approach should be avoided mainly because of the following two reasons:
Changing the method from GET to HEAD does not prevent CEF from making the request to the server. The overhead of opening a connection and handling a request is still there which makes it slower than simply blocking the request.
I'm not sure if images won't be displayed if they are available from browser cache. Currently, I don't know of any methods to test this. Suggestions are welcome.
Edit 1:
Changing URL didn't work in the example I posted in the question because I was passing an empty String as the new URL. If we set the URL to some address that is not an "active" domain name (e.g. https://absolutegarbage-sdjdjfbskdfb.com), the request for that resource fails immediately:
#Override
public boolean onBeforeResourceLoad(CefBrowser cefBrowser, CefFrame cefFrame, CefRequest cefRequest) {
if (cefRequest.getResourceType().equals(CefRequest.ResourceType.RT_IMAGE)) {
cefRequest.setURL("https://yghjbnbk.com");
System.out.println("LOL!");
}
return false;
}
As you can probably guess, this still is not the best solution. Please post an answer or comment if someone has found a better solution.
Edit 2: Finally I have a clean working solution, thanks to user amaitland. We just have to pass a command line switch while setting the CefAppHandler. We can do that by overriding the method onBeforeCommandLineProcessing like this:
CefApp.addAppHandler(new CefAppHandlerAdapter(null) {
#Override
public void onBeforeCommandLineProcessing(String s, CefCommandLine cefCommandLine) {
cefCommandLine.appendSwitch("disable-image-loading");
}
#Override
public void stateHasChanged(CefApp.CefAppState state) {
if (state == CefApp.CefAppState.TERMINATED) System.exit(0);
}
});
Class n.1:
void visualizza(){
System.out.println("Testing")
.....
}
Class n2 (JFrame):
label1.setText(obj1.visualizza());
Netbeans tell me error: "void type not allowed here"
Your void visualizza() method doesn't return any String.
System.out.println("Testing") just prints the String Testing on console.
But for setText() method you need to pass a String as parameter.
Most probably this is what you are trying to achieve:
String visualizza() {
return "Testing";
}
Then-
label1.setText(obj1.visualizza());
PS: Please do your own research at least on the basics before referring to stackoverflow. Good luck!