Skip to content

Commit

Permalink
TEIIDDES-1518: Replaces instanceof calls
Browse files Browse the repository at this point in the history
* The heirarchy of the language objects in 7.7 is not consistent with
  8.*.

* For example, MultiElementSymbol implements IMultiElementSymbol which
  in turn extends IExpression. However, 7.7 version does not implement
  Expression so replacement of instanceof checks with an isExpression()
  method allows the flexibility to keep the spi interface hierarchy and
  return the correct answer.
  • Loading branch information
Paul Richardson committed Dec 11, 2012
1 parent b767264 commit 9f36140
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static ILanguageObject getBuilderLanguageObject( ILanguageObject theLangO

ILanguageObject result = theLangObj;

if ((theLangObj instanceof IFunction) && ((IFunction)theLangObj).isImplicit()) {
if ((theLangObj.isFunction()) && ((IFunction)theLangObj).isImplicit()) {
// according to Alex, all implicit functions are conversions and
// the first argument is what is being converted
result = getBuilderLanguageObject(((IFunction)theLangObj).getArgs()[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.teiid.designer.query.sql.lang.INotCriteria;
import org.teiid.designer.query.sql.lang.IPredicateCriteria;
import org.teiid.designer.query.sql.symbol.IConstant;
import org.teiid.designer.query.sql.symbol.IFunction;
import org.teiid.designer.query.sql.symbol.IReference;
import org.teiid.query.ui.UiConstants;
import org.teiid.query.ui.UiPlugin;
Expand Down Expand Up @@ -80,7 +79,7 @@ public Image getImage( Object theElement ) {

if (theElement instanceof IConstant) {
result = CONSTANT_IMAGE;
} else if (theElement instanceof IFunction) {
} else if (theElement instanceof ILanguageObject && ((ILanguageObject) theElement).isFunction()) {
result = FUNCTION_IMAGE;
} else if (theElement instanceof IPredicateCriteria) {
result = PREDICATE_IMAGE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public boolean isInExpression() {
public DisplayNode getExpression() {
DisplayNode parentNode = this;
while (parentNode != null) {
if (parentNode.languageObject instanceof IExpression) {
if (parentNode.languageObject.isExpression()) {
return parentNode;
}
parentNode = parentNode.getParent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ static DisplayNode constructDisplayNode( DisplayNode parentNode,
} else if (obj instanceof ICompoundCriteria) {
CompoundCriteriaDisplayNode node = new CompoundCriteriaDisplayNode(parentNode, (ICompoundCriteria)obj);
return node;
} else if (obj instanceof IFunction) {
} else if (obj instanceof ILanguageObject && ((ILanguageObject) obj).isFunction()) {
// ---------------------------------------------------------------------
// Constant, Function, Expression Nodes
// ---------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static boolean hasSymbol( DisplayNode node ) {
* @return true if the node has at least one expression, false if not.
*/
public static boolean hasExpression( DisplayNode node ) {
if (node.languageObject instanceof IExpression) {
if (node.languageObject.isExpression()) {
return true;
}
List nodes = node.getDisplayNodeList();
Expand Down Expand Up @@ -289,7 +289,7 @@ public static int getEndIndexOfPreviousSymbol( DisplayNode node,
*/
public static int getStartIndexOfNextExpression( DisplayNode node,
int index ) {
if (node.languageObject instanceof IExpression) {
if (node.languageObject.isExpression()) {
if (node.isAnywhereWithin(index)) {
return node.getStartIndex();
}
Expand Down Expand Up @@ -319,7 +319,7 @@ public static int getStartIndexOfNextExpression( DisplayNode node,
public static int getEndIndexOfPreviousExpression( DisplayNode node,
int index ) {
int prevEnd = -1;
if (node.languageObject instanceof IExpression) {
if (node.languageObject.isExpression()) {
if (node.isAnywhereWithin(index)) {
return node.getEndIndex() + 1;
}
Expand Down Expand Up @@ -889,7 +889,7 @@ public static DisplayNode getExpressionForNode( DisplayNode node ) {
return node.getExpression();
}
return null;
} else if (node.languageObject instanceof IExpression && !(node.languageObject instanceof IScalarSubquery)) {
} else if (node.languageObject.isExpression() && !(node.languageObject instanceof IScalarSubquery)) {
return node;
} else if (node.isInExpression()) {
return node.getExpression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;
import org.teiid.designer.query.sql.lang.ICriteria;
import org.teiid.designer.query.sql.lang.IExpression;
import org.teiid.designer.query.sql.lang.IFrom;
import org.teiid.designer.query.sql.lang.IJoinPredicate;
import org.teiid.designer.query.sql.lang.ILanguageObject;
Expand Down Expand Up @@ -104,7 +103,7 @@ public Image getImage(Object element) {
return FROM_ICON;
} else if ( element instanceof IJoinPredicate ) {
return JOIN_ICON;
} else if ( element instanceof IExpression ) {
} else if ( ((ILanguageObject) element).isExpression() ) {
if ( element instanceof IExpressionSymbol) {
return EXPRESSION_ICON;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,15 @@ public interface ILanguageObject {
* @param visitor
*/
void acceptVisitor(ILanguageVisitor visitor);

/**
* Is this object a function
*/
boolean isFunction();

/**
* Is this object an expression
*/
boolean isExpression();

}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private boolean canDelete( Object theLangObj ) {
if (parent == null) {
// object is root. can only delete if not undefined
result = !isUndefined(theLangObj);
} else if (parent instanceof IFunction) {
} else if (parent.isFunction()) {
// all function arguments except conversion type constants can be deleted
if (theLangObj instanceof IConstant) {
result = !BuilderUtils.isConversionType((IConstant)theLangObj);
Expand Down Expand Up @@ -178,7 +178,7 @@ private void delete( Object theLangObj ) {
refresh(true);
newSelection = contentProvider.getRoot();
expandToLevel(newSelection, ALL_LEVELS);
} else if (parent instanceof IFunction) {
} else if (parent.isFunction()) {
// set the arg to null in parent
int index = contentProvider.getChildIndex(theLangObj);

Expand Down Expand Up @@ -348,14 +348,14 @@ private void modifyLanguageObject( Object theObject,
newSelection = contentProvider.getRoot();
expandToLevel(newSelection, ALL_LEVELS);

if (newSelection instanceof IFunction) {
if (newSelection instanceof ILanguageObject && ((ILanguageObject) newSelection).isFunction()) {
IFunction function = (IFunction)newSelection;

if (function.getArgs().length > 0) {
newSelection = contentProvider.getChildAt(0, function);
}
}
} else if (parent instanceof IFunction) {
} else if (parent.isFunction()) {
// set the arg to new value in parent
int index = contentProvider.getChildIndex(theObject);

Expand All @@ -368,7 +368,7 @@ private void modifyLanguageObject( Object theObject,
newSelection = contentProvider.getChildAt(index, parent);

if (!retainSelection) {
if (newSelection instanceof IFunction) {
if (newSelection instanceof ILanguageObject && ((ILanguageObject) newSelection).isFunction()) {
// if function arg change to be a function, select first function arg
if (contentProvider.getChildCount(newSelection) > 0) {
newSelection = contentProvider.getChildAt(0, newSelection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public void setLanguageObject( ILanguageObject theLanguageObject ) {
if (theLanguageObject == null) {
clear();
} else {
if (!(theLanguageObject instanceof IFunction)) {
CoreArgCheck.isTrue((theLanguageObject instanceof IFunction),
if (!(theLanguageObject.isFunction())) {
CoreArgCheck.isTrue((theLanguageObject.isFunction()),
Util.getString(PREFIX + "invalidLanguageObject", //$NON-NLS-1$
new Object[] {theLanguageObject.getClass().getName()}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ public void setLanguageObject( ILanguageObject theLanguageObject ) {
if (theLanguageObject == null) {
clear();
} else {
if (!(theLanguageObject instanceof IFunction)) {
CoreArgCheck.isTrue((theLanguageObject instanceof IFunction),
if (!(theLanguageObject.isFunction())) {
CoreArgCheck.isTrue((theLanguageObject.isFunction()),
Util.getString(PREFIX + "invalidLanguageObject", //$NON-NLS-1$
new Object[] {theLanguageObject.getClass().getName()}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.teiid.core.designer.util.I18nUtil;
import org.teiid.designer.query.sql.lang.ILanguageObject;
import org.teiid.designer.query.sql.symbol.IConstant;
import org.teiid.designer.query.sql.symbol.IElementSymbol;
import org.teiid.designer.query.sql.symbol.IFunction;
import org.teiid.designer.query.sql.symbol.IReference;
import org.teiid.designer.transformation.ui.UiConstants;
import org.teiid.designer.transformation.ui.builder.ILanguageObjectEditor;
Expand Down Expand Up @@ -127,7 +127,7 @@ private boolean isElement() {
}

private boolean isFunction() {
return (langObj == null) ? false : (langObj instanceof IFunction);
return (langObj == null) ? false : (langObj instanceof ILanguageObject && ((ILanguageObject) langObj).isFunction());
}

private boolean isReference() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private Object[] getChildren( ICriteria theCriteria ) {
private Object[] getChildren( IExpression theExpression ) {
Object[] result = null;

if (theExpression instanceof IFunction) {
if (theExpression.isFunction()) {
result = getChildren((IFunction)theExpression);
}

Expand All @@ -128,7 +128,7 @@ private Object[] getChildren( IFunction theFunction ) {
// according to Alex, all implicit functions are conversions and
// the first argument is what is being converted (which could be a function or expression)
IExpression arg = theFunction.getArgs()[0];
result = (arg instanceof IFunction) ? getChildren((IFunction)arg) : getChildren(arg);
result = (arg.isFunction()) ? getChildren((IFunction)arg) : getChildren(arg);
} else {
IExpression[] args = theFunction.getArgs();

Expand All @@ -155,7 +155,7 @@ public Object[] getChildren( Object theParent ) {
if (theParent != null) {
if (theParent instanceof ICriteria) {
result = getChildren((ICriteria)theParent);
} else if (theParent instanceof IExpression) {
} else if (theParent instanceof ILanguageObject && ((ILanguageObject) theParent).isExpression()) {
result = getChildren((IExpression)theParent);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.teiid.designer.metamodels.transformation.MappingClass;
import org.teiid.designer.query.IQueryService;
import org.teiid.designer.query.sql.lang.IExpression;
import org.teiid.designer.query.sql.lang.ILanguageObject;
import org.teiid.designer.query.sql.symbol.IAliasSymbol;
import org.teiid.designer.transformation.ui.UiConstants;
import org.teiid.designer.transformation.util.TransformationHelper;
Expand Down Expand Up @@ -187,7 +188,7 @@ public void handleEvent( Event event ) {
label.setData("_TABLEITEM", item); //$NON-NLS-1$
Object data = item.getData();
String tipText = null;
if (data != null && data instanceof IExpression) {
if (data instanceof ILanguageObject && ((ILanguageObject) data).isExpression()) {
IQueryService queryService = ModelerCore.getTeiidQueryService();
tipText = queryService.getSymbolName((IExpression)data);
}
Expand Down Expand Up @@ -277,7 +278,7 @@ public String getColumnText( Object theElement,
}
}
// SingleElementSymbol
} else if (theElement instanceof IExpression) {
} else if (theElement instanceof ILanguageObject && ((ILanguageObject) theElement).isExpression()) {
EObject singleElementEObject = TransformationSqlHelper.getSingleElementSymbolEObject((IExpression)theElement);
if (singleElementEObject != null) {
columnText = getAppendedPath(singleElementEObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.teiid.designer.query.IQueryFactory;
import org.teiid.designer.query.IQueryService;
import org.teiid.designer.query.sql.lang.IExpression;
import org.teiid.designer.query.sql.lang.ILanguageObject;
import org.teiid.designer.query.sql.symbol.IAliasSymbol;
import org.teiid.designer.query.sql.symbol.IElementSymbol;
import org.teiid.designer.query.sql.symbol.IExpressionSymbol;
Expand Down Expand Up @@ -464,7 +465,7 @@ public String getSqlSymbolText( boolean showType ) {
if (sqlSymbol instanceof IAliasSymbol) {
sqlSymbol = ((IAliasSymbol)sqlSymbol).getSymbol();
}
if (sqlSymbol != null && sqlSymbol instanceof IExpression) {
if (sqlSymbol instanceof ILanguageObject && ((ILanguageObject) sqlSymbol).isExpression()) {
String symbolName = TransformationSqlHelper.getSingleElementSymbolShortName((IExpression)sqlSymbol, true);
// show aliased if necessary
if (!isInputParamBinding()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.graphics.Image;
import org.teiid.designer.query.sql.lang.IExpression;
import org.teiid.designer.query.sql.lang.ILanguageObject;
import org.teiid.designer.query.sql.symbol.IAggregateSymbol;
import org.teiid.designer.query.sql.symbol.IAliasSymbol;
import org.teiid.designer.query.sql.symbol.IConstant;
import org.teiid.designer.query.sql.symbol.IElementSymbol;
import org.teiid.designer.query.sql.symbol.IExpressionSymbol;
import org.teiid.designer.query.sql.symbol.IFunction;
import org.teiid.designer.transformation.ui.PluginConstants;
import org.teiid.designer.transformation.ui.UiPlugin;
import org.teiid.designer.ui.explorer.ModelExplorerLabelProvider;
Expand Down Expand Up @@ -76,7 +76,7 @@ public Image getColumnImage(Object element, int columnIndex) {
break;
case 1 : // SQL Symbol Column
Object sqlSymbol = binding.getCurrentSymbol();
if(sqlSymbol!=null && sqlSymbol instanceof IExpression) {
if(sqlSymbol instanceof ILanguageObject && ((ILanguageObject) sqlSymbol).isExpression()) {
// Defect 23945 - added private method to get image for multiple types
// of SQL symbols
image = getImageForSymbol((IExpression)sqlSymbol);
Expand Down Expand Up @@ -109,7 +109,7 @@ private Image getImageForSymbol(IExpression seSymbol) {
IExpression expression = ((IExpressionSymbol)seSymbol).getExpression();
if(expression!=null && expression instanceof IConstant) {
result = UiPlugin.getDefault().getImage(CONSTANT_ICON);
} else if ( expression!=null && expression instanceof IFunction ) {
} else if ( expression!=null && expression.isFunction() ) {
result = UiPlugin.getDefault().getImage(FUNCTION_ICON);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ void expressionButtonPressed() {

// Need to crate an Expression Symbol (constant = NULL) name = "expr"
List symbolsList = new ArrayList(1);
if (langObj instanceof IExpression) {
if (langObj.isExpression()) {
IQueryService queryService = ModelerCore.getTeiidQueryService();
IQueryFactory factory = queryService.createQueryFactory();
IExpressionSymbol newExpression = factory.createExpressionSymbol(EXPRESSION, (IExpression)langObj);
Expand Down Expand Up @@ -742,7 +742,7 @@ public void addBindings( Object[] bindings ) {
public void removeBinding( Binding binding ) {
// Put the bound symbol back on the unmatched symbols list
Object sqlSymbol = binding.getCurrentSymbol();
if (sqlSymbol != null && sqlSymbol instanceof IExpression) {
if (sqlSymbol instanceof ILanguageObject && ((ILanguageObject) sqlSymbol).isExpression()) {
IExpression seSymbol = (IExpression)sqlSymbol;
sqlListPanel.addSymbol(seSymbol);
}
Expand All @@ -760,7 +760,7 @@ public void removeBindings( Object[] bindings ) {
for (int i = 0; i < bindings.length; i++) {
Binding binding = (Binding)bindings[i];
Object sqlSymbol = binding.getCurrentSymbol();
if (sqlSymbol != null && sqlSymbol instanceof IExpression) {
if (sqlSymbol instanceof ILanguageObject && ((ILanguageObject) sqlSymbol).isExpression()) {
IExpression seSymbol = (IExpression)sqlSymbol;
sqlListPanel.addSymbol(seSymbol);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.teiid.designer.query.sql.lang.IExpression;
import org.teiid.designer.query.sql.lang.ILanguageObject;
import org.teiid.designer.query.sql.symbol.IAggregateSymbol;
import org.teiid.designer.query.sql.symbol.IAliasSymbol;
import org.teiid.designer.query.sql.symbol.IConstant;
import org.teiid.designer.query.sql.symbol.IElementSymbol;
import org.teiid.designer.query.sql.symbol.IExpressionSymbol;
import org.teiid.designer.query.sql.symbol.IFunction;
import org.teiid.designer.transformation.ui.PluginConstants;
import org.teiid.designer.transformation.ui.UiPlugin;
import org.teiid.designer.transformation.util.TransformationSqlHelper;
Expand Down Expand Up @@ -48,7 +48,7 @@ public String getColumnText(Object element, int columnIndex) {
String symName = TransformationSqlHelper.getSingleElementSymbolShortName(uSymbol,true);
result = symName + " AS " + aSymbol.getShortName(); //$NON-NLS-1$
// SingleElementSymbol
} else if(element instanceof IExpression) {
} else if(element instanceof ILanguageObject && ((ILanguageObject) element).isExpression()) {
result = TransformationSqlHelper.getSingleElementSymbolShortName((IExpression)element,true);
}
}
Expand All @@ -65,7 +65,7 @@ public Image getColumnImage(Object element, int columnIndex) {
if(columnIndex==0) {
if(element instanceof IExpressionSymbol) {
image = UiPlugin.getDefault().getImage(FUNCTION_ICON);
} else if(element instanceof IExpression) {
} else if(element instanceof ILanguageObject && ((ILanguageObject) element).isExpression()) {
// Defect 23945 - added private method to get image for multiple types
// of SQL symbols
image = getImageForSymbol((IExpression)element);
Expand Down Expand Up @@ -95,7 +95,7 @@ private Image getImageForSymbol(IExpression seSymbol) {
IExpression expression = ((IExpressionSymbol)seSymbol).getExpression();
if(expression!=null && expression instanceof IConstant) {
result = UiPlugin.getDefault().getImage(CONSTANT_ICON);
} else if ( expression!=null && expression instanceof IFunction ) {
} else if ( expression!=null && expression.isFunction() ) {
result = UiPlugin.getDefault().getImage(FUNCTION_ICON);
}
}
Expand Down

0 comments on commit 9f36140

Please sign in to comment.