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).

package com.karollotkowski.softwarehouse.dev;
import com.karollotkowski.softwarehouse.app.Application;
public interface Developer {
Application code();
}

view raw
Developer.java
hosted with ❤ by GitHub

package com.karollotkowski.softwarehouse.dev;
import com.karollotkowski.softwarehouse.app.Application;
import com.karollotkowski.softwarehouse.app.NumericApplication;
@Deprecated
public class FortranDeveloper implements Developer {
@Override
public Application code() {
return new NumericApplication("Numerical weather prediction");
}
}

Developers code Applications and FortranDevelopers are specialised in NumericApplications.

package com.karollotkowski.softwarehouse.app;
public interface Application {
String run();
}

view raw
Application.java
hosted with ❤ by GitHub

package com.karollotkowski.softwarehouse.app;
public class NumericApplication implements Application {
private final String codename;
public NumericApplication(final String codename) {
this.codename = codename;
}
@Override
public String run() {
return "Hello, I am " + codename;
}
}

To make it happened SoftwareHouseFactory manage process of applications development.

package com.karollotkowski.softwarehouse;
import com.karollotkowski.softwarehouse.app.Application;
import com.karollotkowski.softwarehouse.dev.Developer;
import com.karollotkowski.softwarehouse.dev.FortranDeveloper;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class SoftwareHouseFactory {
private final List<Developer> developers = new ArrayList<>();
protected void hireDeveloper(final Developer newJoiner) {
developers.add(newJoiner);
}
@SuppressWarnings("deprecation")
public Optional<Application> developNumericApp() {
return developers.stream()
.filter(developer > developer instanceof FortranDeveloper)
.findFirst()
.map(this::codeApp);
}
private Application codeApp(final Developer developer) {
return developer.code();
}
}

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:

package com.karollotkowski.softwarehouse;
import com.karollotkowski.softwarehouse.app.Application;
import com.karollotkowski.softwarehouse.dev.Developer;
import org.junit.Before;
import org.junit.Test;
import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
public class SoftwareHouseFactoryTest {
private SoftwareHouseFactory softwareHouse;
@Before
public void setUp() {
softwareHouse = new SoftwareHouseFactory();
}
@SuppressWarnings("deprecation")
@Test
public void willDevelopNumericAppWhenFortranDeveloperIsHired() {
// given
final Developer fortranDeveloper = new com.karollotkowski.softwarehouse.dev.FortranDeveloper();
softwareHouse.hireDeveloper(fortranDeveloper);
// when
final Optional<Application> application = softwareHouse.developNumericApp();
// then
assertThat("We are rich", application.isPresent(), is(true));
}
@Test
public void willNotDevelopNumericAppWhenLastFortranDeveloperDied20YearsAgo() {
// given
// when
final Optional<Application> application = softwareHouse.developNumericApp();
// then
assertThat("We are losers", application.isPresent(), is(false));
}
}

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.