[jbosstools-commits] JBoss Tools SVN: r30331 - trunk/cdi/plugins/org.jboss.tools.cdi.seam.solder.core/src/org/jboss/tools/cdi/seam/solder/core.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Tue Apr 5 14:22:18 EDT 2011


Author: scabanovich
Date: 2011-04-05 14:22:17 -0400 (Tue, 05 Apr 2011)
New Revision: 30331

Removed:
   trunk/cdi/plugins/org.jboss.tools.cdi.seam.solder.core/src/org/jboss/tools/cdi/seam/solder/core/BeanNameFeature.java
Modified:
   trunk/cdi/plugins/org.jboss.tools.cdi.seam.solder.core/src/org/jboss/tools/cdi/seam/solder/core/CDISeamSolderCoreExtension.java
Log:
JBIDE-8200
https://issues.jboss.org/browse/JBIDE-8200

Deleted: trunk/cdi/plugins/org.jboss.tools.cdi.seam.solder.core/src/org/jboss/tools/cdi/seam/solder/core/BeanNameFeature.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.seam.solder.core/src/org/jboss/tools/cdi/seam/solder/core/BeanNameFeature.java	2011-04-05 18:21:42 UTC (rev 30330)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.seam.solder.core/src/org/jboss/tools/cdi/seam/solder/core/BeanNameFeature.java	2011-04-05 18:22:17 UTC (rev 30331)
@@ -1,122 +0,0 @@
-/******************************************************************************* 
- * Copyright (c) 2011 Red Hat, Inc. 
- * Distributed under license by Red Hat, Inc. All rights reserved. 
- * This program is made available under the terms of the 
- * Eclipse Public License v1.0 which accompanies this distribution, 
- * and is available at http://www.eclipse.org/legal/epl-v10.html 
- * 
- * Contributors: 
- * Red Hat, Inc. - initial API and implementation 
- ******************************************************************************/
-package org.jboss.tools.cdi.seam.solder.core;
-
-
-import java.beans.Introspector;
-
-import org.jboss.tools.cdi.core.CDIConstants;
-import org.jboss.tools.cdi.core.CDIUtil;
-import org.jboss.tools.cdi.core.IAnnotationDeclaration;
-import org.jboss.tools.cdi.core.IBean;
-import org.jboss.tools.cdi.core.IClassBean;
-import org.jboss.tools.cdi.core.IProducerField;
-import org.jboss.tools.cdi.core.IProducerMethod;
-import org.jboss.tools.cdi.core.extension.feature.IBeanNameFeature;
-import org.jboss.tools.cdi.internal.core.impl.AbstractBeanElement;
-import org.jboss.tools.cdi.internal.core.impl.AnnotationDeclaration;
-import org.jboss.tools.cdi.internal.core.impl.definition.AbstractMemberDefinition;
-import org.jboss.tools.cdi.internal.core.impl.definition.AbstractTypeDefinition;
-import org.jboss.tools.cdi.internal.core.impl.definition.PackageDefinition;
-import org.jboss.tools.common.util.BeanUtil;
-import org.jboss.tools.common.util.EclipseJavaUtil;
-
-/**
- * 
- * @author Viacheslav Kabanovich
- * 
- */
-public class BeanNameFeature implements IBeanNameFeature {
-	/**
-	 * The singleton instance that processes requests without building inner
-	 * state.
-	 */
-	public static final IBeanNameFeature instance = new BeanNameFeature();
-
-	public String computeBeanName(IBean bean) {
-		AbstractBeanElement abe = (AbstractBeanElement)bean;
-		AbstractMemberDefinition d = abe.getDefinition();
-		if(d == null) return null;
-		IAnnotationDeclaration named = CDIUtil.getNamedDeclaration(bean);
-
-		AbstractTypeDefinition t = d.getTypeDefinition();
-		PackageDefinition p = d.getPackageDefinition();
-		AnnotationDeclaration namedOnPackage = null;
-		AnnotationDeclaration fullyQualifiedOnPackage = null;
-		if(p != null) {
-			namedOnPackage = p.getAnnotation(CDIConstants.NAMED_QUALIFIER_TYPE_NAME);
-			fullyQualifiedOnPackage = p.getAnnotation(CDISeamSolderConstants.FULLY_QUALIFIED_ANNOTATION_TYPE_NAME);
-		}
-
-		AnnotationDeclaration fullyQualified = d.getAnnotation(CDISeamSolderConstants.FULLY_QUALIFIED_ANNOTATION_TYPE_NAME);
-		
-		//@FullyQualified
-		if((fullyQualified != null || fullyQualifiedOnPackage != null) && (named != null || namedOnPackage != null)) {
-			if(named == null) named = namedOnPackage;
-			String pkg = resolvePackageName(fullyQualified, fullyQualifiedOnPackage, t, p);
-			String simpleName = getSimpleBeanName(bean, named);
-			return (simpleName == null) ? null : pkg.length() > 0 ? pkg + "." + simpleName : simpleName;			
-		}
-
-		// @Named on package only
-		if(named == null && namedOnPackage != null) {
-			return getSimpleBeanName(bean, namedOnPackage);
-		}
-
-		return null;
-	}
-
-	private String getStringValue(IAnnotationDeclaration a) {
-		if(a == null) return null;
-		Object o = a.getMemberValue(null);
-		return o == null ? null : o.toString();
-	}
-
-	private String resolvePackageName(AnnotationDeclaration fullyQualified, AnnotationDeclaration fullyQualifiedOnPackage, AbstractTypeDefinition t, PackageDefinition p) {
-		String contextClass = null;
-		AnnotationDeclaration a = fullyQualified != null ? fullyQualified : fullyQualifiedOnPackage;
-		contextClass = getStringValue(a);
-		if(contextClass == null) {
-			contextClass = t == null ? "" : t.getQualifiedName();
-		} else if(fullyQualified != null && t != null) {
-			String resolved = EclipseJavaUtil.resolveType(t.getType(), contextClass);
-			if(resolved != null) contextClass = resolved;				
-		} else if(fullyQualifiedOnPackage != null) {
-			contextClass = p.resolveType(contextClass);
-		}
-		int dot = contextClass.lastIndexOf('.');
-		return dot < 0 ? "" : contextClass.substring(0, dot);
-	}
-
-	private String getSimpleBeanName(IBean bean, IAnnotationDeclaration named) {
-		String simpleName = null;
-		if(named != null) {
-			simpleName = getStringValue(named);
-		}
-		if(simpleName != null && simpleName.length() > 0) {
-			//do nothing
-		} else if(bean instanceof IClassBean) {
-			simpleName = Introspector.decapitalize(((IClassBean)bean).getBeanClass().getElementName());
-		} else if(bean instanceof IProducerField) {
-			simpleName = ((IProducerField)bean).getField().getElementName();
-		} else if(bean instanceof IProducerMethod) {
-			IProducerMethod m = (IProducerMethod)bean;
-			String mn = m.getMethod().getElementName();
-			if(BeanUtil.isGetter(m.getMethod())) {
-				simpleName = BeanUtil.getPropertyName(mn);
-			} else {
-				simpleName = mn;
-			}
-		}
-		
-		return simpleName;
-	}
-}

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.seam.solder.core/src/org/jboss/tools/cdi/seam/solder/core/CDISeamSolderCoreExtension.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.seam.solder.core/src/org/jboss/tools/cdi/seam/solder/core/CDISeamSolderCoreExtension.java	2011-04-05 18:21:42 UTC (rev 30330)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.seam.solder.core/src/org/jboss/tools/cdi/seam/solder/core/CDISeamSolderCoreExtension.java	2011-04-05 18:22:17 UTC (rev 30331)
@@ -1,41 +1,62 @@
 package org.jboss.tools.cdi.seam.solder.core;
 
 
