1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
/*
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation
//
package org.eclipse.persistence.jpa.jpql.tools;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.eclipse.persistence.jpa.jpql.Assert;
import org.eclipse.persistence.jpa.jpql.ExpressionTools;
import org.eclipse.persistence.jpa.jpql.ITypeHelper;
import org.eclipse.persistence.jpa.jpql.JPQLQueryDeclaration;
import org.eclipse.persistence.jpa.jpql.SemanticValidatorHelper;
import org.eclipse.persistence.jpa.jpql.parser.AbstractExpressionVisitor;
import org.eclipse.persistence.jpa.jpql.parser.Expression;
import org.eclipse.persistence.jpa.jpql.parser.IdentificationVariable;
import org.eclipse.persistence.jpa.jpql.parser.JPQLGrammar;
import org.eclipse.persistence.jpa.jpql.parser.Join;
import org.eclipse.persistence.jpa.jpql.parser.SimpleSelectStatement;
import org.eclipse.persistence.jpa.jpql.tools.resolver.Declaration;
import org.eclipse.persistence.jpa.jpql.tools.resolver.DeclarationResolver;
import org.eclipse.persistence.jpa.jpql.tools.resolver.Resolver;
import org.eclipse.persistence.jpa.jpql.tools.resolver.StateFieldResolver;
import org.eclipse.persistence.jpa.jpql.tools.spi.IConstructor;
import org.eclipse.persistence.jpa.jpql.tools.spi.IEntity;
import org.eclipse.persistence.jpa.jpql.tools.spi.IManagedType;
import org.eclipse.persistence.jpa.jpql.tools.spi.IMapping;
import org.eclipse.persistence.jpa.jpql.tools.spi.IType;
import org.eclipse.persistence.jpa.jpql.tools.spi.ITypeDeclaration;
import org.eclipse.persistence.jpa.jpql.utility.CollectionTools;
/**
* An implementation of {@link SemanticValidatorHelper} that uses {@link JPQLQueryContext} to return
* the required information and Hermes SPI.
* <p>
* 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.
*
* @version 2.5
* @since 2.4
* @author Pascal Filion
*/
@SuppressWarnings("nls")
public class GenericSemanticValidatorHelper implements SemanticValidatorHelper {
private IdentificationVariableVisitor identificationVariableVisitor;
/**
* The context used to query information about the JPQL query.
*/
private final JPQLQueryContext queryContext;
/**
* The concrete instance of {@link ITypeHelper} that simply wraps {@link TypeHelper}.
*/
private ITypeHelper typeHelper;
/**
* Creates a new <code>GenericSemanticValidatorHelper</code>.
*
* @param queryContext The context used to query information about the JPQL query
* @exception NullPointerException The given {@link JPQLQueryContext} cannot be <code>null</code>
*/
public GenericSemanticValidatorHelper(JPQLQueryContext queryContext) {
super();
Assert.isNotNull(queryContext, "The JPQLQueryContext cannot be null");
this.queryContext = queryContext;
}
protected void addIdentificationVariable(IdentificationVariable identificationVariable,
Map<String, List<IdentificationVariable>> identificationVariables) {
String variableName = (identificationVariable != null) ? identificationVariable.getVariableName() : null;
if (ExpressionTools.stringIsNotEmpty(variableName)) {
// Add the IdentificationVariable to the list
List<IdentificationVariable> variables = identificationVariables.get(variableName);
if (variables == null) {
variables = new ArrayList<IdentificationVariable>();
identificationVariables.put(variableName, variables);
}
variables.add(identificationVariable);
}
}
protected IdentificationVariableVisitor buildIdentificationVariableVisitor() {
return new IdentificationVariableVisitor();
}
/**
* {@inheritDoc}
*/
public void collectAllDeclarationIdentificationVariables(Map<String, List<IdentificationVariable>> identificationVariables) {
JPQLQueryContext currentContext = queryContext.getCurrentContext();
while (currentContext != null) {
collectLocalDeclarationIdentificationVariables(currentContext, identificationVariables);
currentContext = currentContext.getParent();
}
}
protected void collectLocalDeclarationIdentificationVariables(JPQLQueryContext queryContext,
Map<String, List<IdentificationVariable>> identificationVariables) {
DeclarationResolver declarationResolver = queryContext.getDeclarationResolverImp();
for (Declaration declaration : declarationResolver.getDeclarations()) {
// Register the identification variable from the base expression
IdentificationVariable identificationVariable = declaration.getIdentificationVariable();
addIdentificationVariable(identificationVariable, identificationVariables);
// Register the identification variable from the JOIN expressions
for (Join join : declaration.getJoins()) {
IdentificationVariable joinIdentificationVariable = getIdentificationVariable(join.getIdentificationVariable());
addIdentificationVariable(joinIdentificationVariable, identificationVariables);
}
}
if (queryContext.getParent() == null) {
for (IdentificationVariable identificationVariable : declarationResolver.getResultVariablesMap().keySet()) {
addIdentificationVariable(identificationVariable, identificationVariables);
}
}
}
/**
* {@inheritDoc}
*/
public void collectLocalDeclarationIdentificationVariables(Map<String, List<IdentificationVariable>> identificationVariables) {
collectLocalDeclarationIdentificationVariables(queryContext.getCurrentContext(), identificationVariables);
}
/**
* {@inheritDoc}
*/
public void disposeSubqueryContext() {
queryContext.disposeSubqueryContext();
}
/**
* {@inheritDoc}
*/
public String[] entityNames() {
List<String> names = new ArrayList<String>();
for (IEntity entity : queryContext.getProvider().entities()) {
names.add(entity.getName());
}
return names.toArray(new String[names.size()]);
}
/**
* {@inheritDoc}
*/
public List<JPQLQueryDeclaration> getAllDeclarations() {
List<JPQLQueryDeclaration> declarations = new ArrayList<JPQLQueryDeclaration>();
JPQLQueryContext context = queryContext.getCurrentContext();
while (context != null) {
declarations.addAll(context.getDeclarationResolverImp().getDeclarations());
context = context.getParent();
}
return declarations;
}
/**
* {@inheritDoc}
*/
public IConstructor[] getConstructors(Object type) {
return CollectionTools.array(IConstructor.class, ((IType) type).constructors());
}
/**
* {@inheritDoc}
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public List getDeclarations() {
return queryContext.getDeclarations();
}
/**
* {@inheritDoc}
*/
public IManagedType getEmbeddable(Object type) {
return queryContext.getProvider().getEmbeddable((IType) type);
}
/**
* {@inheritDoc}
*/
public IEntity getEntityNamed(String entityName) {
return queryContext.getProvider().getEntityNamed(entityName);
}
/**
* {@inheritDoc}
*/
public String[] getEnumConstants(Object type) {
return ((IType) type).getEnumConstants();
}
/**
* {@inheritDoc}
*/
public JPQLGrammar getGrammar() {
return queryContext.getGrammar();
}
protected IdentificationVariable getIdentificationVariable(Expression expression) {
IdentificationVariableVisitor visitor = getIdentificationVariableVisitor();
expression.accept(visitor);
try {
return visitor.identificationVariable;
}
finally {
visitor.identificationVariable = null;
}
}
protected IdentificationVariableVisitor getIdentificationVariableVisitor() {
if (identificationVariableVisitor == null) {
identificationVariableVisitor = buildIdentificationVariableVisitor();
}
return identificationVariableVisitor;
}
/**
* {@inheritDoc}
*/
public IManagedType getManagedType(Expression expression) {
return queryContext.getResolver(expression).getManagedType();
}
/**
* {@inheritDoc}
*/
public IMapping getMappingNamed(Object managedType, String path) {
return ((IManagedType) managedType).getMappingNamed(path);
}
/**
* {@inheritDoc}
*/
public IType getMappingType(Object mapping) {
return (mapping != null) ? ((IMapping) mapping).getType() : queryContext.getTypeHelper().unknownType();
}
/**
* {@inheritDoc}
*/
public ITypeDeclaration[] getMethodParameterTypeDeclarations(Object constructor) {
return ((IConstructor) constructor).getParameterTypes();
}
/**
* Returns the context used to query information about the JPQL query.
*
* @return The context used to query information about the JPQL query
*/
public JPQLQueryContext getQueryContext() {
return queryContext;
}
/**
* {@inheritDoc}
*/
public IManagedType getReferenceManagedType(Object relationshipMapping) {
if (relationshipMapping == null) {
return null;
}
IMapping mapping = (IMapping) relationshipMapping;
return mapping.getParent().getProvider().getManagedType(mapping.getType());
}
/**
* {@inheritDoc}
*/
public IType getType(Expression expression) {
return queryContext.getType(expression);
}
/**
* {@inheritDoc}
*/
public IType getType(Object typeDeclaration) {
return ((ITypeDeclaration) typeDeclaration).getType();
}
/**
* {@inheritDoc}
*/
public IType getType(String typeName) {
return queryContext.getType(typeName);
}
/**
* {@inheritDoc}
*/
public ITypeDeclaration getTypeDeclaration(Expression expression) {
return queryContext.getTypeDeclaration(expression);
}
/**
* {@inheritDoc}
*/
public ITypeHelper getTypeHelper() {
if (typeHelper == null) {
typeHelper = new GenericTypeHelper(queryContext.getTypeHelper());
}
return typeHelper;
}
/**
* {@inheritDoc}
*/
public String getTypeName(Object type) {
return ((IType) type).getName();
}
/**
* {@inheritDoc}
*/
public boolean isAssignableTo(Object type1, Object type2) {
return ((IType) type1).isAssignableTo((IType) type2) ;
}
/**
* {@inheritDoc}
*/
public boolean isCollectionIdentificationVariable(String variableName) {
return queryContext.isCollectionIdentificationVariable(variableName);
}
/**
* {@inheritDoc}
*/
public boolean isCollectionMapping(Object mapping) {
return (mapping != null) && ((IMapping) mapping).isCollection();
}
@Override
public boolean isEmbeddableMapping(Object mapping) {
return (mapping != null) && ((IMapping) mapping).isEmbeddable();
}
/**
* {@inheritDoc}
*/
public boolean isEnumType(Object type) {
return ((IType) type).isEnum();
}
/**
* {@inheritDoc}
*/
public boolean isIdentificationVariableValidInComparison(IdentificationVariable expression) {
return false;
}
/**
* {@inheritDoc}
*/
public boolean isManagedTypeResolvable(Object managedType) {
return ((IManagedType) managedType).getType().isResolvable();
}
/**
* {@inheritDoc}
*/
public boolean isPropertyMapping(Object mapping) {
return (mapping != null) && ((IMapping) mapping).isProperty();
}
/**
* {@inheritDoc}
*/
public boolean isRelationshipMapping(Object mapping) {
return (mapping != null) && ((IMapping) mapping).isRelationship();
}
/**
* {@inheritDoc}
*/
public boolean isResultVariable(String variableName) {
return queryContext.isResultVariable(variableName);
}
/**
* {@inheritDoc}
*/
public boolean isTransient(Object mapping) {
return (mapping != null) && ((IMapping) mapping).isTransient();
}
/**
* {@inheritDoc}
*/
public boolean isTypeDeclarationAssignableTo(Object typeDeclaration1, Object typeDeclaration2) {
ITypeDeclaration declaration1 = (ITypeDeclaration) typeDeclaration1;
ITypeDeclaration declaration2 = (ITypeDeclaration) typeDeclaration2;
// One is an array but not the other one
if (declaration1.isArray() && !declaration2.isArray() ||
!declaration1.isArray() && declaration2.isArray()) {
return false;
}
// Check the array dimensionality
if (declaration1.isArray()) {
return declaration1.getDimensionality() == declaration2.getDimensionality();
}
return isAssignableTo(declaration1.getType(), declaration2.getType());
}
/**
* {@inheritDoc}
*/
public boolean isTypeResolvable(Object type) {
return ((IType) type).isResolvable();
}
/**
* {@inheritDoc}
*/
public void newSubqueryContext(SimpleSelectStatement expression) {
queryContext.newSubqueryContext(expression);
}
/**
* {@inheritDoc}
*/
public IMapping resolveMapping(Expression expression) {
return queryContext.getResolver(expression).getMapping();
}
/**
* {@inheritDoc}
*/
public IMapping resolveMapping(String variableName, String name) {
Resolver parent = queryContext.getResolver(variableName);
Resolver resolver = parent.getChild(name);
if (resolver == null) {
resolver = new StateFieldResolver(parent, name);
parent.addChild(variableName, resolver);
}
return resolver.getMapping();
}
protected static class IdentificationVariableVisitor extends AbstractExpressionVisitor {
/**
*
*/
protected IdentificationVariable identificationVariable;
/**
* {@inheritDoc}
*/
@Override
public void visit(IdentificationVariable expression) {
identificationVariable = expression;
}
}
}
|