use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class TypeDeclarationDescrVisitor method visit.
public void visit(List<TypeDeclarationDescr> typeDeclarationDescrs) {
for (TypeDeclarationDescr typeDeclaration : typeDeclarationDescrs) {
Import objectImport = data.getImportByName(typeDeclaration.getTypeName());
String objectTypeName;
if (objectImport == null) {
objectTypeName = typeDeclaration.getTypeName();
} else {
objectTypeName = objectImport.getName();
}
ObjectType objectType = this.data.getObjectTypeByFullName(objectTypeName);
if (objectType == null) {
objectType = new ObjectType(typeDeclaration);
objectType.setName(typeDeclaration.getTypeName());
objectType.setFullName(typeDeclaration.getTypeName());
data.add(objectType);
}
for (String fieldName : typeDeclaration.getFields().keySet()) {
Field field = data.getFieldByObjectTypeAndFieldName(objectType.getFullName(), fieldName);
if (field == null) {
field = ObjectTypeFactory.createField(typeDeclaration.getFields().get(fieldName), fieldName, objectType);
field.setFieldType(typeDeclaration.getFields().get(fieldName).getPattern().getObjectType());
data.add(field);
}
}
for (AnnotationDescr annDescr : typeDeclaration.getAnnotations()) {
Map<String, Object> values = typeDeclaration.getAnnotation(annDescr.getName()).getValueMap();
for (String value : values.keySet()) {
objectType.getMetadata().put(annDescr.getName(), value);
}
}
}
}
use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class TypeDeclarationNameResolver method qualifyNames.
private void qualifyNames(List<TypeDefinition> unresolvedTypes, PackageDescr packageDescr, TypeResolver typeResolver) {
for (TypeDeclarationDescr declarationDescr : packageDescr.getTypeDeclarations()) {
qualifyNames(declarationDescr, packageDescr, unresolvedTypes, typeResolver);
discoverHierarchyForRedeclarations(declarationDescr, packageDescr, typeResolver);
}
for (EnumDeclarationDescr enumDeclarationDescr : packageDescr.getEnumDeclarations()) {
qualifyNames(enumDeclarationDescr, packageDescr, unresolvedTypes, typeResolver);
}
}
use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class ChangeSetBuilder method build.
public static KieJarChangeSet build(InternalKieModule original, InternalKieModule currentJar) {
KieJarChangeSet result = new KieJarChangeSet();
Collection<String> originalFiles = original.getFileNames();
Collection<String> currentFiles = currentJar.getFileNames();
ArrayList<String> removedFiles = new ArrayList<>(originalFiles);
removedFiles.removeAll(currentFiles);
if (!removedFiles.isEmpty()) {
for (String file : removedFiles) {
// there should be a way to get the JAR name/url to produce a proper URL for the file in it
result.removeFile(file);
}
}
List<TypeDeclarationDescr> typeDeclarations = new ArrayList<>();
Map<String, String> changedClasses = new HashMap<>();
for (String file : currentFiles) {
if (originalFiles.contains(file)) {
// check for modification
byte[] ob = original.getBytes(file);
byte[] cb = currentJar.getBytes(file);
if (!Arrays.equals(ob, cb)) {
if (file.endsWith(".class")) {
changedClasses.put(convertResourceToClassName(file), file);
} else if (!ResourceType.DRL.matchesExtension(file) || !StringUtils.codeAwareEqualsIgnoreSpaces(new String(ob), new String(cb))) {
// check that: (NOT drl file) OR (NOT equalsIgnoringSpaces)
// parse the file to figure out the difference
result.registerChanges(file, diffResource(file, ob, cb, typeDeclarations));
}
}
} else {
// file was added
result.addFile(file);
}
}
for (TypeDeclarationDescr typeDeclaration : typeDeclarations) {
String fqn = typeDeclaration.getFullTypeName();
if (changedClasses.containsKey(fqn)) {
continue;
}
InternalKnowledgePackage pkg = original.getPackage(typeDeclaration.getNamespace());
if (pkg == null) {
continue;
}
TypeResolver resolver = pkg.getTypeResolver();
for (TypeFieldDescr field : typeDeclaration.getFields().values()) {
String fieldType;
try {
fieldType = resolver.resolveType(field.getPattern().getObjectType()).getCanonicalName();
} catch (ClassNotFoundException e) {
continue;
}
if (changedClasses.containsKey(fieldType)) {
changedClasses.put(fqn, convertClassToResourcePath(fqn));
break;
}
}
}
for (String changedClass : changedClasses.values()) {
result.registerChanges(changedClass, new ResourceChangeSet(changedClass, ChangeType.UPDATED));
}
if (original.getKieModuleModel() != null) {
for (String kieBaseName : original.getKieModuleModel().getKieBaseModels().keySet()) {
KnowledgeBuilder originalKbuilder = original.getKnowledgeBuilderForKieBase(kieBaseName);
if (originalKbuilder != null && currentJar.getKnowledgeBuilderForKieBase(kieBaseName) == null) {
currentJar.cacheKnowledgeBuilderForKieBase(kieBaseName, originalKbuilder);
}
}
}
return result;
}
use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class ClassDefinitionFactory method createClassDefinition.
protected ClassDefinition createClassDefinition(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
// extracts type, supertype and interfaces
String fullName = typeDescr.getType().getFullName();
if (type.getKind().equals(TypeDeclaration.Kind.CLASS)) {
TypeDeclarationDescr tdescr = (TypeDeclarationDescr) typeDescr;
if (tdescr.getSuperTypes().size() > 1) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Declared class " + fullName + " - has more than one supertype;"));
return null;
} else if (tdescr.getSuperTypes().isEmpty()) {
tdescr.addSuperType("java.lang.Object");
}
}
Traitable traitableAnn = getTypedAnnotation(typeDescr, Traitable.class);
boolean traitable = traitableAnn != null;
String[] fullSuperTypes = new String[typeDescr.getSuperTypes().size() + 1];
int j = 0;
for (QualifiedName qname : typeDescr.getSuperTypes()) {
fullSuperTypes[j++] = qname.getFullName();
}
fullSuperTypes[j] = Thing.class.getName();
List<String> interfaceList = new ArrayList<>();
interfaceList.add(traitable ? Externalizable.class.getName() : Serializable.class.getName());
if (traitable) {
interfaceList.add(TraitableBean.class.getName());
}
String[] interfaces = interfaceList.toArray(new String[interfaceList.size()]);
// prepares a class definition
ClassDefinition def;
switch(type.getKind()) {
case TRAIT:
def = new ClassDefinition(fullName, Object.class.getName(), fullSuperTypes);
break;
case ENUM:
def = new EnumClassDefinition(fullName, fullSuperTypes[0], null);
break;
case CLASS:
default:
def = new ClassDefinition(fullName, fullSuperTypes[0], interfaces);
def.setTraitable(traitable, traitableAnn != null && traitableAnn.logical());
}
return def;
}
use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class DeclaredClassBuilder method ensureJavaTypeConsistency.
private boolean ensureJavaTypeConsistency(AbstractClassTypeDeclarationDescr typeDescr, ClassDefinition def, TypeResolver typeResolver) {
try {
if (typeDescr instanceof TypeDeclarationDescr && !((TypeDeclarationDescr) typeDescr).isTrait() && typeResolver.resolveType(def.getSuperClass()).isInterface()) {
def.addInterface(def.getSuperClass());
def.setSuperClass(null);
}
for (String sup : def.getInterfaces()) {
if (!typeResolver.resolveType(sup).isInterface()) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Non-interface type used as super interface : " + sup));
return false;
}
}
} catch (ClassNotFoundException cnfe) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to resolve parent type :" + cnfe.getMessage()));
return false;
}
return true;
}
Aggregations