com.openpojo.reflection.java.bytecode.ByteCodeFactory Maven / Gradle / Ivy
/*
* Copyright (c) 2010-2018 Osman Shoukry
*
* 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 com.openpojo.reflection.java.bytecode;
import java.lang.reflect.Modifier;
import com.openpojo.log.Logger;
import com.openpojo.log.LoggerFactory;
import com.openpojo.reflection.java.bytecode.asm.ASMDetector;
import com.openpojo.reflection.java.bytecode.asm.ASMNotLoadedException;
import com.openpojo.reflection.java.bytecode.asm.ASMService;
/**
* This factory is to be used to generate a subclass for a given PojoClass.
*
* @author oshoukry
*/
public class ByteCodeFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(ByteCodeFactory.class);
private static boolean asm_enabled = ASMDetector.getInstance().isASMLoaded();
public static Class extends T> getSubClass(Class clazz) {
if (isNull(clazz) || isAnInterface(clazz) || isAnEnum(clazz) || isPrimitive(clazz) || isAnArray(clazz) || isFinal(clazz)) {
LOGGER.error("Invalid request to generate a subclass for [{0}], argument must be [not null, not an interface, not an" +
" enum, not primitive, not an array or a final class", clazz);
return null;
}
if (!asm_enabled)
throw ASMNotLoadedException.getInstance();
LOGGER.info("Generating subclass for class [{0}]", clazz);
return ASMService.getInstance().createSubclassFor(clazz);
}
private static boolean isNull(Class clazz) {
return clazz == null;
}
private static boolean isAnInterface(Class clazz) {
return Modifier.isInterface(clazz.getModifiers());
}
private static boolean isAnEnum(Class clazz) {
return clazz.isEnum();
}
private static boolean isPrimitive(Class clazz) {
return clazz.isPrimitive();
}
private static boolean isAnArray(Class clazz) {
return clazz.isArray();
}
private static boolean isFinal(Class clazz) {
return Modifier.isFinal(clazz.getModifiers());
}
private ByteCodeFactory() {
throw new UnsupportedOperationException(ByteCodeFactory.class.getName() + " should not be constructed!");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy