Skip to main content
summaryrefslogtreecommitdiffstats
path: root/jpa
diff options
context:
space:
mode:
authorbvosburgh2007-05-08 18:10:55 +0000
committerbvosburgh2007-05-08 18:10:55 +0000
commite747295bae27a785e99864e166629e874aa6acb1 (patch)
tree8a647489d57fc645976dc756b3b87552894805c7 /jpa
parentcf550dd2a99223ea442f1b68d0a1246af5e517bc (diff)
downloadwebtools.dali-e747295bae27a785e99864e166629e874aa6acb1.tar.gz
webtools.dali-e747295bae27a785e99864e166629e874aa6acb1.tar.xz
webtools.dali-e747295bae27a785e99864e166629e874aa6acb1.zip
added StringArrayExpressionConverter
Diffstat (limited to 'jpa')
-rw-r--r--jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/content/java/mappings/JavaRelationshipMapping.java8
-rw-r--r--jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/AbstractExpressionConverter.java18
-rw-r--r--jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/BooleanStringExpressionConverter.java23
-rw-r--r--jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/CharacterStringExpressionConverter.java23
-rw-r--r--jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/ExpressionConverter.java8
-rw-r--r--jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NameStringExpressionConverter.java23
-rw-r--r--jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NullExpressionConverter.java18
-rw-r--r--jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NumberStringExpressionConverter.java23
-rw-r--r--jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/PrimitiveTypeStringExpressionConverter.java23
-rw-r--r--jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/SimpleTypeStringExpressionConverter.java23
-rw-r--r--jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/StringArrayExpressionConverter.java59
-rw-r--r--jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/StringExpressionConverter.java23
-rw-r--r--jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/MemberAnnotationElementAdapterTests.java34
13 files changed, 208 insertions, 98 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/content/java/mappings/JavaRelationshipMapping.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/content/java/mappings/JavaRelationshipMapping.java
index 62ee3e06ba..958583787a 100644
--- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/content/java/mappings/JavaRelationshipMapping.java
+++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/content/java/mappings/JavaRelationshipMapping.java
@@ -1,8 +1,8 @@
/*******************************************************************************
* Copyright (c) 2006, 2007 Oracle. All rights reserved.
- * This program and the accompanying materials are 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.
+ * This program and the accompanying materials are 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:
* Oracle - initial API and implementation
@@ -527,7 +527,7 @@ public abstract class JavaRelationshipMapping extends JavaAttributeMapping
return buildAnnotationElementAdapter(annotationAdapter, elementName, StringExpressionConverter.instance());
}
- protected static DeclarationAnnotationElementAdapter buildAnnotationElementAdapter(DeclarationAnnotationAdapter annotationAdapter, String elementName, ExpressionConverter converter) {
+ protected static DeclarationAnnotationElementAdapter buildAnnotationElementAdapter(DeclarationAnnotationAdapter annotationAdapter, String elementName, ExpressionConverter<?, String> converter) {
return new ConversionDeclarationAnnotationElementAdapter(annotationAdapter, elementName, false, converter);
}
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/AbstractExpressionConverter.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/AbstractExpressionConverter.java
index f57178a958..f8054edfcd 100644
--- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/AbstractExpressionConverter.java
+++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/AbstractExpressionConverter.java
@@ -15,24 +15,28 @@ import org.eclipse.jpt.utility.internal.StringTools;
/**
* Gather together just the tiniest bit of common behavior.
+ * T1 is the expression type, while T2 is the type of object the expression
+ * is convert to/from.
*/
-public abstract class AbstractExpressionConverter implements ExpressionConverter {
+public abstract class AbstractExpressionConverter<T1 extends Expression, T2>
+ implements ExpressionConverter<T1, T2>
+{
protected AbstractExpressionConverter() {
super();
}
- public Expression convert(Object o, AST ast) {
- return (o == null) ? null : this.convert_(o, ast);
+ public T1 convert(T2 object, AST ast) {
+ return (object == null) ? null : this.convert_(object, ast);
}
/**
* The specified object is not null.
- * @see #convert(Object, AST)
+ * @see #convert(T, AST)
*/
- protected abstract Expression convert_(Object o, AST ast);
+ protected abstract T1 convert_(T2 object, AST ast);
- public Object convert(Expression expression) {
+ public T2 convert(T1 expression) {
return (expression == null) ? null : this.convert_(expression);
}
@@ -40,7 +44,7 @@ public abstract class AbstractExpressionConverter implements ExpressionConverter
* The specified expression is not null.
* @see #convert(Expression)
*/
- protected abstract Object convert_(Expression expression);
+ protected abstract T2 convert_(T1 expression);
@Override
public String toString() {
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/BooleanStringExpressionConverter.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/BooleanStringExpressionConverter.java
index ee3c394922..2b96c13df7 100644
--- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/BooleanStringExpressionConverter.java
+++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/BooleanStringExpressionConverter.java
@@ -12,19 +12,20 @@ package org.eclipse.jpt.core.internal.jdtutility;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.BooleanLiteral;
-import org.eclipse.jdt.core.dom.Expression;
/**
- * Convert an expression to/from a string representation of a boolean
+ * Convert a boolean literal to/from a string representation of a boolean
* (e.g. "true").
*/
-public final class BooleanStringExpressionConverter extends AbstractExpressionConverter {
- private static ExpressionConverter INSTANCE;
+public final class BooleanStringExpressionConverter
+ extends AbstractExpressionConverter<BooleanLiteral, String>
+{
+ private static ExpressionConverter<BooleanLiteral, String> INSTANCE;
/**
* Return the singleton.
*/
- public static ExpressionConverter instance() {
+ public static ExpressionConverter<BooleanLiteral, String> instance() {
if (INSTANCE == null) {
INSTANCE = new BooleanStringExpressionConverter();
}
@@ -32,21 +33,21 @@ public final class BooleanStringExpressionConverter extends AbstractExpressionCo
}
/**
- * Ensure non-instantiability.
+ * Ensure single instance.
*/
private BooleanStringExpressionConverter() {
super();
}
@Override
- protected Expression convert_(Object o, AST ast) {
- return ast.newBooleanLiteral(Boolean.valueOf((String) o).booleanValue());
+ protected BooleanLiteral convert_(String string, AST ast) {
+ return ast.newBooleanLiteral(Boolean.valueOf(string).booleanValue());
}
@Override
- protected Object convert_(Expression expression) {
- return (expression.getNodeType() == ASTNode.BOOLEAN_LITERAL) ?
- Boolean.toString(((BooleanLiteral) expression).booleanValue())
+ protected String convert_(BooleanLiteral booleanLiteral) {
+ return (booleanLiteral.getNodeType() == ASTNode.BOOLEAN_LITERAL) ?
+ Boolean.toString(booleanLiteral.booleanValue())
:
null;
}
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/CharacterStringExpressionConverter.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/CharacterStringExpressionConverter.java
index af430e73cf..bdbc5fbf18 100644
--- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/CharacterStringExpressionConverter.java
+++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/CharacterStringExpressionConverter.java
@@ -12,19 +12,20 @@ package org.eclipse.jpt.core.internal.jdtutility;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CharacterLiteral;
-import org.eclipse.jdt.core.dom.Expression;
/**
- * Convert an expression to/from a string representation of a character
+ * Convert a character literal to/from a string representation of a character
* (e.g. "A").
*/
-public final class CharacterStringExpressionConverter extends AbstractExpressionConverter {
- private static ExpressionConverter INSTANCE;
+public final class CharacterStringExpressionConverter
+ extends AbstractExpressionConverter<CharacterLiteral, String>
+{
+ private static ExpressionConverter<CharacterLiteral, String> INSTANCE;
/**
* Return the singleton.
*/
- public static ExpressionConverter instance() {
+ public static ExpressionConverter<CharacterLiteral, String> instance() {
if (INSTANCE == null) {
INSTANCE = new CharacterStringExpressionConverter();
}
@@ -32,23 +33,23 @@ public final class CharacterStringExpressionConverter extends AbstractExpression
}
/**
- * Ensure non-instantiability.
+ * Ensure single instance.
*/
private CharacterStringExpressionConverter() {
super();
}
@Override
- protected Expression convert_(Object o, AST ast) {
+ protected CharacterLiteral convert_(String string, AST ast) {
CharacterLiteral characterLiteral = ast.newCharacterLiteral();
- characterLiteral.setCharValue(((String) o).charAt(0));
+ characterLiteral.setCharValue(string.charAt(0));
return characterLiteral;
}
@Override
- protected Object convert_(Expression expression) {
- return (expression.getNodeType() == ASTNode.CHARACTER_LITERAL) ?
- Character.toString(((CharacterLiteral) expression).charValue())
+ protected String convert_(CharacterLiteral characterLiteral) {
+ return (characterLiteral.getNodeType() == ASTNode.CHARACTER_LITERAL) ?
+ Character.toString(characterLiteral.charValue())
:
null;
}
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/ExpressionConverter.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/ExpressionConverter.java
index c3d8081a46..e7a12876ce 100644
--- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/ExpressionConverter.java
+++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/ExpressionConverter.java
@@ -15,8 +15,10 @@ import org.eclipse.jdt.core.dom.Expression;
/**
* Define the protocol for converting an AST expression back and forth
* from an arbitrary type (e.g. Expression <=> String).
+ * T1 is the expression type, while T2 is the type of object the expression
+ * is convert to/from.
*/
-public interface ExpressionConverter {
+public interface ExpressionConverter<T1 extends Expression, T2> {
/**
* Convert the specified object to an
@@ -24,12 +26,12 @@ public interface ExpressionConverter {
* The type of the object is determined by the
* contract specified by the client.
*/
- Expression convert(Object o, AST ast);
+ T1 convert(T2 object, AST ast);
/**
* Convert the specified expression to an object of some
* pre-determined type.
*/
- Object convert(Expression expression);
+ T2 convert(T1 expression);
}
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NameStringExpressionConverter.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NameStringExpressionConverter.java
index 5a22d520a9..8e37284d94 100644
--- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NameStringExpressionConverter.java
+++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NameStringExpressionConverter.java
@@ -11,20 +11,21 @@ package org.eclipse.jpt.core.internal.jdtutility;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
-import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.Name;
/**
- * Convert an expression to/from a string representation of a name/identifier
+ * Convert a name to/from a string representation of a name/identifier
* (e.g. "com.xxx.Foo.VALUE1" or "value").
*/
-public final class NameStringExpressionConverter extends AbstractExpressionConverter {
- private static ExpressionConverter INSTANCE;
+public final class NameStringExpressionConverter
+ extends AbstractExpressionConverter<Name, String>
+{
+ private static ExpressionConverter<Name, String> INSTANCE;
/**
* Return the singleton.
*/
- public static ExpressionConverter instance() {
+ public static ExpressionConverter<Name, String> instance() {
if (INSTANCE == null) {
INSTANCE = new NameStringExpressionConverter();
}
@@ -32,23 +33,23 @@ public final class NameStringExpressionConverter extends AbstractExpressionConve
}
/**
- * Ensure non-instantiability.
+ * Ensure single instance.
*/
private NameStringExpressionConverter() {
super();
}
@Override
- protected Expression convert_(Object o, AST ast) {
- return ast.newName((String) o);
+ protected Name convert_(String string, AST ast) {
+ return ast.newName(string);
}
@Override
- protected Object convert_(Expression expression) {
- switch (expression.getNodeType()) {
+ protected String convert_(Name name) {
+ switch (name.getNodeType()) {
case ASTNode.QUALIFIED_NAME:
case ASTNode.SIMPLE_NAME:
- return ((Name) expression).getFullyQualifiedName();
+ return name.getFullyQualifiedName();
default:
return null;
}
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NullExpressionConverter.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NullExpressionConverter.java
index 0cb0561b05..e58f3af09b 100644
--- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NullExpressionConverter.java
+++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NullExpressionConverter.java
@@ -16,13 +16,17 @@ import org.eclipse.jpt.utility.internal.StringTools;
/**
* No conversion.
*/
-public final class NullExpressionConverter implements ExpressionConverter {
- private static ExpressionConverter INSTANCE;
+public final class NullExpressionConverter
+ implements ExpressionConverter<Expression, Expression>
+{
+
+ // singleton
+ private static ExpressionConverter<Expression, Expression> INSTANCE = new NullExpressionConverter();
/**
* Return the singleton.
*/
- public static ExpressionConverter instance() {
+ public static ExpressionConverter<Expression, Expression> instance() {
if (INSTANCE == null) {
INSTANCE = new NullExpressionConverter();
}
@@ -30,17 +34,17 @@ public final class NullExpressionConverter implements ExpressionConverter {
}
/**
- * Ensure non-instantiability.
+ * Ensure single instance.
*/
private NullExpressionConverter() {
super();
}
- public Expression convert(Object o, AST ast) {
- return (Expression) o;
+ public Expression convert(Expression expression, AST ast) {
+ return expression;
}
- public Object convert(Expression expression) {
+ public Expression convert(Expression expression) {
return expression;
}
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NumberStringExpressionConverter.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NumberStringExpressionConverter.java
index 83d8d3021c..a643c17c10 100644
--- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NumberStringExpressionConverter.java
+++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/NumberStringExpressionConverter.java
@@ -11,20 +11,21 @@ package org.eclipse.jpt.core.internal.jdtutility;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
-import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.NumberLiteral;
/**
- * Convert an expression to/from a string representation of a number
+ * Convert a number literal to/from a string representation of a number
* (e.g. "48").
*/
-public final class NumberStringExpressionConverter extends AbstractExpressionConverter {
- private static ExpressionConverter INSTANCE;
+public final class NumberStringExpressionConverter
+ extends AbstractExpressionConverter<NumberLiteral, String>
+{
+ private static ExpressionConverter<NumberLiteral, String> INSTANCE;
/**
* Return the singleton.
*/
- public static ExpressionConverter instance() {
+ public static ExpressionConverter<NumberLiteral, String> instance() {
if (INSTANCE == null) {
INSTANCE = new NumberStringExpressionConverter();
}
@@ -32,21 +33,21 @@ public final class NumberStringExpressionConverter extends AbstractExpressionCon
}
/**
- * Ensure non-instantiability.
+ * Ensure single instance.
*/
private NumberStringExpressionConverter() {
super();
}
@Override
- protected Expression convert_(Object o, AST ast) {
- return ast.newNumberLiteral((String) o);
+ protected NumberLiteral convert_(String string, AST ast) {
+ return ast.newNumberLiteral(string);
}
@Override
- protected Object convert_(Expression expression) {
- return (expression.getNodeType() == ASTNode.NUMBER_LITERAL) ?
- ((NumberLiteral) expression).getToken()
+ protected String convert_(NumberLiteral numberLiteral) {
+ return (numberLiteral.getNodeType() == ASTNode.NUMBER_LITERAL) ?
+ numberLiteral.getToken()
:
null;
}
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/PrimitiveTypeStringExpressionConverter.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/PrimitiveTypeStringExpressionConverter.java
index de71cdc752..3d1dfd521a 100644
--- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/PrimitiveTypeStringExpressionConverter.java
+++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/PrimitiveTypeStringExpressionConverter.java
@@ -11,21 +11,22 @@ package org.eclipse.jpt.core.internal.jdtutility;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
-import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.TypeLiteral;
/**
- * Convert an expression to/from a string representation of a primitive type
+ * Convert a type literal to/from a string representation of a primitive type
* (e.g. "int").
*/
-public final class PrimitiveTypeStringExpressionConverter extends AbstractExpressionConverter {
- private static ExpressionConverter INSTANCE;
+public final class PrimitiveTypeStringExpressionConverter
+ extends AbstractExpressionConverter<TypeLiteral, String>
+{
+ private static ExpressionConverter<TypeLiteral, String> INSTANCE;
/**
* Return the singleton.
*/
- public static ExpressionConverter instance() {
+ public static ExpressionConverter<TypeLiteral, String> instance() {
if (INSTANCE == null) {
INSTANCE = new PrimitiveTypeStringExpressionConverter();
}
@@ -33,24 +34,24 @@ public final class PrimitiveTypeStringExpressionConverter extends AbstractExpres
}
/**
- * Ensure non-instantiability.
+ * Ensure single instance.
*/
private PrimitiveTypeStringExpressionConverter() {
super();
}
@Override
- protected Expression convert_(Object o, AST ast) {
- org.eclipse.jdt.core.dom.Type type = ast.newPrimitiveType(PrimitiveType.toCode((String) o));
+ protected TypeLiteral convert_(String string, AST ast) {
+ org.eclipse.jdt.core.dom.Type type = ast.newPrimitiveType(PrimitiveType.toCode(string));
TypeLiteral typeLiteral = ast.newTypeLiteral();
typeLiteral.setType(type);
return typeLiteral;
}
@Override
- protected Object convert_(Expression expression) {
- if (expression.getNodeType() == ASTNode.TYPE_LITERAL) {
- org.eclipse.jdt.core.dom.Type type = ((TypeLiteral) expression).getType();
+ protected String convert_(TypeLiteral typeLiteral) {
+ if (typeLiteral.getNodeType() == ASTNode.TYPE_LITERAL) {
+ org.eclipse.jdt.core.dom.Type type = typeLiteral.getType();
if (type.getNodeType() == ASTNode.PRIMITIVE_TYPE) {
return ((PrimitiveType) type).getPrimitiveTypeCode().toString();
}
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/SimpleTypeStringExpressionConverter.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/SimpleTypeStringExpressionConverter.java
index f4122fa7fa..45747d9275 100644
--- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/SimpleTypeStringExpressionConverter.java
+++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/SimpleTypeStringExpressionConverter.java
@@ -11,22 +11,23 @@ package org.eclipse.jpt.core.internal.jdtutility;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
-import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.TypeLiteral;
/**
- * Convert an expression to/from a string representation of a simple type
+ * Convert a type literal to/from a string representation of a simple type
* (e.g. "java.lang.Object").
*/
-public final class SimpleTypeStringExpressionConverter extends AbstractExpressionConverter {
- private static ExpressionConverter INSTANCE;
+public final class SimpleTypeStringExpressionConverter
+ extends AbstractExpressionConverter<TypeLiteral, String>
+{
+ private static ExpressionConverter<TypeLiteral, String> INSTANCE;
/**
* Return the singleton.
*/
- public static ExpressionConverter instance() {
+ public static ExpressionConverter<TypeLiteral, String> instance() {
if (INSTANCE == null) {
INSTANCE = new SimpleTypeStringExpressionConverter();
}
@@ -34,15 +35,15 @@ public final class SimpleTypeStringExpressionConverter extends AbstractExpressio
}
/**
- * Ensure non-instantiability.
+ * Ensure single instance.
*/
private SimpleTypeStringExpressionConverter() {
super();
}
@Override
- protected Expression convert_(Object o, AST ast) {
- Name name = ast.newName((String) o);
+ protected TypeLiteral convert_(String string, AST ast) {
+ Name name = ast.newName(string);
org.eclipse.jdt.core.dom.Type type = ast.newSimpleType(name);
TypeLiteral typeLiteral = ast.newTypeLiteral();
typeLiteral.setType(type);
@@ -50,9 +51,9 @@ public final class SimpleTypeStringExpressionConverter extends AbstractExpressio
}
@Override
- protected Object convert_(Expression expression) {
- if (expression.getNodeType() == ASTNode.TYPE_LITERAL) {
- org.eclipse.jdt.core.dom.Type type = ((TypeLiteral) expression).getType();
+ protected String convert_(TypeLiteral typeLiteral) {
+ if (typeLiteral.getNodeType() == ASTNode.TYPE_LITERAL) {
+ org.eclipse.jdt.core.dom.Type type = typeLiteral.getType();
if (type.getNodeType() == ASTNode.SIMPLE_TYPE) {
return ((SimpleType) type).getName().getFullyQualifiedName();
}
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/StringArrayExpressionConverter.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/StringArrayExpressionConverter.java
new file mode 100644
index 0000000000..cf653cd2e3
--- /dev/null
+++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/StringArrayExpressionConverter.java
@@ -0,0 +1,59 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Oracle. All rights reserved.
+ * This program and the accompanying materials are 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.core.internal.jdtutility;
+
+import java.util.List;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.ArrayInitializer;
+import org.eclipse.jdt.core.dom.Expression;
+import org.eclipse.jdt.core.dom.StringLiteral;
+
+/**
+ * Convert an array initializer to/from an array of strings (e.g. {"text0", "text1"}).
+ * E is the type of the expressions to be found in the array initializer.
+ */
+public class StringArrayExpressionConverter<E extends Expression>
+ extends AbstractExpressionConverter<ArrayInitializer, String[]>
+{
+ private final ExpressionConverter<E, String> elementConverter;
+
+ public StringArrayExpressionConverter(ExpressionConverter<E, String> elementConverter) {
+ super();
+ this.elementConverter = elementConverter;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ protected ArrayInitializer convert_(String[] strings, AST ast) {
+ ArrayInitializer arrayInitializer = ast.newArrayInitializer();
+ List<Expression> expressions = arrayInitializer.expressions();
+ for (String string : strings) {
+ expressions.add(this.elementConverter.convert(string, ast));
+ }
+ return arrayInitializer;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ protected String[] convert_(ArrayInitializer arrayInitializer) {
+ List<E> expressions = arrayInitializer.expressions();
+ int len = expressions.size();
+ String[] strings = new String[len];
+ for (int i = len; i-- > 0; ) {
+ strings[i] = this.elementConverter.convert(expressions.get(i));
+ }
+ return strings;
+ }
+
+ public static StringArrayExpressionConverter<StringLiteral> forStringLiterals() {
+ return new StringArrayExpressionConverter<StringLiteral>(StringExpressionConverter.instance());
+ }
+
+}
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/StringExpressionConverter.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/StringExpressionConverter.java
index a6202707a8..b9000089ce 100644
--- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/StringExpressionConverter.java
+++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/jdtutility/StringExpressionConverter.java
@@ -11,19 +11,20 @@ package org.eclipse.jpt.core.internal.jdtutility;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
-import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.StringLiteral;
/**
- * Convert an expression to/from a string (e.g. "text").
+ * Convert a string literal to/from a string (e.g. "text").
*/
-public final class StringExpressionConverter extends AbstractExpressionConverter {
- private static ExpressionConverter INSTANCE;
+public final class StringExpressionConverter
+ extends AbstractExpressionConverter<StringLiteral, String>
+{
+ private static ExpressionConverter<StringLiteral, String> INSTANCE;
/**
* Return the singleton.
*/
- public static ExpressionConverter instance() {
+ public static ExpressionConverter<StringLiteral, String> instance() {
if (INSTANCE == null) {
INSTANCE = new StringExpressionConverter();
}
@@ -31,23 +32,23 @@ public final class StringExpressionConverter extends AbstractExpressionConverter
}
/**
- * Ensure non-instantiability.
+ * Ensure single instance.
*/
private StringExpressionConverter() {
super();
}
@Override
- protected Expression convert_(Object o, AST ast) {
+ protected StringLiteral convert_(String string, AST ast) {
StringLiteral stringLiteral = ast.newStringLiteral();
- stringLiteral.setLiteralValue((String) o);
+ stringLiteral.setLiteralValue(string);
return stringLiteral;
}
@Override
- protected Object convert_(Expression expression) {
- return (expression.getNodeType() == ASTNode.STRING_LITERAL) ?
- ((StringLiteral) expression).getLiteralValue()
+ protected String convert_(StringLiteral stringLiteral) {
+ return (stringLiteral.getNodeType() == ASTNode.STRING_LITERAL) ?
+ stringLiteral.getLiteralValue()
:
null;
}
diff --git a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/MemberAnnotationElementAdapterTests.java b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/MemberAnnotationElementAdapterTests.java
index 90ca36671b..d420344911 100644
--- a/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/MemberAnnotationElementAdapterTests.java
+++ b/jpa/tests/org.eclipse.jpt.core.tests/src/org/eclipse/jpt/core/tests/internal/jdtutility/MemberAnnotationElementAdapterTests.java
@@ -9,6 +9,8 @@
******************************************************************************/
package org.eclipse.jpt.core.tests.internal.jdtutility;
+import java.util.Arrays;
+import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jpt.core.internal.ITextRange;
import org.eclipse.jpt.core.internal.jdtutility.ASTNodeTextRange;
import org.eclipse.jpt.core.internal.jdtutility.AnnotationElementAdapter;
@@ -25,6 +27,8 @@ import org.eclipse.jpt.core.internal.jdtutility.NumberStringExpressionConverter;
import org.eclipse.jpt.core.internal.jdtutility.PrimitiveTypeStringExpressionConverter;
import org.eclipse.jpt.core.internal.jdtutility.SimpleDeclarationAnnotationAdapter;
import org.eclipse.jpt.core.internal.jdtutility.SimpleTypeStringExpressionConverter;
+import org.eclipse.jpt.core.internal.jdtutility.StringArrayExpressionConverter;
+import org.eclipse.jpt.core.internal.jdtutility.StringExpressionConverter;
public class MemberAnnotationElementAdapterTests extends AnnotationTestCase {
@@ -539,4 +543,34 @@ public class MemberAnnotationElementAdapterTests extends AnnotationTestCase {
this.assertSourceContains(annotation);
}
+ public void testGetValueStringArray() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String[] bar();");
+ this.createTestType("@annot.Foo(bar={\"string0\", \"string1\"})");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", new StringArrayExpressionConverter<StringLiteral>(StringExpressionConverter.instance()));
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertTrue(Arrays.equals(new String[] {"string0", "string1"}, (String[]) aea.getValue()));
+ }
+
+ public void testGetValueNullStringArray() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String[] bar();");
+ this.createTestType("@annot.Foo()");
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", new StringArrayExpressionConverter<StringLiteral>(StringExpressionConverter.instance()));
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ assertNull(aea.getValue());
+ }
+
+ public void testSetValueStringArray() throws Exception {
+ this.createAnnotationAndMembers("Foo", "String[] bar();");
+ String annotation = "@Foo(bar={\"string0\",\"string1\"})";
+ this.createTestType();
+ this.assertSourceDoesNotContain(annotation);
+ DeclarationAnnotationAdapter daa = new SimpleDeclarationAnnotationAdapter("annot.Foo");
+ DeclarationAnnotationElementAdapter daea = new ConversionDeclarationAnnotationElementAdapter(daa, "bar", new StringArrayExpressionConverter<StringLiteral>(StringExpressionConverter.instance()));
+ AnnotationElementAdapter aea = new MemberAnnotationElementAdapter(this.idField(), daea);
+ aea.setValue(new String[] {"string0", "string1"});
+ this.assertSourceContains(annotation);
+ }
+
}

Back to the top