Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am writing a URL validator. The code is as follows

public ValidationResult validate(final Object data, final Object root, final String path, final ValidationContext validationContext, final EvaluationContext evaluationContext) {

    ValidationResult validationResult = new ValidationResult();

    @SuppressWarnings("unchecked")
    List<URL> URLs = (List<URL>)data;

    UrlValidator urlValidator = new UrlValidator();
    boolean isValid = true;
    for (URL websiteURL : URLs) {

        if (urlValidator.isValid(websiteURL.getPath())) {
            isValid = true;
        } else {
            isValid = false;
            break;
        }
        System.out.println("websiteURL.getPath() " + websiteURL.getPath());         
    }
    System.out.println("websiteURL.getPath() " + isValid);  

    return validationResult;
}

While debugging i found that for (URL websiteURL : URLs) is giving java.lang.ClassCastException: java.net.URI cannot be cast to java.net.URL error. How can i make my logic work?

share|improve this question

The real problem is here:

@SuppressWarnings("unchecked")
List<URL> URLs = (List<URL>)data;

In fact, it would appear that you are calling this code with an object that is a list of URI objects rather than a list of URL objects. At any rate, the class cast is happening in a hidden cast that happens when the object returned by the Iterator.next() object is assigned to websiteURL.

It is not really clear what the correct solution to this is:

  • You could (possibly) change the code that calls this to pass it a list containing URL objects ... as expected by the validator.

  • You could change the validator to work on a list of URI objects ... starting with the declaration of the URLs variable. (YUCK! Didn't anyone explain Java identifier conventions to you??)

      @SuppressWarnings("unchecked")
      List<URI> uris = (List<URI>)data;
    
  • You could change the validator to work with URL and URI objects ... starting by changing the above declaration to:

      List<?> urls = (List) data;
    

    Then you need to use instanceof to discriminate between URI and URL objects before validating them.

For the record, the way to convert a URI to a URL is to call URI.toURL() on it.

share|improve this answer
    
why is it casting it to URI when i am giving URL in the list type ? How can i coerce it to be URL object. – CoderGuy Apr 7 '15 at 9:25
    
1) It is not being cast to URI. It is being cast to URL. And the cast is happening >>because<< you declared the list type as List<URL>. 2) See the last sentence of my Answer. However, you must get of the unchecked cast of the data to List<URL> because that is problematic. – Stephen C Apr 7 '15 at 10:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.