/*
* Copyright © 2014 Cask Data, Inc.
*
* 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 co.cask.cdap.common.lang;
import co.cask.cdap.internal.lang.FieldVisitor;
import co.cask.cdap.internal.lang.Reflections;
import com.google.common.base.Defaults;
import com.google.common.reflect.TypeToken;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
/**
*
*/
public class InstantiatorTest {
@Test
public void testUnsafe() {
Record record = new InstantiatorFactory(false).get(TypeToken.of(Record.class)).create();
Reflections.visit(record, Record.class, new FieldVisitor() {
@Override
public void visit(Object instance, Type inspectType, Type declareType, Field field) throws Exception {
if (!Modifier.isStatic(field.getModifiers())) {
Assert.assertEquals(Defaults.defaultValue(field.getType()), field.get(instance));
}
}
});
}
public static final class Record {
private static final Logger LOG = LoggerFactory.getLogger(Record.class);
private boolean bool;
private byte b;
private char c;
private short s;
private int i;
private long l;
private float f;
private double d;
private String str;
// Just to avoid having default constructor.
public Record(String s) {
}
}
}