1 11 package org.eclipse.core.internal.expressions; 12 13 import org.w3c.dom.Element ; 14 15 import org.eclipse.core.runtime.Assert; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IAdapterManager; 18 import org.eclipse.core.runtime.IConfigurationElement; 19 import org.eclipse.core.runtime.Platform; 20 21 import org.eclipse.core.expressions.EvaluationResult; 22 import org.eclipse.core.expressions.ExpressionInfo; 23 import org.eclipse.core.expressions.IEvaluationContext; 24 25 public class AdaptExpression extends CompositeExpression { 26 27 private static final String ATT_TYPE= "type"; 29 32 private static final int HASH_INITIAL= AdaptExpression.class.getName().hashCode(); 33 34 private String fTypeName; 35 36 public AdaptExpression(IConfigurationElement configElement) throws CoreException { 37 fTypeName= configElement.getAttribute(ATT_TYPE); 38 Expressions.checkAttribute(ATT_TYPE, fTypeName); 39 } 40 41 public AdaptExpression(Element element) throws CoreException { 42 fTypeName= element.getAttribute(ATT_TYPE); 43 Expressions.checkAttribute(ATT_TYPE, fTypeName.length() > 0 ? fTypeName : null); 44 } 45 46 public AdaptExpression(String typeName) { 47 Assert.isNotNull(typeName); 48 fTypeName= typeName; 49 } 50 51 public boolean equals(final Object object) { 52 if (!(object instanceof AdaptExpression)) 53 return false; 54 55 final AdaptExpression that= (AdaptExpression)object; 56 return this.fTypeName.equals(that.fTypeName) 57 && equals(this.fExpressions, that.fExpressions); 58 } 59 60 protected int computeHashCode() { 61 return HASH_INITIAL * HASH_FACTOR + hashCode(fExpressions) 62 * HASH_FACTOR + fTypeName.hashCode(); 63 } 64 65 68 public EvaluationResult evaluate(IEvaluationContext context) throws CoreException { 69 if (fTypeName == null) 70 return EvaluationResult.FALSE; 71 Object var= context.getDefaultVariable(); 72 Object adapted= null; 73 IAdapterManager manager= Platform.getAdapterManager(); 74 if (Expressions.isInstanceOf(var, fTypeName)) { 75 adapted= var; 76 } else { 77 if (!manager.hasAdapter(var, fTypeName)) 78 return EvaluationResult.FALSE; 79 80 adapted= manager.getAdapter(var, fTypeName); 81 } 82 if (adapted == null) { 85 if (manager.queryAdapter(var, fTypeName) == IAdapterManager.NOT_LOADED) { 86 return EvaluationResult.NOT_LOADED; 87 } else { 88 return EvaluationResult.FALSE; 89 } 90 } 91 return evaluateAnd(new DefaultVariable(context, adapted)); 92 } 93 94 public void collectExpressionInfo(ExpressionInfo info) { 95 info.markDefaultVariableAccessed(); 99 super.collectExpressionInfo(info); 100 } 101 } 102 | Popular Tags |