How can I remove a warning warning in java?

I have this piece of code:

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;

public class ClientLookup<T extends Remote> {
 private T sharedObject;

 public void lookup(String adress) throws MalformedURLException, RemoteException, NotBoundException {
   sharedObject = (T) Naming.lookup(adress);
 }

 public T getSharedObject() {
   return sharedObject;
 }
}

The part with the name "(T) Naming.lookup (address)" gives me a warning: "Security type: uncheck the remote control at T"

I don't want to use "@SuppressWarnings (" unchecked ")", I just want to know why it shows a warning when "T extends Remote" and fixes it (for clean code)

Thnaks.

+3
source share
3 answers

"Unechecked cast" , Java , , , T , T . , Remote.

Class<T> :

public class ClientLookup<T extends Remote> {
  private T sharedObject;
  private Class<T> clazz;

  public ClientLookup(Class<T> clazz) {
    this.clazz = clazz;
  }

  public void lookup(String adress) throws MalformedURLException, RemoteException, NotBoundException {
    sharedObject = clazz.cast(Naming.lookup(adress));
  }

  public T getSharedObject() {
    return sharedObject;
  }
}

T .

+7

Naming.lookup() Remote, .

T, :

private Class<T> clazz;
clazz.cast(Naming.lookup(address)); 

T. - - , : MoreTypes.java.

+2

, , . @SuppressWarnings.

, IDE, , . Eclipse . , UID Swing GUI ( ) . , .

+1

Source: https://habr.com/ru/post/1728726/


All Articles