Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /* 
  * Copyright (C) 2012 FoxLabs
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.foxlabs.validation.converter;
This class provides Converter implementation for the java.lang.Class type.

public final class ClassConverter extends AbstractConverter<Class<?>> {
    
    
ClassConverter default instance.
    public static final ClassConverter DEFAULT = new ClassConverter(null);
    
    
Class loader to be used for class loading.
    private final ClassLoader loader;
    
    
Constructs a new ClassConverter with the specified class loader.

Parameters:
loader Class loader to be used for class loading.
    ClassConverter(ClassLoader loader) {
        if (loader == null) {
            loader = Thread.currentThread().getContextClassLoader();
            if (loader == null) {
                loader = ClassConverter.class.getClassLoader();
                if (loader == null)
                    loader = ClassLoader.getSystemClassLoader();
            }
        }
        this. = loader;
    }
    
    
Returns java.lang.Class type.

Returns:
java.lang.Class type.
    @Override
    @SuppressWarnings("unchecked")
    public Class<Class<?>> getType() {
        return (Class<Class<?>>) Class.class.getClass();
    }
    
    
Returns java.lang.Class object for the specified class name.

Parameters:
value Class name.
context Validation context.
Returns:
java.lang.Class object.
Throws:
MalformedValueException if class with the specified name is not found on classpath.
    @Override
    protected <T> Class<?> doDecode(String valueValidationContext<T> context) {
        try {
            return .loadClass(value);
        } catch (ClassNotFoundException e) {
            throw new MalformedValueException(thiscontextvalue);
        }
    }
    
    
Returns class name for the specified java.lang.Class object.

Parameters:
value java.lang.Class object.
context Validation context.
Returns:
Class name.
    @Override
    protected <T> String doEncode(Class<?> valueValidationContext<T> context) {
        return value.getName();
    }
    
New to GrepCode? Check out our FAQ X