178c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson/**
2eebfcaffb2d52b214abfdb1c0102395c88c9db54Paul Duffin * Copyright 2006-2017 the original author or authors.
378c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson *
478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * Licensed under the Apache License, Version 2.0 (the "License");
578c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * you may not use this file except in compliance with the License.
678c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * You may obtain a copy of the License at
778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson *
878c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson *     http://www.apache.org/licenses/LICENSE-2.0
978c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson *
1078c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * Unless required by applicable law or agreed to in writing, software
1178c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * distributed under the License is distributed on an "AS IS" BASIS,
1278c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1378c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * See the License for the specific language governing permissions and
1478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * limitations under the License.
1578c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson */
1678c496fe0fac4c89993109340aec80d1afa3141fIan Parkinsonpackage org.objenesis.instantiator.basic;
1778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
1878c496fe0fac4c89993109340aec80d1afa3141fIan Parkinsonimport org.objenesis.ObjenesisException;
1978c496fe0fac4c89993109340aec80d1afa3141fIan Parkinsonimport org.objenesis.instantiator.ObjectInstantiator;
20eebfcaffb2d52b214abfdb1c0102395c88c9db54Paul Duffinimport org.objenesis.instantiator.annotations.Instantiator;
21eebfcaffb2d52b214abfdb1c0102395c88c9db54Paul Duffinimport org.objenesis.instantiator.annotations.Typology;
2278c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
2378c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson/**
2478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * The simplest instantiator - simply calls Class.newInstance(). This can deal with default public
2578c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * constructors, but that's about it.
26eebfcaffb2d52b214abfdb1c0102395c88c9db54Paul Duffin *
2778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * @author Joe Walnes
2878c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson * @see ObjectInstantiator
2978c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson */
30eebfcaffb2d52b214abfdb1c0102395c88c9db54Paul Duffin@Instantiator(Typology.NOT_COMPLIANT)
31eebfcaffb2d52b214abfdb1c0102395c88c9db54Paul Duffinpublic class NewInstanceInstantiator<T> implements ObjectInstantiator<T> {
3278c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
33eebfcaffb2d52b214abfdb1c0102395c88c9db54Paul Duffin   private final Class<T> type;
3478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
35eebfcaffb2d52b214abfdb1c0102395c88c9db54Paul Duffin   public NewInstanceInstantiator(Class<T> type) {
3678c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson      this.type = type;
3778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   }
3878c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
39eebfcaffb2d52b214abfdb1c0102395c88c9db54Paul Duffin   public T newInstance() {
4078c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson      try {
4178c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson         return type.newInstance();
42eebfcaffb2d52b214abfdb1c0102395c88c9db54Paul Duffin      }
4378c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson      catch(Exception e) {
4478c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson         throw new ObjenesisException(e);
4578c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson      }
4678c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson   }
4778c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson
4878c496fe0fac4c89993109340aec80d1afa3141fIan Parkinson}
49