Author: scabanovich
Date: 2007-07-09 09:38:58 -0400 (Mon, 09 Jul 2007)
New Revision: 2358
Added:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/ComponentBuilder.java
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/ValueInfo.java
Modified:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamAnnotatedFactory.java
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamComponentDeclaration.java
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamJavaComponentDeclaration.java
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamProject.java
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/ASTVisitorImpl.java
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/JavaScanner.java
Log:
EXIN-217 Java source scanner is being developed.
Modified:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamAnnotatedFactory.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamAnnotatedFactory.java 2007-07-09
08:09:04 UTC (rev 2357)
+++
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamAnnotatedFactory.java 2007-07-09
13:38:58 UTC (rev 2358)
@@ -10,12 +10,17 @@
******************************************************************************/
package org.jboss.tools.seam.internal.core;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
+import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IMethod;
import org.jboss.tools.seam.core.ISeamAnnotatedFactory;
+import org.jboss.tools.seam.core.ISeamTextSourceReference;
import org.jboss.tools.seam.core.ISeamXmlComponentDeclaration;
import org.jboss.tools.seam.core.event.Change;
+import org.jboss.tools.seam.internal.core.scanner.java.ValueInfo;
/**
* @author Viacheslav Kabanovich
@@ -23,6 +28,8 @@
public class SeamAnnotatedFactory extends SeamJavaContextVariable implements
ISeamAnnotatedFactory {
boolean autoCreate = false;
+ protected Map<String,ValueInfo> attributes = new HashMap<String,
ValueInfo>();
+
public IMethod getSourceMethod() {
return (IMethod)javaSource;
}
@@ -51,4 +58,43 @@
return changes;
}
+ /**
+ * @param path
+ * @return source reference for some member of declaration.
+ * e.g. if you need source reference for @Name you have to
+ * invoke getLocationFor("name");
+ */
+ public ISeamTextSourceReference getLocationFor(String path) {
+ final ValueInfo valueInfo = attributes.get(path);
+ ISeamTextSourceReference reference = new ISeamTextSourceReference() {
+ public int getLength() {
+ return valueInfo != null ? valueInfo.getLength() : 0;
+ }
+
+ public IResource getResource() {
+ return SeamAnnotatedFactory.this.getResource();
+ }
+
+ public int getStartPosition() {
+ return valueInfo != null ? valueInfo.getStartPosition() : 0;
+ }
+ };
+ return reference;
+ }
+
+ public void setName(ValueInfo value) {
+ attributes.put(ISeamXmlComponentDeclaration.NAME, value);
+ name = value == null ? null : value.getValue();
+ }
+
+ public void setScope(ValueInfo value) {
+ attributes.put(ISeamXmlComponentDeclaration.SCOPE, value);
+ setScopeAsString(value == null ? null : value.getValue());
+ }
+
+ public void setAutoCreate(ValueInfo value) {
+ attributes.put(ISeamXmlComponentDeclaration.AUTO_CREATE, value);
+ setAutoCreate(value != null && "true".equals(value.getValue()));
+ }
+
}
Modified:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamComponentDeclaration.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamComponentDeclaration.java 2007-07-09
08:09:04 UTC (rev 2357)
+++
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamComponentDeclaration.java 2007-07-09
13:38:58 UTC (rev 2358)
@@ -10,15 +10,18 @@
******************************************************************************/
package org.jboss.tools.seam.internal.core;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.jboss.tools.seam.core.ISeamComponentDeclaration;
import org.jboss.tools.seam.core.ISeamTextSourceReference;
-import org.jboss.tools.seam.core.SeamCorePlugin;
+import org.jboss.tools.seam.core.ISeamXmlComponentDeclaration;
import org.jboss.tools.seam.core.event.Change;
+import org.jboss.tools.seam.internal.core.scanner.java.ValueInfo;
/**
* @author Viacheslav Kabanovich
@@ -44,6 +47,8 @@
*/
protected String name;
+ protected Map<String,ValueInfo> attributes = new HashMap<String,
ValueInfo>();
+
public Object getId() {
return id;
}
@@ -106,6 +111,9 @@
}
if(id != d.id) id = d.id;
+ //be more specific
+ this.attributes = d.attributes;
+
return changes;
}
@@ -117,13 +125,13 @@
* @param path
* @return source reference for some member of declaration.
* e.g. if you need source reference for @Name you have to
- * invore getLocationFor("name");
+ * invoke getLocationFor("name");
*/
public ISeamTextSourceReference getLocationFor(String path) {
- // TODO
+ final ValueInfo valueInfo = attributes.get(path);
ISeamTextSourceReference reference = new ISeamTextSourceReference() {
public int getLength() {
- return 10;
+ return valueInfo != null ? valueInfo.getLength() : 0;
}
public IResource getResource() {
@@ -131,9 +139,15 @@
}
public int getStartPosition() {
- return 0;
+ return valueInfo != null ? valueInfo.getStartPosition() : 0;
}
};
return reference;
}
+
+ public void setName(ValueInfo value) {
+ attributes.put(ISeamXmlComponentDeclaration.NAME, value);
+ name = value == null ? null : value.getValue();
+ }
+
}
\ No newline at end of file
Modified:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamJavaComponentDeclaration.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamJavaComponentDeclaration.java 2007-07-09
08:09:04 UTC (rev 2357)
+++
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamJavaComponentDeclaration.java 2007-07-09
13:38:58 UTC (rev 2358)
@@ -11,10 +11,12 @@
import org.jboss.tools.seam.core.IRole;
import org.jboss.tools.seam.core.ISeamComponentMethod;
import org.jboss.tools.seam.core.ISeamJavaComponentDeclaration;
+import org.jboss.tools.seam.core.ISeamXmlComponentDeclaration;
import org.jboss.tools.seam.core.ScopeType;
import org.jboss.tools.seam.core.SeamComponentMethodType;
import org.jboss.tools.seam.core.SeamComponentPrecedenceType;
import org.jboss.tools.seam.core.event.Change;
+import org.jboss.tools.seam.internal.core.scanner.java.ValueInfo;
public class SeamJavaComponentDeclaration extends SeamComponentDeclaration
implements ISeamJavaComponentDeclaration {
@@ -202,4 +204,20 @@
public void setPrecedence(int precedence) {
this.precedence = precedence;
}
-}
\ No newline at end of file
+
+ public void setScope(ValueInfo value) {
+ attributes.put(ISeamXmlComponentDeclaration.SCOPE, value);
+ setScope(value == null ? null : value.getValue());
+ }
+
+ public void setPrecedence(ValueInfo value) {
+ attributes.put(ISeamXmlComponentDeclaration.PRECEDENCE, value);
+ try {
+ setPrecedence(value == null ? 0 : Integer.parseInt(value.getValue()));
+ } catch (NumberFormatException e) {
+ setPrecedence(-1); //error value
+ //ignore - exact value is stored in ValueInfo
+ }
+ }
+
+}
Modified:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamProject.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamProject.java 2007-07-09
08:09:04 UTC (rev 2357)
+++
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/SeamProject.java 2007-07-09
13:38:58 UTC (rev 2358)
@@ -121,7 +121,7 @@
String name = loaded.getName();
- boolean nameChanged = current != null && !name.equals(current.getName());
+ boolean nameChanged = current != null && !stringsEqual(name,
current.getName());
SeamComponent c = getComponent(name);
@@ -203,6 +203,10 @@
factoryDeclarationsRemoved(currentFactories);
}
+ boolean stringsEqual(String s1, String s2) {
+ return s1 == null ? s2 == null : s1.equals(s2);
+ }
+
/**
* Package local method called by builder.
* @param source
@@ -416,7 +420,7 @@
}
public SeamComponent getComponent(String name) {
- return allComponents.get(name);
+ return name == null ? null : allComponents.get(name);
}
SeamComponent newComponent(String name) {
Modified:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/ASTVisitorImpl.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/ASTVisitorImpl.java 2007-07-09
08:09:04 UTC (rev 2357)
+++
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/ASTVisitorImpl.java 2007-07-09
13:38:58 UTC (rev 2358)
@@ -18,43 +18,38 @@
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.Block;
-import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
-import org.eclipse.jdt.core.dom.MemberValuePair;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.NormalAnnotation;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
-import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclaration;
/**
- * This object collects changes in target that should be fired to listeners.
+ * Processes AST tree to find annotated type, fields and methods.
*
* @author Viacheslav Kabanovich
*/
public class ASTVisitorImpl extends ASTVisitor implements SeamAnnotations {
- static int UNDEFINED_CONTEXT = -1;
- static int TYPE_CONTEXT = 0;
- static int FIELD_CONTEXT = 1;
- static int METHOD_CONTEXT = 2;
-
IType type;
- String name = null;
- String scope = null;
AnnotatedASTNode annotatedType = null;
- Set<Object> annotatedFields = new HashSet<Object>();
- Set<Object> annotatedMethods = new HashSet<Object>();
+ Set<AnnotatedASTNode> annotatedFields = new HashSet<AnnotatedASTNode>();
+ Set<AnnotatedASTNode> annotatedMethods = new HashSet<AnnotatedASTNode>();
AnnotatedASTNode currentAnnotatedNode = null;
- int context = UNDEFINED_CONTEXT;
+ public boolean hasSeamComponent() {
+ if(annotatedFields.size() > 0 || annotatedMethods.size() > 0) return true;
+ if(annotatedType != null && annotatedType.getAnnotations() != null) return
true;
+ return false;
+ }
+
public boolean visit(SingleMemberAnnotation node) {
String type = resolveType(node);
if(isSeamAnnotationType(type) && currentAnnotatedNode != null) {
@@ -100,18 +95,10 @@
}
boolean isSeamAnnotationType(String n) {
- return n != null && n.startsWith(SEAM_ANNOTATION_TYPE_PREFIX);
+ return n != null && (n.startsWith(SEAM_ANNOTATION_TYPE_PREFIX)
+ || n.equals(STATEFUL_ANNOTATION_TYPE));
}
- String checkExpression(Expression exp) {
- if(exp instanceof StringLiteral) {
- return ((StringLiteral)exp).getLiteralValue();
- } else if(exp instanceof QualifiedName) {
- return exp.toString();
- }
- return null;
- }
-
public boolean visit(Block node) {
return false;
}
@@ -125,7 +112,6 @@
public void endVisit(TypeDeclaration node) {
currentAnnotatedNode = null;
- process();
}
public boolean visit(FieldDeclaration node) {
@@ -134,7 +120,7 @@
}
public void endVisit(FieldDeclaration node) {
- List fragments = node.fragments();
+ List<?> fragments = node.fragments();
for (int i = 0; i < fragments.size(); i++) {
VariableDeclaration vd = (VariableDeclaration)fragments.get(i);
String name = vd.getName().getIdentifier();
@@ -158,47 +144,4 @@
currentAnnotatedNode = null;
}
- void process() {
- if(annotatedType == null) return;
- ResolvedAnnotation[] as = annotatedType.getAnnotations();
- for (int i = 0; i < as.length; i++) {
- String type = as[i].getType();
- if(NAME_ANNOTATION_TYPE.equals(type)) {
- name = getValue(as[i].getAnnotation());
- } else if(SCOPE_ANNOTATION_TYPE.equals(type)) {
- scope = getValue(as[i].getAnnotation());
- if(scope != null) {
- int q = scope.lastIndexOf('.');
- if(q >= 0) scope = scope.substring(q + 1).toLowerCase();
- }
- }
- //TODO
- }
- //TODO
- }
-
- String getValue(Annotation node) {
- if(node instanceof SingleMemberAnnotation) {
- return getValue((SingleMemberAnnotation)node);
- } else if(node instanceof NormalAnnotation) {
- return getValue((NormalAnnotation)node);
- } else {
- return null;
- }
- }
-
- String getValue(SingleMemberAnnotation node) {
- return checkExpression(node.getValue());
- }
-
- String getValue(NormalAnnotation node) {
- List vs = node.values();
- if(vs != null) for (int i = 0; i < vs.size(); i++) {
- MemberValuePair p = (MemberValuePair)vs.get(i);
- if("value".equals(p.getName().getIdentifier())) {
- return checkExpression(p.getValue());
- }
- }
- return null;
- }
}
Added:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/ComponentBuilder.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/ComponentBuilder.java
(rev 0)
+++
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/ComponentBuilder.java 2007-07-09
13:38:58 UTC (rev 2358)
@@ -0,0 +1,145 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Exadel, Inc. and 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:
+ * Exadel, Inc. and Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.seam.internal.core.scanner.java;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IMethod;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.dom.Annotation;
+import org.eclipse.jdt.core.dom.MethodDeclaration;
+import org.jboss.tools.seam.core.ISeamXmlComponentDeclaration;
+import org.jboss.tools.seam.internal.core.SeamAnnotatedFactory;
+import org.jboss.tools.seam.internal.core.SeamJavaComponentDeclaration;
+import org.jboss.tools.seam.internal.core.scanner.LoadedDeclarations;
+
+/**
+ * Builds component using results of ASTVisitorImpl
+ *
+ * @author Viacheslav Kabanovich
+ */
+public class ComponentBuilder implements SeamAnnotations {
+ LoadedDeclarations ds = null;
+
+ AnnotatedASTNode annotatedType = null;
+ Set<AnnotatedASTNode> annotatedFields = new HashSet<AnnotatedASTNode>();
+ Set<AnnotatedASTNode> annotatedMethods = new HashSet<AnnotatedASTNode>();
+
+ SeamJavaComponentDeclaration component = new SeamJavaComponentDeclaration();
+
+
+ public ComponentBuilder(LoadedDeclarations ds, ASTVisitorImpl visitor) {
+ annotatedType = visitor.annotatedType;
+ annotatedFields = visitor.annotatedFields;
+ annotatedMethods = visitor.annotatedMethods;
+
+ String n = visitor.type.getElementName();
+ n = JavaScanner.getResolvedType(visitor.type, n);
+
+ ds.getComponents().add(component);
+ component.setType(visitor.type);
+ component.setId(visitor.type);
+ component.setClassName(n);
+
+ process();
+ }
+
+ void process() {
+ if(annotatedType == null) return;
+ ResolvedAnnotation[] as = annotatedType.getAnnotations();
+ for (int i = 0; i < as.length; i++) {
+ String type = as[i].getType();
+ if(NAME_ANNOTATION_TYPE.equals(type)) {
+ component.setName(ValueInfo.getValueInfo(as[i].getAnnotation(), null));
+ } else if(SCOPE_ANNOTATION_TYPE.equals(type)) {
+ ValueInfo scope = ValueInfo.getValueInfo(as[i].getAnnotation(), null);
+ if(scope != null && scope.value != null) {
+ int q = scope.value.lastIndexOf('.');
+ if(q >= 0) scope.value = scope.value.substring(q + 1).toLowerCase();
+ }
+ component.setScope(scope);
+ } else if(INSTALL_ANNOTATION_TYPE.equals(type)) {
+ component.setPrecedence(ValueInfo.getValueInfo(as[i].getAnnotation(),
"precedence"));
+ } else if(STATEFUL_ANNOTATION_TYPE.equals(type)) {
+ ValueInfo stateful = new ValueInfo();
+ stateful.value = "true";
+ stateful.valueStartPosition = as[i].getAnnotation().getStartPosition();
+ stateful.valueLength = as[i].getAnnotation().getLength();
+ component.setStateful(true);
+ }
+ //TODO entity
+ }
+
+ processFactories();
+ processBijections();
+ processComponentMethods();
+
+ //TODO
+ }
+
+ void processFactories() {
+ for (AnnotatedASTNode n: annotatedMethods) {
+ Annotation a = findAnnotation(n, FACTORY_ANNOTATION_TYPE);
+ if(a == null) continue;
+ MethodDeclaration m = (MethodDeclaration)n.getNode();
+ ValueInfo factoryName = ValueInfo.getValueInfo(a, null);
+ if(factoryName == null) {
+ factoryName = new ValueInfo();
+ factoryName.value = m.getName().getIdentifier();
+ factoryName.valueLength = m.getName().getLength();
+ factoryName.valueStartPosition = m.getName().getStartPosition();
+ }
+ System.out.println("");
+ ValueInfo scope = ValueInfo.getValueInfo(a, ISeamXmlComponentDeclaration.SCOPE);
+ ValueInfo autoCreate = ValueInfo.getValueInfo(a, "autoCreate");
+
+ SeamAnnotatedFactory factory = new SeamAnnotatedFactory();
+ factory.setMethod(findMethod(m));
+ factory.setName(factoryName);
+ if(autoCreate != null) factory.setAutoCreate(true);
+ if(scope != null) {
+ factory.setScope(scope);
+ }
+ ds.getFactories().add(factory);
+ }
+ }
+
+ void processBijections() {
+ //TODO
+ }
+
+ void processComponentMethods() {
+ //TODO
+ }
+
+ void processRoles() {
+ //TODO
+ }
+
+ private Annotation findAnnotation(AnnotatedASTNode n, String type) {
+ ResolvedAnnotation[] as = n.getAnnotations();
+ for (int i = 0; i < as.length; i++) {
+ if(type.equals(as[i].getType())) return as[i].getAnnotation();
+ }
+ return null;
+ }
+
+ private IMethod findMethod(MethodDeclaration m) {
+// IType type = (IType)component.getSourceMember();
+ IJavaElement e = m.resolveBinding().getJavaElement();
+ if(e instanceof IMethod) return (IMethod)e;
+ return null;
+ }
+
+}
Modified:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/JavaScanner.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/JavaScanner.java 2007-07-09
08:09:04 UTC (rev 2357)
+++
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/JavaScanner.java 2007-07-09
13:38:58 UTC (rev 2358)
@@ -25,7 +25,6 @@
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.jboss.tools.common.model.util.EclipseResourceUtil;
import org.jboss.tools.common.util.FileUtil;
-import org.jboss.tools.seam.internal.core.SeamJavaComponentDeclaration;
import org.jboss.tools.seam.internal.core.scanner.IFileScanner;
import org.jboss.tools.seam.internal.core.scanner.LoadedDeclarations;
@@ -114,7 +113,6 @@
}
public void acceptAST(ICompilationUnit source, CompilationUnit ast) {
- visitor.name = null;
try {
IType[] ts = source.getTypes();
@@ -125,24 +123,13 @@
//ignore
}
ast.accept(visitor);
- if(visitor.name != null && visitor.type != null) {
- String n = visitor.type.getElementName();
- n = getResolvedType(visitor.type, n);
- SeamJavaComponentDeclaration component = new SeamJavaComponentDeclaration();
-
- component.setId(visitor.type);
- component.setSourcePath(sourcePath);
- component.setResource(resource);
-
- ds.getComponents().add(component);
- component.setType(visitor.type);
- component.setId(visitor.type);
- component.setClassName(n);
- component.setName(visitor.name);
- if(visitor.scope != null) {
- component.setScope(visitor.scope);
- }
- }
+
+ if(!visitor.hasSeamComponent()) return;
+
+ ComponentBuilder b = new ComponentBuilder(ds, visitor);
+
+ b.component.setSourcePath(sourcePath);
+ b.component.setResource(resource);
}
}
Added:
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/ValueInfo.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/ValueInfo.java
(rev 0)
+++
trunk/seam/plugins/org.jboss.tools.seam.core/src/org/jboss/tools/seam/internal/core/scanner/java/ValueInfo.java 2007-07-09
13:38:58 UTC (rev 2358)
@@ -0,0 +1,81 @@
+package org.jboss.tools.seam.internal.core.scanner.java;
+
+import java.util.List;
+
+import org.eclipse.jdt.core.dom.Annotation;
+import org.eclipse.jdt.core.dom.Expression;
+import org.eclipse.jdt.core.dom.MemberValuePair;
+import org.eclipse.jdt.core.dom.NormalAnnotation;
+import org.eclipse.jdt.core.dom.QualifiedName;
+import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
+import org.eclipse.jdt.core.dom.StringLiteral;
+
+public class ValueInfo {
+ String value;
+ int valueStartPosition;
+ int valueLength;
+
+ /**
+ * Factory method.
+ * @param node
+ * @param name
+ * @return
+ */
+ public static ValueInfo getValueInfo(Annotation node, String name) {
+ if(name == null) name = "value";
+ if(node instanceof SingleMemberAnnotation) {
+ if(name == null || "value".equals(name)) {
+ SingleMemberAnnotation m = (SingleMemberAnnotation)node;
+ ValueInfo result = new ValueInfo();
+ Expression exp = m.getValue();
+ result.valueLength = exp.getLength();
+ result.valueStartPosition = exp.getStartPosition();
+ result.value = checkExpression(exp);
+ return result;
+ }
+ return null;
+ } else if(node instanceof NormalAnnotation) {
+ NormalAnnotation n = (NormalAnnotation)node;
+ List<?> vs = n.values();
+ if(vs != null) for (int i = 0; i < vs.size(); i++) {
+ MemberValuePair p = (MemberValuePair)vs.get(i);
+ String pname = p.getName().getIdentifier();
+ if(!name.equals(pname)) continue;
+ ValueInfo result = new ValueInfo();
+ Expression exp = p.getValue();
+ result.valueLength = exp.getLength();
+ result.valueStartPosition = exp.getStartPosition();
+ result.value = checkExpression(exp);
+ return result;
+ }
+ return null;
+ }
+ return null;
+ }
+
+ public ValueInfo() {
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public int getStartPosition() {
+ return valueStartPosition;
+ }
+
+ public int getLength() {
+ return valueLength;
+ }
+
+ static String checkExpression(Expression exp) {
+ if(exp == null) return null;
+ if(exp instanceof StringLiteral) {
+ return ((StringLiteral)exp).getLiteralValue();
+ } else if(exp instanceof QualifiedName) {
+ return exp.toString();
+ }
+ return exp.toString();
+ }
+
+}