use of org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType in project jbosstools-hibernate by jbosstools.
the class ProcessEntityInfo method visit.
@SuppressWarnings("unchecked")
public boolean visit(MethodDeclaration node) {
if (entityInfo == null) {
return false;
}
if (annotationStyle != AnnotStyle.GETTERS) {
return true;
}
if (node.getName().getFullyQualifiedName().compareTo(entityInfo.getName()) == 0 || node.isConstructor()) {
// this is constructor declaration
return true;
}
// -) is it setter?
if (// $NON-NLS-1$
node.getName().getIdentifier().startsWith("set") && node.parameters().size() == 1) {
// setter - do not process it
return true;
}
// +) is it getter?
if (!(// $NON-NLS-1$
node.getName().getIdentifier().startsWith("get") || // $NON-NLS-1$
node.getName().getIdentifier().startsWith("is")) || node.parameters().size() > 0) {
// not the getter - do not process it
return true;
}
Type type = node.getReturnType2();
if (type == null) {
return true;
}
String returnIdentifier = CollectEntityInfo.getReturnIdentifier(node);
if (type.isSimpleType() || type.isPrimitiveType()) {
if (entityInfo.isAddGeneratedValueFlag()) {
String primaryIdName = entityInfo.getPrimaryIdName();
boolean addGeneratedValueMarker = false;
if (primaryIdName.equals(returnIdentifier)) {
addGeneratedValueMarker = true;
}
if (addGeneratedValueMarker) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_GENERATED_VALUE));
ListRewrite lrw = rewriter.getListRewrite(node, MethodDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
}
}
if (entityInfo.isAddPrimaryIdFlag()) {
String primaryIdName = entityInfo.getPrimaryIdName();
boolean addIdMarker = false;
if (primaryIdName.equals(returnIdentifier)) {
addIdMarker = true;
}
if (addIdMarker) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_ID));
ListRewrite lrw = rewriter.getListRewrite(node, MethodDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
}
}
if (enableOptLock && entityInfo.isAddVersionFlag() && !entityInfo.hasVersionAnnotation()) {
boolean addVersionMarker = false;
if ("version".equals(returnIdentifier)) {
// $NON-NLS-1$
addVersionMarker = true;
}
if (addVersionMarker) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_VERSION));
ListRewrite lrw = rewriter.getListRewrite(node, MethodDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
}
}
}
if (type.isSimpleType() && (AllEntitiesProcessor.columnLength != defaultStrLength)) {
SimpleType simpleType = (SimpleType) type;
String typeName = simpleType.getName().getFullyQualifiedName();
if ("java.lang.String".equals(typeName) || "String".equals(typeName)) {
// $NON-NLS-1$ //$NON-NLS-2$
String fieldId = returnIdentifier;
RefColumnInfo rci = entityInfo.getRefColumnInfo(fieldId);
if (rci == null || !rci.isExist()) {
// if there is no @Column annotation - create new @Column annotation
// with user defined default value length
NormalAnnotation natd = rewriter.getAST().newNormalAnnotation();
natd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_COLUMN));
ListRewrite lrw = rewriter.getListRewrite(node, MethodDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(natd, null);
MemberValuePair mvp = rewriter.getAST().newMemberValuePair();
// $NON-NLS-1$
mvp.setName(rewriter.getAST().newSimpleName("length"));
NumberLiteral nl = rewriter.getAST().newNumberLiteral(String.valueOf(defaultStrLength));
mvp.setValue(nl);
natd.values().add(mvp);
}
}
}
if (type.isSimpleType() || type.isParameterizedType() || type.isArrayType()) {
// $NON-NLS-1$
String fieldId = "";
RefType refType = RefType.UNDEF;
boolean annotated = false;
// $NON-NLS-1$
String fullyQualifiedName2 = "";
fieldId = returnIdentifier;
refType = entityInfo.getFieldIdRelValue(fieldId);
annotated = entityInfo.getFieldIdAnnotatedValue(fieldId);
fullyQualifiedName2 = entityInfo.getFieldIdFQNameValue(fieldId);
Set<RefFieldInfo> setRFI = entityInfo.getRefFieldInfoSet(fullyQualifiedName2);
if (!annotated && setRFI != null && isSimilarType(type, fullyQualifiedName2)) {
RefEntityInfo rei = entityInfo.getFieldIdRefEntityInfo(fieldId);
// either side may be the owning side
if (setRFI.size() > 1 && refType != RefType.MANY2ONE) {
if (rei.mappedBy == null || rei.mappedBy == "") {
// $NON-NLS-1$
addSimpleMarkerAnnotation(node, JPAConst.getRefType(refType));
} else {
// give to the user information about selected mapping
addComplexNormalAnnotation(node, JPAConst.getRefType(refType), rei);
}
} else if (refType == RefType.MANY2ONE || refType == RefType.ENUMERATED || rei.mappedBy == null || rei.mappedBy == "") {
// $NON-NLS-1$
addSimpleMarkerAnnotation(node, JPAConst.getRefType(refType));
} else {
// in case of bidirectional OneToOne - mark both sides with mappedBy -
// user should select the right decision
addComplexNormalAnnotation(node, JPAConst.getRefType(refType), rei);
}
}
}
return true;
}
use of org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType in project jbosstools-hibernate by jbosstools.
the class ProcessEntityInfo method visit.
@SuppressWarnings("unchecked")
public boolean visit(FieldDeclaration node) {
if (entityInfo == null) {
return false;
}
if (annotationStyle != AnnotStyle.FIELDS) {
return true;
}
Type type = node.getType();
if (type == null) {
return true;
}
if (type.isSimpleType() || type.isPrimitiveType()) {
if (entityInfo.isAddGeneratedValueFlag()) {
String primaryIdName = entityInfo.getPrimaryIdName();
Iterator<VariableDeclarationFragment> itVarNames = node.fragments().iterator();
boolean addGeneratedValueMarker = false;
while (itVarNames.hasNext()) {
VariableDeclarationFragment var = itVarNames.next();
String name = var.getName().getIdentifier();
if (primaryIdName.equals(name)) {
addGeneratedValueMarker = true;
break;
}
}
if (addGeneratedValueMarker) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_GENERATED_VALUE));
ListRewrite lrw = rewriter.getListRewrite(node, FieldDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
}
}
if (entityInfo.isAddPrimaryIdFlag()) {
String primaryIdName = entityInfo.getPrimaryIdName();
Iterator<VariableDeclarationFragment> itVarNames = node.fragments().iterator();
boolean addIdMarker = false;
while (itVarNames.hasNext()) {
VariableDeclarationFragment var = itVarNames.next();
String name = var.getName().getIdentifier();
if (primaryIdName.equals(name)) {
addIdMarker = true;
break;
}
}
if (addIdMarker) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_ID));
ListRewrite lrw = rewriter.getListRewrite(node, FieldDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
}
}
if (enableOptLock && entityInfo.isAddVersionFlag() && !entityInfo.hasVersionAnnotation()) {
Iterator<VariableDeclarationFragment> itVarNames = node.fragments().iterator();
boolean addVersionMarker = false;
while (itVarNames.hasNext()) {
VariableDeclarationFragment var = itVarNames.next();
String name = var.getName().getIdentifier();
if ("version".equals(name)) {
// $NON-NLS-1$
addVersionMarker = true;
break;
}
}
if (addVersionMarker) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_VERSION));
ListRewrite lrw = rewriter.getListRewrite(node, FieldDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
}
}
}
if (type.isSimpleType() && (AllEntitiesProcessor.columnLength != defaultStrLength)) {
SimpleType simpleType = (SimpleType) type;
String typeName = simpleType.getName().getFullyQualifiedName();
if ("java.lang.String".equals(typeName) || "String".equals(typeName)) {
// $NON-NLS-1$ //$NON-NLS-2$
String fieldId = null;
Iterator<VariableDeclarationFragment> itVarNames = node.fragments().iterator();
while (itVarNames.hasNext()) {
VariableDeclarationFragment var = itVarNames.next();
fieldId = var.getName().getIdentifier();
if (fieldId != null) {
break;
}
}
RefColumnInfo rci = entityInfo.getRefColumnInfo(fieldId);
if (rci == null || !rci.isExist()) {
// if there is no @Column annotation - create new @Column annotation
// with user defined default value length
NormalAnnotation natd = rewriter.getAST().newNormalAnnotation();
natd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_COLUMN));
ListRewrite lrw = rewriter.getListRewrite(node, FieldDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(natd, null);
MemberValuePair mvp = rewriter.getAST().newMemberValuePair();
// $NON-NLS-1$
mvp.setName(rewriter.getAST().newSimpleName("length"));
NumberLiteral nl = rewriter.getAST().newNumberLiteral(String.valueOf(defaultStrLength));
mvp.setValue(nl);
natd.values().add(mvp);
}
}
}
if (type.isSimpleType() || type.isParameterizedType() || type.isArrayType()) {
Iterator<VariableDeclarationFragment> itVarNames = node.fragments().iterator();
// $NON-NLS-1$
String fieldId = "";
RefType refType = RefType.UNDEF;
boolean annotated = false;
// $NON-NLS-1$
String fullyQualifiedName2 = "";
while (itVarNames.hasNext()) {
VariableDeclarationFragment var = itVarNames.next();
String name = var.getName().getIdentifier();
fieldId = name;
refType = entityInfo.getFieldIdRelValue(fieldId);
annotated = entityInfo.getFieldIdAnnotatedValue(fieldId);
fullyQualifiedName2 = entityInfo.getFieldIdFQNameValue(fieldId);
if (refType != RefType.UNDEF) {
break;
}
}
Set<RefFieldInfo> setRFI = entityInfo.getRefFieldInfoSet(fullyQualifiedName2);
if (!annotated && setRFI != null && isSimilarType(type, fullyQualifiedName2)) {
RefEntityInfo rei = entityInfo.getFieldIdRefEntityInfo(fieldId);
// either side may be the owning side
if (setRFI.size() > 1 && refType != RefType.MANY2ONE) {
if (rei.mappedBy == null || rei.mappedBy == "") {
// $NON-NLS-1$
addSimpleMarkerAnnotation(node, JPAConst.getRefType(refType));
} else {
// give to the user information about selected mapping
addComplexNormalAnnotation(node, JPAConst.getRefType(refType), rei);
}
} else if (refType == RefType.MANY2ONE || refType == RefType.ENUMERATED || rei.mappedBy == null || rei.mappedBy == "") {
// $NON-NLS-1$
addSimpleMarkerAnnotation(node, JPAConst.getRefType(refType));
} else {
// in case of bidirectional OneToOne - mark both sides with mappedBy -
// user should select the right decision
addComplexNormalAnnotation(node, JPAConst.getRefType(refType), rei);
}
}
}
return true;
}
use of org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType in project jbosstools-hibernate by jbosstools.
the class CollectEntityInfo method processFieldOrGetter.
public boolean processFieldOrGetter(Type type, List<String> list, boolean fieldFlag) {
if (type == null) {
return false;
}
if (type.isPrimitiveType()) {
PrimitiveType pt = (PrimitiveType) type;
if (!pt.getPrimitiveTypeCode().equals(PrimitiveType.BOOLEAN)) {
// this is candidate for primary id
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
if ("version".equalsIgnoreCase(name)) {
// $NON-NLS-1$
FieldGetterType versionFieldGetter = updateFieldGetter(entityInfo.getVersionFieldGetter(), fieldFlag);
entityInfo.setVersionFieldGetter(versionFieldGetter);
} else {
entityInfo.addPrimaryIdCandidate(name);
}
}
}
} else if (type.isSimpleType()) {
SimpleType st = (SimpleType) type;
ITypeBinding tb = st.resolveBinding();
if (tb != null) {
// $NON-NLS-1$
String entityFullyQualifiedName = "";
if (tb.getJavaElement() instanceof SourceType) {
SourceType sourceT = (SourceType) tb.getJavaElement();
entityFullyQualifiedName = sourceT.getFullyQualifiedName();
entityInfo.addDependency(entityFullyQualifiedName);
RefType refType2Use = tb.isEnum() ? RefType.ENUMERATED : RefType.MANY2ONE;
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
entityInfo.addReference(name, entityFullyQualifiedName, refType2Use);
}
} else if (tb.getJavaElement() instanceof BinaryType) {
ITypeBinding tbParent = tb.getTypeDeclaration().getSuperclass();
if (tbParent != null) {
if ("java.lang.Number".equals(tbParent.getBinaryName())) {
// $NON-NLS-1$
// this is candidate for primary id
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
if ("version".equalsIgnoreCase(name)) {
// $NON-NLS-1$
FieldGetterType versionFieldGetter = updateFieldGetter(entityInfo.getVersionFieldGetter(), fieldFlag);
entityInfo.setVersionFieldGetter(versionFieldGetter);
} else {
entityInfo.addPrimaryIdCandidate(name);
}
}
} else if ("java.util.Date".equals(tbParent.getBinaryName())) {
// $NON-NLS-1$
// this is candidate for version
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
if ("version".equalsIgnoreCase(name)) {
// $NON-NLS-1$
FieldGetterType versionFieldGetter = updateFieldGetter(entityInfo.getVersionFieldGetter(), fieldFlag);
entityInfo.setVersionFieldGetter(versionFieldGetter);
}
}
}
}
if ("java.lang.String".equals(tb.getBinaryName())) {
// $NON-NLS-1$
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
entityInfo.updateAnnotationColumn(name, null, false);
entityInfo.addPrimaryIdCandidate(name);
}
}
}
}
} else if (type.isArrayType()) {
ArrayType at = (ArrayType) type;
Type elementType = at.getElementType();
ITypeBinding tb = elementType.resolveBinding();
if (tb != null) {
if (tb.getJavaElement() instanceof SourceType) {
// $NON-NLS-1$
String entityFullyQualifiedName = "";
SourceType sourceT = (SourceType) tb.getJavaElement();
try {
entityFullyQualifiedName = sourceT.getFullyQualifiedParameterizedName();
} catch (JavaModelException e) {
// $NON-NLS-1$
HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e);
}
entityInfo.addDependency(entityFullyQualifiedName);
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
entityInfo.addReference(name, entityFullyQualifiedName, RefType.ONE2MANY);
}
}
}
} else if (type.isParameterizedType()) {
ParameterizedType pt = (ParameterizedType) type;
Type typeP = pt.getType();
ITypeBinding tb = typeP.resolveBinding();
if (tb != null) {
ITypeBinding[] interfaces = Utils.getAllInterfaces(tb);
// $NON-NLS-1$
String fullyQualifiedNameTypeName = "";
if (Utils.isImplementInterface(interfaces, "java.util.Collection")) {
// $NON-NLS-1$
// $NON-NLS-1$
fullyQualifiedNameTypeName = "java.util.Collection";
}
if (Utils.isImplementInterface(interfaces, "java.util.Map")) {
// $NON-NLS-1$
// $NON-NLS-1$
fullyQualifiedNameTypeName = "java.util.Map";
}
/*for (int i = 0; i < interfaces.length; i++) {
if (interfaces[i].getJavaElement() instanceof BinaryType) {
BinaryType binaryT = (BinaryType)interfaces[i].getJavaElement();
String tmp = binaryT.getFullyQualifiedName('.');
if (0 == "java.util.Collection".compareTo(tmp)) { //$NON-NLS-1$
fullyQualifiedNameTypeName = tmp;
break;
}
}
}*/
if (fullyQualifiedNameTypeName.length() > 0) {
Iterator<Type> typeArgsIt = pt.typeArguments().iterator();
while (typeArgsIt.hasNext()) {
typeP = typeArgsIt.next();
tb = typeP.resolveBinding();
// $NON-NLS-1$
String entityFullyQualifiedName = "";
if (tb.getJavaElement() instanceof SourceType) {
SourceType sourceT = (SourceType) tb.getJavaElement();
try {
entityFullyQualifiedName = sourceT.getFullyQualifiedParameterizedName();
} catch (JavaModelException e) {
// $NON-NLS-1$
HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e);
}
entityInfo.addDependency(entityFullyQualifiedName);
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
entityInfo.addReference(name, entityFullyQualifiedName, RefType.ONE2MANY);
}
}
}
}
}
} else if (type.isQualifiedType()) {
QualifiedType qt = (QualifiedType) type;
@SuppressWarnings("unused") ITypeBinding tb = qt.resolveBinding();
} else if (type.isWildcardType()) {
WildcardType wt = (WildcardType) type;
@SuppressWarnings("unused") ITypeBinding tb = wt.resolveBinding();
}
return true;
}
Aggregations