diff options
Diffstat (limited to 'common')
7 files changed, 168 insertions, 10 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/resource/java/source/SourceAnnotation.java b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/resource/java/source/SourceAnnotation.java index ef6fa97f44..cc7f47e155 100644 --- a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/resource/java/source/SourceAnnotation.java +++ b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/resource/java/source/SourceAnnotation.java @@ -26,6 +26,7 @@ import org.eclipse.jpt.common.core.utility.jdt.AnnotationElementAdapter; import org.eclipse.jpt.common.core.utility.jdt.DeclarationAnnotationAdapter; import org.eclipse.jpt.common.core.utility.jdt.DeclarationAnnotationElementAdapter; import org.eclipse.jpt.common.core.utility.jdt.IndexedAnnotationAdapter; +import org.eclipse.jpt.common.core.utility.jdt.IndexedDeclarationAnnotationElementAdapter; /** * some common state and behavior for Java source annotations; @@ -176,6 +177,24 @@ public abstract class SourceAnnotation protected Expression getAnnotationElementExpression(DeclarationAnnotationElementAdapter<?> adapter, CompilationUnit astRoot) { return adapter.getExpression(this.annotatedElement.getModifiedDeclaration(astRoot)); } + + /** + * Return the text range corresponding to the element's indexed subvalue + * @throws ArrayIndexOutOfBoundsException if the index is out of range + */ + protected TextRange getAnnotationElementSubvalueTextRange(IndexedDeclarationAnnotationElementAdapter<?> adapter, int index, CompilationUnit astRoot) { + // the AST is null for virtual Java attributes + // TODO remove the AST null check once we start storing text ranges + // in the resource model + return (astRoot == null) ? null : this.getTextRange(this.getAnnotationElementSubvalueExpression(adapter, index, astRoot)); + } + + /** + * Return the expression corresponding to the element's indexed subvalue. + */ + protected Expression getAnnotationElementSubvalueExpression(IndexedDeclarationAnnotationElementAdapter<?> adapter, int index, CompilationUnit astRoot) { + return adapter.getSubvalueExpression(index, this.annotatedElement.getModifiedDeclaration(astRoot)); + } /** * Return the text range corresponding to the specified AST node. diff --git a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/AnnotationStringArrayExpressionConverter.java b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/AnnotationStringArrayExpressionConverter.java index bd085ef4ff..e67ac839c2 100644 --- a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/AnnotationStringArrayExpressionConverter.java +++ b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/AnnotationStringArrayExpressionConverter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 Oracle. All rights reserved. + * Copyright (c) 2007, 2011 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. @@ -14,6 +14,7 @@ import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.ArrayInitializer; import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jpt.common.core.utility.jdt.ExpressionConverter; +import org.eclipse.jpt.common.core.utility.jdt.IndexedExpressionConverter; /** * Convert an array initializer or single expression to/from an array of @@ -22,8 +23,9 @@ import org.eclipse.jpt.common.core.utility.jdt.ExpressionConverter; * as elements in the array initializer. */ public class AnnotationStringArrayExpressionConverter - extends AbstractExpressionConverter<String[]> -{ + extends AbstractExpressionConverter<String[]> + implements IndexedExpressionConverter<String> { + private final ExpressionConverter<String> elementConverter; private final StringArrayExpressionConverter arrayConverter; @@ -69,6 +71,16 @@ public class AnnotationStringArrayExpressionConverter : new String[] {this.elementConverter.convert(expression)}; } + + public Expression getSubexpression(int index, Expression expression) { + if (expression.getNodeType() == ASTNode.ARRAY_INITIALIZER) { + return this.arrayConverter.getSubexpression(index, expression); + } + if (index > 0) { + throw new ArrayIndexOutOfBoundsException(); + } + return expression; + } // ********** factory methods ********** diff --git a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/ConversionDeclarationAnnotationElementAdapter.java b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/ConversionDeclarationAnnotationElementAdapter.java index b65a2fec5f..1185f5233b 100644 --- a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/ConversionDeclarationAnnotationElementAdapter.java +++ b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/ConversionDeclarationAnnotationElementAdapter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2010 Oracle. All rights reserved. + * Copyright (c) 2006, 2011 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. @@ -56,8 +56,13 @@ public class ConversionDeclarationAnnotationElementAdapter<T> this.adapter = adapter; this.converter = converter; } - - + + + protected ExpressionConverter<T> getConverter() { + return this.converter; + } + + // ********** DeclarationAnnotationElementAdapter implementation ********** public T getValue(ModifiedDeclaration declaration) { diff --git a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/IndexedConversionDeclarationAnnotationElementAdapter.java b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/IndexedConversionDeclarationAnnotationElementAdapter.java new file mode 100644 index 0000000000..7045a05bf6 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/IndexedConversionDeclarationAnnotationElementAdapter.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2011 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.common.core.internal.utility.jdt; + +import org.eclipse.jdt.core.dom.Expression; +import org.eclipse.jpt.common.core.utility.jdt.DeclarationAnnotationAdapter; +import org.eclipse.jpt.common.core.utility.jdt.IndexedDeclarationAnnotationElementAdapter; +import org.eclipse.jpt.common.core.utility.jdt.IndexedExpressionConverter; +import org.eclipse.jpt.common.core.utility.jdt.ModifiedDeclaration; + + +public class IndexedConversionDeclarationAnnotationElementAdapter<T> + extends ConversionDeclarationAnnotationElementAdapter<T[]> + implements IndexedDeclarationAnnotationElementAdapter<T> { + + public IndexedConversionDeclarationAnnotationElementAdapter( + DeclarationAnnotationAdapter annotationAdapter, IndexedExpressionConverter<T> converter) { + super(annotationAdapter, converter); + } + + public IndexedConversionDeclarationAnnotationElementAdapter( + DeclarationAnnotationAdapter annotationAdapter, String elementName, IndexedExpressionConverter<T> converter) { + super(annotationAdapter, elementName, converter); + } + + + @Override + protected IndexedExpressionConverter<T> getConverter() { + return (IndexedExpressionConverter<T>) super.getConverter(); + } + + + public Expression getSubvalueExpression(int index, ModifiedDeclaration declaration) { + return getConverter().getSubexpression(index, super.getExpression(declaration)); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/StringArrayExpressionConverter.java b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/StringArrayExpressionConverter.java index 9ee7e60872..91ee6cfa24 100644 --- a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/StringArrayExpressionConverter.java +++ b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/StringArrayExpressionConverter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2008 Oracle. All rights reserved. + * Copyright (c) 2007, 2011 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. @@ -15,6 +15,7 @@ import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.ArrayInitializer; import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jpt.common.core.utility.jdt.ExpressionConverter; +import org.eclipse.jpt.common.core.utility.jdt.IndexedExpressionConverter; import org.eclipse.jpt.common.utility.internal.StringTools; /** @@ -30,8 +31,9 @@ import org.eclipse.jpt.common.utility.internal.StringTools; * the appropriate index. */ public class StringArrayExpressionConverter - extends AbstractExpressionConverter<String[]> -{ + extends AbstractExpressionConverter<String[]> + implements IndexedExpressionConverter<String> { + private final ExpressionConverter<String> elementConverter; private final boolean removeArrayInitializerWhenEmpty; @@ -100,5 +102,11 @@ public class StringArrayExpressionConverter } return strings; } - + + public Expression getSubexpression(int index, Expression expression) { + if (expression.getNodeType() == ASTNode.ARRAY_INITIALIZER) { + return expressions((ArrayInitializer) expression).get(index); + } + throw new ArrayIndexOutOfBoundsException(); + } } diff --git a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/utility/jdt/IndexedDeclarationAnnotationElementAdapter.java b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/utility/jdt/IndexedDeclarationAnnotationElementAdapter.java new file mode 100644 index 0000000000..ae9f83ada8 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/utility/jdt/IndexedDeclarationAnnotationElementAdapter.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2011 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.common.core.utility.jdt; + +import org.eclipse.jdt.core.dom.Expression; + +/** + * Provide clients with a pluggable way to manipulate an <i>indexed</i> annotation + * element modifying a "declaration". + * T[] is the type of the object to be passed to and returned by the adapter. + * T is the sub-value of the value object. + * + * Provisional API: This interface is part of an interim API that is still + * under development and expected to change significantly before reaching + * stability. It is available at this early stage to solicit feedback from + * pioneering adopters on the understanding that any code that uses this API + * will almost certainly be broken (repeatedly) as the API evolves. + * + * This interface is not intended to be implemented by clients. + */ +public interface IndexedDeclarationAnnotationElementAdapter<T> + extends DeclarationAnnotationElementAdapter<T[]> { + + /** + * Given the specified declaration, return the expression for the element's subvalue + * at the given index. + * @throws IndexOutOfBoundsException if the index is out of range + */ + Expression getSubvalueExpression(int index, ModifiedDeclaration declaration); +} diff --git a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/utility/jdt/IndexedExpressionConverter.java b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/utility/jdt/IndexedExpressionConverter.java new file mode 100644 index 0000000000..a25ab674fb --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/utility/jdt/IndexedExpressionConverter.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright (c) 2011 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.common.core.utility.jdt; + +import org.eclipse.jdt.core.dom.Expression; + +/** + * A type of {@link ExpressionConverter} that can further retrieve sub-expressions + * based on indices. + * + * Provisional API: This interface is part of an interim API that is still + * under development and expected to change significantly before reaching + * stability. It is available at this early stage to solicit feedback from + * pioneering adopters on the understanding that any code that uses this API + * will almost certainly be broken (repeatedly) as the API evolves. + * + * This interface is not intended to be implemented by clients. + */ +public interface IndexedExpressionConverter<T> + extends ExpressionConverter<T[]> { + + /** + * Return the expression for the sub-value at the given index. + * @throws ArrayIndexOutOfBoundsException if the index is out of range + */ + Expression getSubexpression(int index, Expression expression); +} |