The first rule of using deprecated classes is to do not use deprecated classes. If you have to use such class then double check how to avoid such situation and find newer implementation of deprecated class (most of the time the new alternative solution is provided). But if you really forced to use deprecated class (e.g. 3rd-party library) then during class compilation (compileJava and compileTestJava Gradle tasks) you will get below warning:

Note: <path_to_your_class_where_deprecated_class_is_used>.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

I will show you few solutions how to disable this warning.

Source code

As an example I chosen Software House concept and deprecated class is a FortranDeveloper (simple implementation of Developer).

Developers code Applications and FortranDevelopers are specialised in NumericApplications.

To make it happened SoftwareHouseFactory manage process of applications development.

1. Use annotation @SuppressWarnings(“deprecation”) on method

As you probably saw in the code I imported FortranDeveloper class and used annotation @SuppressWarnings(“deprecation”) on method developNumericApp.

import com.karollotkowski.softwarehouse.dev.FortranDeveloper;
...
@SuppressWarnings("deprecation")
public Optional<Application> developNumericApp() {
2. Use annotation @SuppressWarnings(“deprecation”) on class

As an alternative to the first solution you can use annotation on the class level. It will disable deprecation warning on whole class.

@SuppressWarnings("deprecation")
public class SoftwareHouseFactory {
3. Use full class name instead of import + annotation

Both solutions works prefect if you are using deprecated class in the source code (executed compileJava Gradle task). They doesn’t work in the case when deprecated class FortranDeveloper is used in the tests package. Here is a working example:

Significant is the line where I created FortranDeveloper by using full class name and removed import declaration for this class:

@SuppressWarnings("deprecation")
@Test
...
final Developer fortranDeveloper = new com.karollotkowski.softwarehouse.dev.FortranDeveloper();

As an alternative you can use annotation on the test class level:

@SuppressWarnings("deprecation")
public class SoftwareHouseFactoryTest {
4. Import all classes from the package + annotation

My last proposition is to use * in import declaration and well known annotation on class or method:

import com.karollotkowski.softwarehouse.dev.*;

P.S. My intention wasn’t to offend any Fortran Developer. Fortran was the first think come to my mind when I thought about old/deprecated programming language 😉

This language was used the first time in 1957, so it is hard to think about it as a cutting edge technology.

Advertisements