@Override protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) { final List<Expression> joinColumns = getJoinColumns(enclosingClass, fieldName, mappedClass); if (joinColumns != null && joinColumns.size() == 1) { return new NodeData((AnnotationExpr) joinColumns.get(0), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } return null; } }
@Override protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) { final List<Expression> joinColumns = getJoinColumns(enclosingClass, fieldName, mappedClass); if (joinColumns != null && joinColumns.size() == 1) { return new NodeData((AnnotationExpr) joinColumns.get(0), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } return null; } }
@Override protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) { final List<Expression> joinColumns = getJoinColumns(enclosingClass, fieldName, mappedClass); if (joinColumns != null && joinColumns.size() > 1) { final Comment fixme = new BlockComment("\nFIXME: JPA_CONVERSION\n" + "For compound primary keys, make sure the join columns are in the correct order.\n"); AnnotationExpr annotation = new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new ArrayInitializerExpr(joinColumns)); annotation.setComment(fixme); return new NodeData(annotation, new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false), Collections.singletonList(new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), "PrimaryKeyJoinColumn"), false, false))); } return null; } }
@Override protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) { final List<Expression> joinColumns = getJoinColumns(enclosingClass, fieldName, mappedClass); if (joinColumns != null && joinColumns.size() > 1) { final Comment fixme = new BlockComment("\nFIXME: JPA_CONVERSION\n" + "For compound primary keys, make sure the join columns are in the correct order.\n"); AnnotationExpr annotation = new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new ArrayInitializerExpr(joinColumns)); annotation.setComment(fixme); return new NodeData(annotation, new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false), Arrays.asList( new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PrimaryKeyJoinColumnResolver.PACKAGE),PrimaryKeyJoinColumnResolver.SIMPLE_NAME), false, false) , new ImportDeclaration(new QualifiedNameExpr(new NameExpr(JoinColumnResolver.PACKAGE),JoinColumnResolver.SIMPLE_NAME), false, false) )); } return null; } }
@Override protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) { final FieldDescriptor fd = OjbUtil.findFieldDescriptor(mappedClass, fieldName, descriptorRepositories); if (fd != null) { final boolean autoInc = fd.isAutoIncrement(); final String seqName = fd.getSequenceName(); if (autoInc && StringUtils.isBlank(seqName)) { LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field has autoincrement set to true but sequenceName is blank."); } if (!autoInc && StringUtils.isNotBlank(seqName)) { LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field has autoincrement set to false but sequenceName is " + seqName + "."); } if (autoInc || StringUtils.isNotBlank(seqName)) { return new NodeData(new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), Collections.singletonList(new MemberValuePair("name", new StringLiteralExpr(upperCaseTableName ? seqName.toUpperCase() : seqName)))), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } } return null; } }
@Override protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) { final FieldDescriptor fd = OjbUtil.findFieldDescriptor(mappedClass, fieldName, descriptorRepositories); if (fd != null) { final boolean autoInc = fd.isAutoIncrement(); final String seqName = fd.getSequenceName(); if (autoInc && StringUtils.isBlank(seqName)) { LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field has autoincrement set to true but sequenceName is blank."); } if (!autoInc && StringUtils.isNotBlank(seqName)) { LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field has autoincrement set to false but sequenceName is " + seqName + "."); } if (autoInc || StringUtils.isNotBlank(seqName)) { return new NodeData(new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), Collections.singletonList(new MemberValuePair("generator", new StringLiteralExpr(upperCaseTableName ? seqName.toUpperCase() : seqName)))), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } } return null; } }
@Override protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) { final boolean pk = isPrimaryKeyColumn(mappedClass, fieldName); if (pk) { return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } return null; }
private NodeAndImports<MethodDeclaration> createPrimaryKeyHashCode(Collection<FieldDescriptor> primaryKeyDescriptors) { final MethodDeclaration hashCode = new MethodDeclaration(ModifierSet.PUBLIC, new PrimitiveType(PrimitiveType.Primitive.Int), "hashCode"); hashCode.setAnnotations(Collections.<AnnotationExpr>singletonList(new MarkerAnnotationExpr(new NameExpr("Override")))); final List<Expression> ctorArgs = new ArrayList<Expression>(); ctorArgs.add(new IntegerLiteralExpr("17")); ctorArgs.add(new IntegerLiteralExpr("37")); Expression hashCodeExpr = new ObjectCreationExpr(null, new ClassOrInterfaceType("HashCodeBuilder"), ctorArgs); for (FieldDescriptor f : primaryKeyDescriptors) { final List<Expression> args = new ArrayList<Expression>(); args.add(new FieldAccessExpr(new ThisExpr(), f.getAttributeName())); hashCodeExpr = new MethodCallExpr(hashCodeExpr, "append", args); } hashCodeExpr = new MethodCallExpr(hashCodeExpr, "toHashCode"); final BlockStmt equalsBody = new BlockStmt(Collections.<Statement>singletonList(new ReturnStmt(hashCodeExpr))); hashCode.setBody(equalsBody); return new NodeAndImports<MethodDeclaration>(hashCode, Collections.singleton(new ImportDeclaration(new QualifiedNameExpr(new NameExpr("org.apache.commons.lang.builder"), "HashCodeBuilder"), false, false))); }
@Override protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) { final FieldDescriptor fd = OjbUtil.findFieldDescriptor(mappedClass, fieldName, descriptorRepositories); if (fd != null) { final Class<?> fc = ResolverUtil.getType(enclosingClass, fieldName); if (isEnum(fc)) { final Comment fixme = new BlockComment("\nFIXME:\n" + "Enums must be annotated with the @Enumerated annotation.\n " + "The @Enumerated annotation should set the EnumType.\n" + "If the EnumType is not set, then the Enumerated annotation is defaulted to EnumType.ORDINAL.\n" + "This conversion program cannot tell whether EnumType.ORDINAL is the appropriate EnumType."); AnnotationExpr enumerated = new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), Collections.singletonList(new MemberValuePair("value", new NameExpr("EnumType.")))); enumerated.setComment(fixme); return new NodeData(enumerated, new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false), Collections.singletonList(new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), "TemporalType"), false, false))); } } return null; }
@Override protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) { final FieldDescriptor fd = OjbUtil.findFieldDescriptor(mappedClass, fieldName, descriptorRepositories); if (fd != null) { if (fd.isLocking()) { return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } } return null; } }
private NodeAndImports<MethodDeclaration> createPrimaryKeyToString(Collection<FieldDescriptor> primaryKeyDescriptors) { final MethodDeclaration toString = new MethodDeclaration(ModifierSet.PUBLIC, new ClassOrInterfaceType("String"), "toString"); toString.setAnnotations(Collections.<AnnotationExpr>singletonList(new MarkerAnnotationExpr(new NameExpr("Override")))); Expression toStringBuilderExpr = new ObjectCreationExpr(null, new ClassOrInterfaceType("ToStringBuilder"), Collections.<Expression>singletonList(new ThisExpr())); for (FieldDescriptor f : primaryKeyDescriptors) { final List<Expression> args = new ArrayList<Expression>(); args.add(new StringLiteralExpr(f.getAttributeName())); args.add(new FieldAccessExpr(new ThisExpr(), f.getAttributeName())); toStringBuilderExpr = new MethodCallExpr(toStringBuilderExpr, "append", args); } toStringBuilderExpr = new MethodCallExpr(toStringBuilderExpr, "toString"); final BlockStmt toStringBody = new BlockStmt(Collections.<Statement>singletonList(new ReturnStmt(toStringBuilderExpr))); toString.setBody(toStringBody); return new NodeAndImports<MethodDeclaration>(toString, Collections.singleton(new ImportDeclaration(new QualifiedNameExpr(new NameExpr("org.apache.commons.lang.builder"), "ToStringBuilder"), false, false))); }
@Override public NodeData resolve(Node node, String mappedClass) { if (!(node instanceof FieldDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on FieldDeclaration"); } final FieldDeclaration field = (FieldDeclaration) node; if (ResolverUtil.canFieldBeAnnotated(field)) { final boolean mappedColumn = OjbUtil.isMappedColumn(mappedClass, ParserUtil.getFieldName(field), descriptorRepositories); if (!mappedColumn) { return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } } return null; } }
@Override public NodeData resolve(Node node, String mappedClass) { if (!(node instanceof ClassOrInterfaceDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); } final TypeDeclaration dclr = (TypeDeclaration) node; if (!(dclr.getParentNode() instanceof CompilationUnit)) { //handling nested classes return null; } final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String enclosingClass = pckg + "." + name; if (!enclosingClass.equals(mappedClass)) { return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } return null; } }
/** gets the annotation but also adds an import in the process if a Convert annotation is required. */ @Override protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) { final CollectionDescriptor cld = OjbUtil.findCollectionDescriptor(mappedClass, fieldName, descriptorRepositories); if (cld != null) { Collection<FieldHelper> orderBy = cld.getOrderBy(); if (orderBy != null && !orderBy.isEmpty()) { String orderByStr = ""; for (FieldHelper fh : orderBy) { orderByStr += fh.name + (fh.isAscending ? "" : " DESC") + ", "; } orderByStr = orderByStr.replaceAll(", $", ""); return new NodeData(new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new StringLiteralExpr(orderByStr)), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } } return null; } }
private NodeAndImports<MethodDeclaration> createPrimaryKeyCompareTo(Collection<FieldDescriptor> primaryKeyDescriptors, String enclosingClassName) { final MethodDeclaration compareTo = new MethodDeclaration(ModifierSet.PUBLIC, new PrimitiveType(PrimitiveType.Primitive.Int), "compareTo", Collections.singletonList(new Parameter(new ClassOrInterfaceType(enclosingClassName), new VariableDeclaratorId("other")))); compareTo.setAnnotations(Collections.<AnnotationExpr>singletonList(new MarkerAnnotationExpr(new NameExpr("Override")))); Expression compareToBuilderExpr = new ObjectCreationExpr(null, new ClassOrInterfaceType("CompareToBuilder"), Collections.<Expression>emptyList()); for (FieldDescriptor f : primaryKeyDescriptors) { final List<Expression> args = new ArrayList<Expression>(); args.add(new FieldAccessExpr(new ThisExpr(), f.getAttributeName())); args.add(new FieldAccessExpr(new NameExpr("other"), f.getAttributeName())); compareToBuilderExpr = new MethodCallExpr(compareToBuilderExpr, "append", args); } compareToBuilderExpr = new MethodCallExpr(compareToBuilderExpr, "toComparison"); final List<Statement> statements = new ArrayList<Statement>(); statements.add(new ReturnStmt(compareToBuilderExpr)); final BlockStmt equalsBody = new BlockStmt(statements); compareTo.setBody(equalsBody); return new NodeAndImports<MethodDeclaration>(compareTo, Collections.singleton(new ImportDeclaration(new QualifiedNameExpr(new NameExpr("org.apache.commons.lang.builder"), "CompareToBuilder"), false, false))); }
@Override public NodeData resolve(Node node, String mappedClass) { if (!(node instanceof ClassOrInterfaceDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); } final TypeDeclaration dclr = (TypeDeclaration) node; if (!(dclr.getParentNode() instanceof CompilationUnit)) { //handling nested classes return null; } final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String enclosingClass = pckg + "." + name; final Collection<String> customizedFieldsOnNode = getFieldsOnNode(dclr, getCustomizedFields(mappedClass)); if (customizedFieldsOnNode == null || customizedFieldsOnNode.isEmpty()) { LOG.info(ResolverUtil.logMsgForClass(enclosingClass, mappedClass) + " has no customized fields"); return null; } return new NodeData(new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new NameExpr("CreateCustomizerFor" + customizedFieldsOnNode.toString())), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); }
@Override public NodeData resolve(Node node, String mappedClass) { if (!(node instanceof ClassOrInterfaceDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); } final TypeDeclaration dclr = (TypeDeclaration) node; if (!(dclr.getParentNode() instanceof CompilationUnit)) { //handling nested classes return null; } final String name = dclr.getName(); final Collection<FieldDescriptor> primaryKeyDescriptors = getPrimaryKeyDescriptors(mappedClass); if (primaryKeyDescriptors != null && primaryKeyDescriptors.size() > 1 && nodeContainsPkFields(dclr, primaryKeyDescriptors)) { final NodeAndImports<ClassOrInterfaceDeclaration> primaryKeyClass = createPrimaryKeyClass(name, primaryKeyDescriptors); final String pkClassName = primaryKeyClass.node.getName(); return new NodeData(new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new NameExpr(name + "." + pkClassName + ".class")), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false), primaryKeyClass.imprts, primaryKeyClass.node); } return null; }
@Override protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) { final FieldDescriptor fd = OjbUtil.findFieldDescriptor(mappedClass, fieldName, descriptorRepositories); if (fd != null) { final Class<?> fc = ResolverUtil.getType(enclosingClass, fieldName); final String columnType = fd.getColumnType(); if (isLob(columnType)) { if (isValidFieldType(fc)) { return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } else { LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " is not a valid field type for the @Lob annotation, must be one of " + VALID_TYPES_STR); } } return null; } return null; }
@Override public Node visit(ImportDeclaration _n, Object _arg) { NameExpr name = cloneNodes(_n.getName(), _arg); Comment comment = cloneNodes(_n.getComment(), _arg); ImportDeclaration r = new ImportDeclaration( _n.getBeginLine(), _n.getBeginColumn(), _n.getEndLine(), _n.getEndColumn(), name, _n.isStatic(), _n.isAsterisk() ); r.setComment(comment); return r; }
@Override public NodeData resolve(Node node, String mappedClass) { if (!(node instanceof ClassOrInterfaceDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); } final TypeDeclaration dclr = (TypeDeclaration) node; if (!(dclr.getParentNode() instanceof CompilationUnit)) { //handling nested classes return null; } final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String enclosingClass = pckg + "." + name; final ClassDescriptor cd = OjbUtil.findClassDescriptor(enclosingClass, descriptorRepositories); if (cd != null) { return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } return null; } }