In Java, if you import a deprecated class:
import SomeDeprecatedClass;
You get this warning: The type SomeDeprecatedClass is deprecated
Is there a way to suppress this warning?
To avoid the warning:
do not import the class
instead use the fully qualified class name
and use it in as few locations as possible.
Use this annotation on your class or method:
#SuppressWarnings("deprecation")
Since Java 9, you might need to add:
#SuppressWarnings("removal")
If the class was annotated with something like:
#Deprecated(since = "3.14", forRemoval = true)
As a hack you can not do the import and use the fully qualified name inside the code.
You might also try javac -Xlint:-deprecation not sure if that would address it.
I solved this by changing the import to:
import package.*
then annotating the method that used the deprecated classes with#SuppressWarnings("deprecation")
Suppose that you are overriding/implementing an interface with a deprecated method (such as the getUnicodeStream(String columnLabel) in java.sql.ResultSet) then you will not get rid of deprecation warnings just by using the annotation #SuppressWarnings( "deprecation" ), unless you also annotate the same new method with the #Deprecated annotation. This is logical, because otherwise you could undeprecate a method by just overriding its interface description.
you can use:
javac FileName.java -Xlint:-deprecation
But then this will give you warnings and also tell you the part of the code that is causing deprecation or using deprecated API. Now either you can run your code with these warnings or make appropriate changes in the code.
In my case I was using someListItem.addItem("red color") whereas the compiler wanted me to use someListItem.add("red color");.
If #SuppressWarnings("deprecation") is not working for you like for me. You can find exact squid number in sonar lint plugin. And then you can simply suppress warning: #SuppressWarnings("squid:CallToDeprecatedMethod")
Related
I have a package info class, it used to compile in Java 6/7. But in Java 8, I get compilation errors:
*****error: annotation type not applicable to this kind of declaration
#NamedNativeQueries({
error: annotation type not applicable to this kind of declaration
#SqlResultSetMappings({*****
This is the code:
#NamedNativeQueries({
#NamedNativeQuery(name = "",
query = "",
resultSetMapping = "mapping"
),
NamedNativeQuery(
name = "",
query = "",
callable = true,
readOnly = false,
resultSetMapping = ""
)
})
#SqlResultSetMappings({
})
package abc.domain;
import javax.persistence.ColumnResult;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.SqlResultSetMappings;
Thanks in advance for your help
EDIT: Additional comments have made more clear what's going wrong here; as a consequence, the nature of this answer has changed somewhat.
It sounds like you switched from Hibernate's own version of #NamedNativeQueries and company, which you can stick on packages, to the general javax persistence variant, which cannot be placed on packages.
You must have removed and re-generated the imports in your attempt to convert this code. Don't do that - remove all those imports and replace them with import org.hibernate.annotations.NamedNativeQueries and friends instead.
For posterity, the original answer, still valid but only in context specifically for javax.persistence.NamedNativeQueries.
You must be misremembering; it does not and never has worked on java7 (that's the java7 docs of amedNativeQueries - note how it has a #Target(value=TYPE) marker, so it cannot be put on a package, and that's the v7 edition of the docs!)
You put such things on a type, for example the top level type. Which means the annotations appear near the top, but after the package statement. Given that you're using them in a package-info.java file, they simply cannot appear here (and never could).
SqlResultSetMappings is the same.
There's a small chance that somehow javac7 didn't actually check the Target condition of these annotations. However, that simply means that your code never worked, even if it did compile.
We are using mapstruct 1.3.1FINAL (in combination with lombok v1.18.4 if that matters) and the generated classes aren't compiling because the imports of the static methods used in expression mappings aren't generated. Any clues?
#Mapping(target = "value", expression = "java(ValueUtil.getValue(sourceValue))")
The generated code has compilation errors because the import of ValueUtil is missing :
request.setValue( ValueUtil.getValue(sourceValue) );
Finally i got it, tried out what Sjaak wrote.
#Mapper(imports = { ValueUtil.class })
The import made it. Will try out if Deepaks answer works as well.
Please try with fully qualified class name for ValueUtil I.e. packagename.ValueUtil. This will provide the context to mapstruct to locate the class.
you can add an import statement to the #Mapper annotation, precisely for such cases. Checkout the documentation.
I try to use the #NotNull annotation from package com.sun.istack.internal.
I am using IDE Intellij IDEA Community Edition.
when I build a program using IDE no problem. When I try to compile a file from the command line using javac, I get an error "cannot find symbol".
package ibkr;
import com.sun.istack.internal.NotNull;
public class Main {
public static void main(String[] args) {
test("Test");
}
public static void test(#NotNull String text) {
System.out.println(text);
}
}
I don't understand why i can't compile this code using javac and how Intellij IDEA make compilation and run it.
The annotation is an internal class, it's not for public use. The closest alternative is Jetbrains' stuff:
https://www.jetbrains.com/help/idea/nullable-and-notnull-annotations.html
Even if the class is in the classpath, it doesn't necessarily mean that you can safely refer to that class. The compiler can have some limitations upon accessing some classes/packages.
In most cases, as here, it's obvious whether the package is internal or not: com.sun.istack.internal. Oracle discourages developers from using classes from such packages.
Ok i know this question is a bit old, but if my info is correct, the reason for this is the fact that intellij uses full rt.jar for compilation while javac uses incomplete version, because of ct.sym
This is an annotation used to identify non-nullable values, also this will let static analyzer have their checks in place. In case you are using IntelliJ you can use its annotation but it would make it very tool-specific, same is the case for eclipse
One can also you
https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl
This provides the same non-null annotation but you need to add an extra dependency there.
I tried to suppres warnings using #SuppressWarnings("deprecation") for depreceated APIs. Tried to build using make. Still getting compiler warnings.
Deprecated methods should be replaced as soon as possible. You can check the javadoc to see what you should replace it with.
One way to "suppress" the warnings is to compile your code with an older compiler where the methods you're using aren't deprecated yet.
Another would be to compile your code using -Xlint:deprecation
javac -Xlint:deprecation Application.java
#SuppressWarnings("deprecation") does not work on imports. In that case - if you have the liberty to do that - remove the import and address the class with the complete name in your code. That way, #SuppressWarnings("deprecation") will work on it successfully.
Warnings:
import net.example.fancy.DeprecatedClass;
public class FooBar {
#SuppressWarnings("deprecation")
private DeprecatedClass depr;
}
No more warnings:
public class FooBar {
#SuppressWarnings("deprecation")
private net.example.fancy.DeprecatedClass depr;
}
I have downloaded a third party library and they have classes I need to refer to in the default package? How do I import these classes?
It's not possible directly with the compiler. Sun removed this capability. If something is in the default namespace, everything must be in the default namespace.
However, you can do it using the ClassLoader. Assuming the class is called Thirdparty, and it has a static method call doSomething(), you can execute it like this:
Class clazz = ClassLoader.getSystemClassLoader().loadClass("Thirdparty");
java.lang.reflect.Method method = clazz.getMethod("doSomething");
method.invoke(null);
This is tedious to say the least...
Long ago, sometime before Java 1.5, you used to be able to import Thirdparty; (a class from the unnamed/default namespace), but no longer. See this Java bug report. A bug report asking for a workaround to not being able to use classes from the default namespace suggests to use the JDK 1.3.1 compiler.
To avoid the tedious method.invoke() calls, I adapted the above solution:
Write an interface for the desired functionality in your desired my.package
package my.package;
public interface MyAdaptorInterface{
public void function1();
...
}
Write an adaptor in the default package:
public class MyAdaptor implements my.package.MyAdaptorInterface{
public void function1(){thirdparty.function1();}
...
}
Use ClassLoader/Typecast to access object from my.package
package my.package;
Class clazz = ClassLoader.getSystemClassLoader().loadClass("MyAdaptor");
MyAdaptorInterface myObj = (MyAdaptorInterface)clazz.newInstance();
myObj.function1();