ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD
时间: 2023-11-01 08:07:32 浏览: 65
ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD是一个findbugs的警告,它表示在实例方法中更新了静态属性。这种情况常见于常量类,直接通过类名.常量名获取的方式违背了封装的原则,因此findbugs不建议使用。但是如果将常量改成静态成员变量,又因为Spring不支持静态注入导致不能实现。解决方法是使用非静态的setter方法调用静态的setter方法给静态成员变量赋值。例如,将常量a改为静态成员变量,通过F.getA()获取,且由于Spring不支持静态注入,可以改为:
class F{
private static String a;
public static String getA() {
return a;
}
public void setA(String a) {
setAValue(a);
}
public static void setAValue(String a) {
F.a = a;
}
}
相关问题
java运行字符串的代码_Java实现字符串转换成可执行代码的方法
Java中可以使用动态编译的方式将字符串转换成可执行代码。下面是一个简单的示例:
```java
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
public class StringToCode {
public static void main(String[] args) throws Exception {
String code = "public class HelloWorld { public static void main(String[] args) { System.out.println(\"Hello World!\"); } }";
String className = "HelloWorld";
// 将字符串写入到.java文件中
File sourceFile = new File(className + ".java");
try (PrintWriter writer = new PrintWriter(new FileWriter(sourceFile))) {
writer.write(code);
}
// 使用JavaCompiler将.java文件编译成.class文件
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, sourceFile.getAbsolutePath());
// 使用URLClassLoader加载.class文件,并执行其中的main方法
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI().toURL() });
Class<?> cls = Class.forName(className, true, classLoader);
Method mainMethod = cls.getMethod("main", String[].class);
mainMethod.invoke(null, new Object[] { null });
}
}
```
这个示例中,将字符串代码写入到一个名为"HelloWorld.java"的文件中,然后使用JavaCompiler进行编译,最后使用URLClassLoader加载并执行其中的main方法。
Implement the constructor and the read() method in the ChangeToUpperCaseInputStream class. The constructor should call the constructor of the superclass and the read() method should read the data using the superclass's read() method and convert it to upper case before returning it. Right-click on the project name again and select "New -> Java Class". Name the class "ChangeToUpperCaseOutputStream" and make sure to select "extends FilterOutputStream" as the superclass. Implement the constructor and the write() method in the ChangeToUpperCaseOutputStream class. The constructor should call the constructor of the superclass and the write() method should convert the data to upper case before calling the superclass's write() method to write it to the output stream. Once you have implemented these classes, you can use them in your main program by creating an instance of the ChangeToUpperCaseInputStream or ChangeToUpperCaseOutputStream and passing it to your FileInputStream or FileOutputStream, respectively. This will allow you to read or write data in upper case.
Sure, here is an example implementation of the ChangeToUpperCaseInputStream and ChangeToUpperCaseOutputStream classes:
```java
import java.io.*;
public class ChangeToUpperCaseInputStream extends FilterInputStream {
public ChangeToUpperCaseInputStream(InputStream in) {
super(in);
}
@Override
public int read() throws IOException {
int c = super.read();
return (c == -1 ? c : Character.toUpperCase((char) c));
}
}
public class ChangeToUpperCaseOutputStream extends FilterOutputStream {
public ChangeToUpperCaseOutputStream(OutputStream out) {
super(out);
}
@Override
public void write(int b) throws IOException {
super.write(Character.toUpperCase((char) b));
}
}
```
In the above code, the ChangeToUpperCaseInputStream class extends FilterInputStream and overrides the read() method to read the data using the superclass's read() method and convert it to upper case before returning it. Similarly, the ChangeToUpperCaseOutputStream class extends FilterOutputStream and overrides the write() method to convert the data to upper case before calling the superclass's write() method to write it to the output stream.
To use these classes in your main program, you can create an instance of ChangeToUpperCaseInputStream or ChangeToUpperCaseOutputStream and pass it to your FileInputStream or FileOutputStream, respectively. Here is an example:
```java
import java.io.*;
public class MyProgram {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("input.txt");
ChangeToUpperCaseInputStream upperCaseIn = new ChangeToUpperCaseInputStream(in);
FileOutputStream out = new FileOutputStream("output.txt");
ChangeToUpperCaseOutputStream upperCaseOut = new ChangeToUpperCaseOutputStream(out);
int c;
while ((c = upperCaseIn.read()) != -1) {
upperCaseOut.write(c);
}
upperCaseIn.close();
upperCaseOut.close();
}
}
```
In the above code, we first create a FileInputStream and wrap it in a ChangeToUpperCaseInputStream. We also create a FileOutputStream and wrap it in a ChangeToUpperCaseOutputStream. We then read data from the input stream using the read() method of the ChangeToUpperCaseInputStream and write data to the output stream using the write() method of the ChangeToUpperCaseOutputStream. Finally, we close both streams to release any resources they are holding.