+import java.beans.Introspector;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
 import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IMemberValuePair;
 import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.SourceRange;
+import org.eclipse.jdt.internal.core.MemberValuePair;
+import org.jboss.tools.cdi.core.CDIConstants;
 import org.jboss.tools.cdi.core.IAnnotated;
 import org.jboss.tools.cdi.core.IAnnotationDeclaration;
-import org.jboss.tools.cdi.core.IParametedType;
 import org.jboss.tools.cdi.core.extension.ICDIExtension;
-import org.jboss.tools.cdi.core.extension.feature.IBeanNameFeature;
 import org.jboss.tools.cdi.core.extension.feature.IProcessAnnotatedTypeFeature;
+import org.jboss.tools.cdi.internal.core.impl.AnnotationLiteral;
 import org.jboss.tools.cdi.internal.core.impl.ParametedType;
 import org.jboss.tools.cdi.internal.core.impl.TypeDeclaration;
+import org.jboss.tools.cdi.internal.core.impl.definition.AbstractMemberDefinition;
+import org.jboss.tools.cdi.internal.core.impl.definition.AbstractTypeDefinition;
+import org.jboss.tools.cdi.internal.core.impl.definition.AnnotationDefinition;
 import org.jboss.tools.cdi.internal.core.impl.definition.DefinitionContext;
 import org.jboss.tools.cdi.internal.core.impl.definition.FieldDefinition;
 import org.jboss.tools.cdi.internal.core.impl.definition.MethodDefinition;
