/* // $Id: //guest/julian_hyde/saffron/src/main/saffron/util/SaffronValidator.java#2 $ // Saffron preprocessor and data engine // Copyright (C) 2002 Julian Hyde <julian.hyde@mail.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the // Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // See the COPYING file located in the top-level-directory of // the archive of this library for complete text of license. // // jhyde, 25 April, 2002 */ package saffron.util; import openjava.mop.*; import openjava.ptree.*; import openjava.ptree.util.VariableBinder; /** * <code>SaffronValidator</code> makes sure that an expression is valid. It * produces more meaningful error messages than code further down the line (for * example {@link saffron.rel.SaffronQueryExpander}) would produce. * * @author jhyde * @since 25 April, 2002 * @version $Id: //guest/julian_hyde/saffron/src/main/saffron/util/SaffronValidator.java#2 $ **/ public class SaffronValidator extends VariableBinder { public SaffronValidator(Environment env) { super(env); } private static OJClass lookupClass(Environment env, ParseTree ref) { OJClass clazz = null; if (ref instanceof TypeName) { TypeName refType = (TypeName) ref; if (refType != null) { String qname = env.toQualifiedName(refType.toString()); clazz = env.lookupClass(qname); } if (clazz == null) { throw Util.newInternal( "unknown type '" + refType + "'"); } } else if (ref instanceof Expression) { Expression exp = (Expression) ref; try { clazz = exp.getType(env); } catch (Exception e) { throw Util.newInternal( e, "cannot derive type for expression '" + exp + "'"); } if (clazz == null) { throw Util.newInternal( "cannot derive type for expression '" + exp + "'"); } } else { throw Util.newInternal( "cannot derive type for " + ref.getClass() + ": " + ref); } return clazz; } public Expression evaluateUp(FieldAccess p) throws ParseTreeException { Environment env = getEnvironment(); OJClass reftype = lookupClass(env, p.getReference()); String name = p.getName(); OJClass situation = env.lookupClass(env.currentClassName()); OJField field = null; try { field = reftype.getField(name, situation); } catch (NoSuchMemberException e) { Signature signature = new Signature(name); throw Util.newInternal( e, "no " + signature + " is accessible in type '" + reftype + "'"); } if (field == null) { Signature signature = new Signature(name); throw Util.newInternal( "no " + signature + " is accessible in type '" + reftype + "'"); } return super.evaluateUp(p); } public Expression evaluateUp(MethodCall p) throws ParseTreeException { Environment env = getEnvironment(); ParseTree ref = p.getReferenceExpr(); if (ref == null) { ref = p.getReferenceType(); } if (ref == null) { ref = SelfAccess.constantThis(); } OJClass refType = lookupClass(env, ref); ExpressionList args = p.getArguments(); OJClass[] argTypes = new OJClass[args.size()]; for (int i = 0; i < argTypes.length; i++) { argTypes[i] = lookupClass(env, args.get(i)); } String name = p.getName(); OJClass situation = env.lookupClass(env.currentClassName()); OJMethod method = null; try { method = refType.getAcceptableMethod(name, argTypes, situation); } catch (NoSuchMemberException e) { Signature signature = new Signature(name, argTypes); throw Util.newInternal( e, "no " + signature + " found"); } if (method == null) { Signature signature = new Signature(name, argTypes); throw Util.newInternal( "no " + signature + " found"); } return super.evaluateUp(p); } public TypeName evaluateUp(TypeName p) throws ParseTreeException { Environment env = getEnvironment(); String qname = env.toQualifiedName( p.getName() ); OJClass clazz = env.lookupClass( qname, p.getDimension() ); if (clazz == null) { throw Util.newInternal( "unknown type " + p.toString()); } return super.evaluateUp(p); } } // End SaffronValidator.java
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 1801 | Julian Hyde |
saffron: add ObjectSchema; rules can now be matched more than once; started to implement correlations in queries in from list. |
||
#1 | 1749 | Julian Hyde |
saffron: Oops, forgot some files. |