public TypeDeclaration processTypeDeclaration( PackageRegistry pkgRegistry, AbstractClassTypeDeclarationDescr typeDescr) { TypeDeclaration type = kbuilder.getTypeBuilder().getExistingTypeDeclaration(typeDescr.getFullTypeName()); if (type == null) { type = new TypeDeclaration(typeDescr.getTypeName()); type.setResource(typeDescr.getResource()); // if is not new, search the already existing declaration and // compare them o see if they are at least compatibles // check whether it is necessary to build the class or not type.setNovel(TypeDeclarationUtils.isNovelClass(typeDescr, pkgRegistry)); type.setNature( type.isNovel() ? TypeDeclaration.Nature.DEFINITION : TypeDeclaration.Nature.DECLARATION); } processTypeAnnotations(typeDescr, type); return type; }
protected void normalizeForeignPackages(PackageDescr packageDescr) { Map<String, PackageDescr> foreignPackages = null; for (AbstractClassTypeDeclarationDescr typeDescr : packageDescr.getClassAndEnumDeclarationDescrs()) { if (kbuilder.filterAccepts( ResourceChange.Type.DECLARATION, typeDescr.getNamespace(), typeDescr.getTypeName())) { if (!typeDescr.getNamespace().equals(packageDescr.getNamespace())) { // If the type declaration is for a different namespace, process that separately. PackageDescr altDescr; if (foreignPackages == null) { foreignPackages = new HashMap<String, PackageDescr>(); } if (foreignPackages.containsKey(typeDescr.getNamespace())) { altDescr = foreignPackages.get(typeDescr.getNamespace()); } else { altDescr = new PackageDescr(typeDescr.getNamespace()); altDescr.setResource(packageDescr.getResource()); foreignPackages.put(typeDescr.getNamespace(), altDescr); } if (typeDescr instanceof TypeDeclarationDescr) { altDescr.addTypeDeclaration((TypeDeclarationDescr) typeDescr); } else if (typeDescr instanceof EnumDeclarationDescr) { altDescr.addEnumDeclaration((EnumDeclarationDescr) typeDescr); } for (ImportDescr imp : packageDescr.getImports()) { altDescr.addImport(imp); } if (!kbuilder.getPackageRegistry().containsKey(altDescr.getNamespace())) { kbuilder.createPackageRegistry(altDescr); } } } } }
protected void updateTraitInformation( AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, ClassDefinition def, PackageRegistry pkgRegistry) { if (typeDescr.hasAnnotation(Traitable.class) || (!type.getKind().equals(TypeDeclaration.Kind.TRAIT) && kbuilder.getPackageRegistry().containsKey(def.getSuperClass()) && kbuilder .getPackageRegistry(def.getSuperClass()) .getTraitRegistry() .getTraitables() .containsKey(def.getSuperClass()))) { // traitable if (type.isNovel()) { try { PackageRegistry reg = kbuilder.getPackageRegistry(typeDescr.getNamespace()); String availableName = typeDescr.getType().getFullName(); Class<?> resolvedType = reg.getTypeResolver().resolveType(availableName); updateTraitDefinition(type, resolvedType, false); } catch (ClassNotFoundException cnfe) { // we already know the class exists } } pkgRegistry.getTraitRegistry().addTraitable(def); } else if (type.getKind().equals(TypeDeclaration.Kind.TRAIT) || typeDescr.hasAnnotation(Trait.class)) { // trait if (!type.isNovel()) { try { PackageRegistry reg = kbuilder.getPackageRegistry(typeDescr.getNamespace()); String availableName = typeDescr.getType().getFullName(); Class<?> resolvedType = reg.getTypeResolver().resolveType(availableName); if (!Thing.class.isAssignableFrom(resolvedType)) { if (!resolvedType.isInterface()) { kbuilder.addBuilderResult( new TypeDeclarationError( typeDescr, "Unable to redeclare concrete class " + resolvedType.getName() + " as a trait.")); return; } updateTraitDefinition(type, resolvedType, false); String target = typeDescr.getTypeName() + TraitFactory.SUFFIX; TypeDeclarationDescr tempDescr = new TypeDeclarationDescr(); tempDescr.setNamespace(typeDescr.getNamespace()); tempDescr.setFields(typeDescr.getFields()); tempDescr.setType(target, typeDescr.getNamespace()); tempDescr.setTrait(true); tempDescr.addSuperType(typeDescr.getType()); tempDescr.setResource(type.getResource()); TypeDeclaration tempDeclr = new TypeDeclaration(target); tempDeclr.setKind(TypeDeclaration.Kind.TRAIT); tempDeclr.setTypesafe(type.isTypesafe()); tempDeclr.setNovel(true); tempDeclr.setTypeClassName(tempDescr.getType().getFullName()); tempDeclr.setResource(type.getResource()); ClassDefinition tempDef = new ClassDefinition(target); tempDef.setClassName(tempDescr.getType().getFullName()); tempDef.setTraitable(false); for (FieldDefinition fld : def.getFieldsDefinitions()) { tempDef.addField(fld); } tempDef.setInterfaces(def.getInterfaces()); tempDef.setSuperClass(def.getClassName()); tempDef.setDefinedClass(resolvedType); tempDef.setAbstrakt(true); tempDeclr.setTypeClassDef(tempDef); declaredClassBuilder.generateBeanFromDefinition( tempDescr, tempDeclr, pkgRegistry, tempDef); try { Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(tempDescr.getType().getFullName()); tempDeclr.setTypeClass(clazz); pkgRegistry .getTraitRegistry() .addTrait(tempDef.getClassName().replace(TraitFactory.SUFFIX, ""), tempDef); } catch (ClassNotFoundException cnfe) { kbuilder.addBuilderResult( new TypeDeclarationError( typeDescr, "Internal Trait extension Class '" + target + "' could not be generated correctly'")); } finally { pkgRegistry.getPackage().addTypeDeclaration(tempDeclr); } } else { updateTraitDefinition(type, resolvedType, true); pkgRegistry.getTraitRegistry().addTrait(def); } } catch (ClassNotFoundException cnfe) { // we already know the class exists } } else { if (def.getClassName().endsWith(TraitFactory.SUFFIX)) { pkgRegistry .getTraitRegistry() .addTrait(def.getClassName().replace(TraitFactory.SUFFIX, ""), def); } else { pkgRegistry.getTraitRegistry().addTrait(def); } } } }
protected void checkRedeclaration( AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, PackageRegistry pkgRegistry) { TypeDeclaration previousTypeDeclaration = kbuilder .getPackageRegistry(typeDescr.getNamespace()) .getPackage() .getTypeDeclaration(typeDescr.getTypeName()); try { // if there is no previous declaration, then the original declaration was a POJO // to the behavior previous these changes if (previousTypeDeclaration == null) { // new declarations of a POJO can't declare new fields, // except if the POJO was previously generated/compiled and saved into the kjar Class<?> existingDeclarationClass = TypeDeclarationUtils.getExistingDeclarationClass(typeDescr, pkgRegistry); if (!kbuilder.getBuilderConfiguration().isPreCompiled() && !GeneratedFact.class.isAssignableFrom(existingDeclarationClass) && !type.getTypeClassDef().getFields().isEmpty()) { try { Class existingClass = pkgRegistry .getPackage() .getTypeResolver() .resolveType(typeDescr.getType().getFullName()); ClassFieldInspector cfi = new ClassFieldInspector(existingClass); int fieldCount = 0; for (String existingFieldName : cfi.getFieldTypesField().keySet()) { if (!cfi.isNonGetter(existingFieldName) && !"class".equals(existingFieldName) && cfi.getSetterMethods().containsKey(existingFieldName) && cfi.getGetterMethods().containsKey(existingFieldName)) { if (!typeDescr.getFields().containsKey(existingFieldName)) { type.setValid(false); kbuilder.addBuilderResult( new TypeDeclarationError( typeDescr, "New declaration of " + typeDescr.getType().getFullName() + " does not include field " + existingFieldName)); } else { String fldType = cfi.getFieldTypes().get(existingFieldName).getName(); fldType = TypeDeclarationUtils.toBuildableType(fldType, kbuilder.getRootClassLoader()); TypeFieldDescr declaredField = typeDescr.getFields().get(existingFieldName); if (!fldType.equals( type.getTypeClassDef().getField(existingFieldName).getTypeName())) { type.setValid(false); kbuilder.addBuilderResult( new TypeDeclarationError( typeDescr, "New declaration of " + typeDescr.getType().getFullName() + " redeclared field " + existingFieldName + " : \n" + "existing : " + fldType + " vs declared : " + declaredField.getPattern().getObjectType())); } else { fieldCount++; } } } } if (fieldCount != typeDescr.getFields().size()) { kbuilder.addBuilderResult(reportDeclarationDiff(cfi, typeDescr)); } } catch (IOException e) { e.printStackTrace(); type.setValid(false); kbuilder.addBuilderResult( new TypeDeclarationError( typeDescr, "Unable to redeclare " + typeDescr.getType().getFullName() + " : " + e.getMessage())); } catch (ClassNotFoundException e) { type.setValid(false); kbuilder.addBuilderResult( new TypeDeclarationError( typeDescr, "Unable to redeclare " + typeDescr.getType().getFullName() + " : " + e.getMessage())); } } } else { int typeComparisonResult = this.compareTypeDeclarations(previousTypeDeclaration, type); if (typeComparisonResult < 0) { // oldDeclaration is "less" than newDeclaration -> error kbuilder.addBuilderResult( new TypeDeclarationError( typeDescr, typeDescr.getType().getFullName() + " declares more fields than the already existing version")); type.setValid(false); } else if (typeComparisonResult > 0 && !type.getTypeClassDef().getFields().isEmpty()) { // oldDeclaration is "grater" than newDeclaration -> error kbuilder.addBuilderResult( new TypeDeclarationError( typeDescr, typeDescr.getType().getFullName() + " declares less fields than the already existing version")); type.setValid(false); } // if they are "equal" -> no problem // in the case of a declaration, we need to copy all the // fields present in the previous declaration if (type.getNature() == TypeDeclaration.Nature.DECLARATION) { mergeTypeDeclarations(previousTypeDeclaration, type); } } } catch (IncompatibleClassChangeError error) { // if the types are incompatible -> error kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, error.getMessage())); } }