+import org.jboss.tools.cdi.internal.core.impl.definition.PackageDefinition;
 import org.jboss.tools.cdi.internal.core.impl.definition.ParameterDefinition;
 import org.jboss.tools.cdi.internal.core.impl.definition.TypeDefinition;
 import org.jboss.tools.common.model.util.EclipseResourceUtil;
+import org.jboss.tools.common.util.BeanUtil;
 import org.jboss.tools.common.util.EclipseJavaUtil;
 
-public class CDISeamSolderCoreExtension implements ICDIExtension,
-		IProcessAnnotatedTypeFeature {
+public class CDISeamSolderCoreExtension implements ICDIExtension, IProcessAnnotatedTypeFeature {
 
 	public Object getAdapter(Class adapter) {
-		if (adapter == IBeanNameFeature.class) {
-			return BeanNameFeature.instance;
-		}
 		return null;
 	}
 
-	public void processAnnotatedType(TypeDefinition typeDefinition,
-			DefinitionContext context) {
+	public void processAnnotatedType(TypeDefinition typeDefinition, DefinitionContext context) {
+
+		if(processVeto(typeDefinition, context)) {
+			return;
+		}
+
+		if(processRequires(typeDefinition, context)) {
+			return;
+		}
+	
+		processNames(typeDefinition, context);
+
+		processExact(typeDefinition, context);
+
+	}
+
+	// @Veto
+	private boolean processVeto(TypeDefinition typeDefinition, DefinitionContext context) {
 		if (typeDefinition
 				.isAnnotationPresent(CDISeamSolderConstants.VETO_ANNOTATION_TYPE_NAME)
 				|| (typeDefinition.getPackageDefinition() != null && typeDefinition
@@ -43,9 +64,13 @@
 						.isAnnotationPresent(
 								CDISeamSolderConstants.VETO_ANNOTATION_TYPE_NAME))) {
 			typeDefinition.veto();
-			return;
+			return true;
 		}
+		return false;
+	}
 
+	// @Requires
+	private boolean processRequires(TypeDefinition typeDefinition, DefinitionContext context) {
 		Set<String> requiredClasses = new HashSet<String>();
 		List<String> typeRequiredClasses = getRequiredClasses(typeDefinition);
 		if (typeRequiredClasses != null)
@@ -62,36 +87,77 @@
 				try {
 					if (EclipseJavaUtil.findType(jp, c) == null) {
 						typeDefinition.veto();
-						return;
+						return true;
 					}
 				} catch (JavaModelException e) {
 					CDISeamSolderCorePlugin.getDefault().logError(e);
 					typeDefinition.veto();
-					return;
+					return true;
 				}
 			}
 		}
+		return false;
+	}
 
+	// @FullyQualified @Named
+	private void processNames(TypeDefinition typeDefinition, DefinitionContext context) {
+		PackageDefinition p = typeDefinition.getPackageDefinition();
+		IAnnotationDeclaration namedOnPackage = null;
+		IAnnotationDeclaration fullyQualifiedOnPackage = null;
+		if(p != null) {
+			namedOnPackage = p.getAnnotation(CDIConstants.NAMED_QUALIFIER_TYPE_NAME);
+			fullyQualifiedOnPackage = p.getAnnotation(CDISeamSolderConstants.FULLY_QUALIFIED_ANNOTATION_TYPE_NAME);
+		}
+
+		processNames(typeDefinition, context, namedOnPackage, fullyQualifiedOnPackage, p);
+	
 		List<FieldDefinition> fs = typeDefinition.getFields();
-		for (FieldDefinition f : fs) {
-			TypeDeclaration exact = getExactType(f, typeDefinition, context);
-			if (exact != null) {
-				f.setOverridenType(exact);
+		for (FieldDefinition f: fs) {
+			if(f.isAnnotationPresent(CDIConstants.PRODUCES_ANNOTATION_TYPE_NAME)) {
+				processNames(f, context, null, fullyQualifiedOnPackage, p);
 			}
 		}
-
+		
 		List<MethodDefinition> ms = typeDefinition.getMethods();
-		for (MethodDefinition m : ms) {
-			List<ParameterDefinition> ps = m.getParameters();
-			for (ParameterDefinition p : ps) {
-				TypeDeclaration exact = getExactType(p, typeDefinition, context);
-				if (exact != null) {
-					p.setOverridenType(exact);
-				}
+		for (MethodDefinition m: ms) {
+			if(m.isAnnotationPresent(CDIConstants.PRODUCES_ANNOTATION_TYPE_NAME)) {
+				processNames(m, context, null, fullyQualifiedOnPackage, p);
 			}
 		}
+		
 	}
 
+	private void processNames(AbstractMemberDefinition d, DefinitionContext context,
+			IAnnotationDeclaration namedOnPackage, IAnnotationDeclaration fullyQualifiedOnPackage, PackageDefinition p) {
+		IAnnotationDeclaration named = d.getAnnotation(CDIConstants.NAMED_QUALIFIER_TYPE_NAME);
+		IAnnotationDeclaration fullyQualified = d.getAnnotation(CDISeamSolderConstants.FULLY_QUALIFIED_ANNOTATION_TYPE_NAME);
+
+		String beanName = null;
+
+		if((fullyQualified != null || fullyQualifiedOnPackage != null) && (named != null || namedOnPackage != null)) {
+			//@FullyQualified
+			if(named == null) named = namedOnPackage;
+			String pkg = resolvePackageName(fullyQualified, fullyQualifiedOnPackage, d.getTypeDefinition(), p);
+			String simpleName = getSimpleBeanName(d, named);
+			beanName = (simpleName == null) ? null : pkg.length() > 0 ? pkg + "." + simpleName : simpleName;			
+		} else if(named == null && namedOnPackage != null) {
+			// @Named on package only
+			beanName = getSimpleBeanName(d, namedOnPackage);
+		}
+		
+		if(beanName != null) {
+			IMemberValuePair[] pairs = new IMemberValuePair[]{new MemberValuePair("value", beanName, IMemberValuePair.K_STRING)};
+			AnnotationDefinition n = context.getAnnotation(CDIConstants.NAMED_QUALIFIER_TYPE_NAME);
+			if(n != null) {
+				AnnotationLiteral l = new AnnotationLiteral(d.getResource(), "", new SourceRange(0, 0), pairs, n.getType());
+				if(named != null) d.removeAnnotation(named);
+				d.addAnnotation(l, context);
+			}
+		}
+
+	}
+
+
 	private List<String> getRequiredClasses(IAnnotated d) {
 		if (d == null)
 			return null;
@@ -115,6 +181,28 @@
 		return result;
 	}
 
+	// @Exact
+	private void processExact(TypeDefinition typeDefinition, DefinitionContext context) {
+		List<FieldDefinition> fs = typeDefinition.getFields();
+		for (FieldDefinition f : fs) {
+			TypeDeclaration exact = getExactType(f, typeDefinition, context);
+			if (exact != null) {
+				f.setOverridenType(exact);
+			}
+		}
+
+		List<MethodDefinition> ms = typeDefinition.getMethods();
+		for (MethodDefinition m : ms) {
+			List<ParameterDefinition> ps = m.getParameters();
+			for (ParameterDefinition p : ps) {
+				TypeDeclaration exact = getExactType(p, typeDefinition, context);
+				if (exact != null) {
+					p.setOverridenType(exact);
+				}
+			}
+		}
+	}
+
 	private TypeDeclaration getExactType(IAnnotated annotated, TypeDefinition declaringType, DefinitionContext context) {
 		IAnnotationDeclaration a = annotated.getAnnotation(CDISeamSolderConstants.EXACT_ANNOTATION_TYPE_NAME);
 		if(a != null) {
@@ -143,4 +231,54 @@
 		}
 		return null;
 	}
+
+	private String resolvePackageName(IAnnotationDeclaration fullyQualified, IAnnotationDeclaration fullyQualifiedOnPackage, AbstractTypeDefinition t, PackageDefinition p) {
+		String contextClass = null;
+		IAnnotationDeclaration a = fullyQualified != null ? fullyQualified : fullyQualifiedOnPackage;
+		contextClass = getStringValue(a);
+		if(contextClass == null) {
+			contextClass = t == null ? "" : t.getQualifiedName();
+		} else if(fullyQualified != null && t != null) {
+			String resolved = EclipseJavaUtil.resolveType(t.getType(), contextClass);
+			if(resolved != null) contextClass = resolved;				
+		} else if(fullyQualifiedOnPackage != null) {
+			contextClass = p.resolveType(contextClass);
+		}
+		if("java.lang.Class".equals(contextClass)) {
+			contextClass = t.getType().getFullyQualifiedName();
+		}
+		int dot = contextClass.lastIndexOf('.');
+		return dot < 0 ? "" : contextClass.substring(0, dot);
+	}
+
+	private String getSimpleBeanName(AbstractMemberDefinition d, IAnnotationDeclaration named) {
+		String simpleName = null;
+		if(named != null) {
+			simpleName = getStringValue(named);
+		}
+		if(simpleName != null && simpleName.length() > 0) {
+			//do nothing
+		} else if(d instanceof TypeDefinition) {
+			simpleName = Introspector.decapitalize(((TypeDefinition)d).getType().getElementName());
+		} else if(d instanceof FieldDefinition) {
+			simpleName = ((FieldDefinition)d).getField().getElementName();
+		} else if(d instanceof MethodDefinition) {
+			MethodDefinition m = (MethodDefinition)d;
+			String mn = m.getMethod().getElementName();
+			if(BeanUtil.isGetter(m.getMethod())) {
+				simpleName = BeanUtil.getPropertyName(mn);
+			} else {
+				simpleName = mn;
+			}
+		}
+		
+		return simpleName;
+	}
+
+	private String getStringValue(IAnnotationDeclaration a) {
+		if(a == null) return null;
+		Object o = a.getMemberValue(null);
+		return o == null ? null : o.toString();
+	}
+
 }



More information about the jbosstools-commits mailing list