/******************************************************************************* * Copyright (c) 2006, 2010 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.ASTNode; import org.eclipse.jdt.core.dom.Expression; 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.ExpressionConverter; import org.eclipse.jpt.common.core.utility.jdt.ModifiedDeclaration; import org.eclipse.jpt.common.utility.internal.StringTools; /** * Wrap a declaration annotation element adapter that deals with AST * expressions, converting them to/from various other objects. * * @param the type of the object be passed to and returned by the adapter */ public class ConversionDeclarationAnnotationElementAdapter implements DeclarationAnnotationElementAdapter { /** * The wrapped adapter that returns and takes AST expressions. */ private final DeclarationAnnotationElementAdapter adapter; /** * The converter that converts AST expressions to other objects * (e.g. Strings). */ private final ExpressionConverter converter; // ********** constructors ********** /** * The default element name is value. */ public ConversionDeclarationAnnotationElementAdapter(DeclarationAnnotationAdapter annotationAdapter, ExpressionConverter converter) { this(new ExpressionDeclarationAnnotationElementAdapter(annotationAdapter), converter); } public ConversionDeclarationAnnotationElementAdapter(DeclarationAnnotationAdapter annotationAdapter, String elementName, ExpressionConverter converter) { this(new ExpressionDeclarationAnnotationElementAdapter(annotationAdapter, elementName), converter); } public ConversionDeclarationAnnotationElementAdapter(DeclarationAnnotationElementAdapter adapter, ExpressionConverter converter) { super(); this.adapter = adapter; this.converter = converter; } // ********** DeclarationAnnotationElementAdapter implementation ********** public T getValue(ModifiedDeclaration declaration) { Expression expression = this.adapter.getValue(declaration); return this.converter.convert(expression); } public void setValue(T value, ModifiedDeclaration declaration) { Expression expression; try { expression = this.converter.convert(value, declaration.getAst()); } catch (IllegalArgumentException ex) { // if there is a problem converting the 'value' to an Expression we get this exception return; // don't set the value if it is "illegal" } this.adapter.setValue(expression, declaration); } public Expression getExpression(ModifiedDeclaration declaration) { return this.adapter.getExpression(declaration); } public ASTNode getAstNode(ModifiedDeclaration declaration) { return this.adapter.getAstNode(declaration); } @Override public String toString() { return StringTools.buildToStringFor(this, this.adapter); } // ********** factory static methods ********** /** * The default element name is value; * the default expression converter expects string constant expressions. */ public static ConversionDeclarationAnnotationElementAdapter forStrings(DeclarationAnnotationAdapter annotationAdapter) { return new ConversionDeclarationAnnotationElementAdapter(annotationAdapter, StringExpressionConverter.instance()); } /** * The default expression converter expects string constant expressions. */ public static ConversionDeclarationAnnotationElementAdapter forStrings(DeclarationAnnotationAdapter annotationAdapter, String elementName) { return new ConversionDeclarationAnnotationElementAdapter(annotationAdapter, elementName, StringExpressionConverter.instance()); } /** * The default element name is value; * the default expression converter expects number constant expressions. */ public static ConversionDeclarationAnnotationElementAdapter forNumbers(DeclarationAnnotationAdapter annotationAdapter) { return new ConversionDeclarationAnnotationElementAdapter(annotationAdapter, NumberIntegerExpressionConverter.instance()); } /** * The default expression converter expects number constant expressions. */ public static ConversionDeclarationAnnotationElementAdapter forNumbers(DeclarationAnnotationAdapter annotationAdapter, String elementName) { return new ConversionDeclarationAnnotationElementAdapter(annotationAdapter, elementName, NumberIntegerExpressionConverter.instance()); } /** * The default element name is value; * the default expression converter expects boolean constant expressions. */ public static ConversionDeclarationAnnotationElementAdapter forBooleans(DeclarationAnnotationAdapter annotationAdapter) { return new ConversionDeclarationAnnotationElementAdapter(annotationAdapter, BooleanExpressionConverter.instance()); } /** * The default expression converter expects boolean constant expressions. */ public static ConversionDeclarationAnnotationElementAdapter forBooleans(DeclarationAnnotationAdapter annotationAdapter, String elementName) { return new ConversionDeclarationAnnotationElementAdapter(annotationAdapter, elementName, BooleanExpressionConverter.instance()); } /** * The default element name is value; * the default expression converter expects character constant expressions. */ public static ConversionDeclarationAnnotationElementAdapter forCharacters(DeclarationAnnotationAdapter annotationAdapter) { return new ConversionDeclarationAnnotationElementAdapter(annotationAdapter, CharacterStringExpressionConverter.instance()); } /** * The default expression converter expects character constant expressions. */ public static ConversionDeclarationAnnotationElementAdapter forCharacters(DeclarationAnnotationAdapter annotationAdapter, String elementName) { return new ConversionDeclarationAnnotationElementAdapter(annotationAdapter, elementName, CharacterStringExpressionConverter.instance()); } }