View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.plsql.rule;
5   
6   import java.util.List;
7   import java.util.logging.Logger;
8   
9   import net.sourceforge.pmd.RuleContext;
10  import net.sourceforge.pmd.lang.Language;
11  import net.sourceforge.pmd.lang.ast.Node;
12  import net.sourceforge.pmd.lang.plsql.ast.*;
13  import net.sourceforge.pmd.lang.rule.AbstractRule;
14  import net.sourceforge.pmd.lang.rule.ImmutableLanguage;
15  
16  public abstract class AbstractPLSQLRule extends AbstractRule implements PLSQLParserVisitor, ImmutableLanguage
17  {
18      private final static Logger LOGGER = Logger.getLogger(AbstractPLSQLRule.class.getName()); 
19      private final static String CLASS_NAME = AbstractPLSQLRule.class.getName(); 
20  
21    
22      public AbstractPLSQLRule() {
23  	super.setLanguage(Language.PLSQL);
24  	// Enable Type Resolution on PLSQL Rules by default
25  	super.setUsesTypeResolution();
26      }
27  
28      public void apply(List<? extends Node> nodes, RuleContext ctx) {
29  	visitAll(nodes, ctx);
30      }
31  
32      protected void visitAll(List<? extends Node> nodes, RuleContext ctx) {
33          LOGGER.entering(CLASS_NAME,"visitAll");
34  	for (Object element : nodes) {
35  	    ASTInput node = (ASTInput) element;
36  	    visit(node, ctx);
37  	}
38          LOGGER.exiting(CLASS_NAME,"visitAll");
39      }
40  
41      /**
42       * Gets the Image of the first parent node of type
43       * ASTClassOrInterfaceDeclaration or <code>null</code>
44       *
45       * @param node
46       *            the node which will be searched
47       */
48      protected final String getDeclaringType(Node node) {
49  	Node c;  
50  
51          /*
52           * Choose the Object Type
53           */
54  	if (null != (c = node.getFirstParentOfType(ASTPackageSpecification.class))) {
55  	    return c.getImage();
56  	}
57          else 
58  	if (null != (c = node.getFirstParentOfType(ASTTypeSpecification.class))) {
59  	    return c.getImage();
60  	}
61          else 
62  	if (null != (c = node.getFirstParentOfType(ASTPackageBody.class))) {
63  	    return c.getImage();
64  	}
65          else 
66  	if (null != (c = node.getFirstParentOfType(ASTTriggerUnit.class))) {
67  	    return c.getImage();
68  	}
69          else //Finally Schema-level Methods 
70  	if (null != (c = node.getFirstParentOfType(ASTProgramUnit.class))) {
71  	    return c.getImage();
72  	}
73  
74  	return null;
75      }
76  
77      public static boolean isQualifiedName(Node node) {
78  	return node.getImage().indexOf('.') != -1;
79      }
80  
81      public static boolean importsPackage(ASTInput node, String packageName) {
82  	return false;
83      }
84  
85    /*
86     * Duplicate PLSQLParserVisitor API 
87     */
88  	@Override
89  	public Object visit(PLSQLNode node, Object data) {
90  		node.childrenAccept(this, data);
91  		return null;
92  	}
93  
94  	@Override
95  	public Object visit(ASTInput node, Object data) {
96  		return visit((PLSQLNode) node, data);
97  	}
98  
99  	@Override
100 	public Object visit(ASTDDLCommand node, Object data) {
101 		return visit((PLSQLNode) node, data);
102 	}
103 
104 	@Override
105 	public Object visit(ASTSqlPlusCommand node, Object data) {
106 		return visit((PLSQLNode) node, data);
107 	}
108 
109 	@Override
110 	public Object visit(ASTGlobal node, Object data) {
111 		return visit((PLSQLNode) node, data);
112 	}
113 
114 	@Override
115 	public Object visit(ASTBlock node, Object data) {
116 		return visit((PLSQLNode) node, data);
117 	}
118 
119 	@Override
120 	public Object visit(ASTPackageSpecification node, Object data) {
121 		LOGGER.entering(CLASS_NAME, "visit(ASTPackageSpecification)");
122 		return visit((PLSQLNode) node, data);
123 	}
124 
125 	@Override
126 	public Object visit(ASTPackageBody node, Object data) {
127 		LOGGER.entering(CLASS_NAME, "visit(ASTPackageBody)");
128 		return visit((PLSQLNode) node, data);
129 	}
130 
131 	@Override
132 	public Object visit(ASTDeclarativeUnit node, Object data) {
133 		return visit((PLSQLNode) node, data);
134 	}
135 
136 	@Override
137 	public Object visit(ASTDeclarativeSection node, Object data) {
138 		return visit((PLSQLNode) node, data);
139 	}
140 
141 	@Override
142 	public Object visit(ASTCompilationDeclarationFragment node, Object data) {
143 		return visit((PLSQLNode) node, data);
144 	}
145 
146 	@Override
147 	public Object visit(ASTProgramUnit node, Object data) {
148 		LOGGER.entering(CLASS_NAME, "visit(ASTProgramUnit)");
149 		return visit((PLSQLNode) node, data);
150 	}
151 
152 	@Override
153 	public Object visit(ASTObjectNameDeclaration node, Object data) {
154 		return visit((PLSQLNode) node, data);
155 	}
156 
157 	@Override
158 	public Object visit(ASTFormalParameter node, Object data) {
159 		return visit((PLSQLNode) node, data);
160 	}
161 
162 	@Override
163 	public Object visit(ASTMethodDeclaration node, Object data) {
164 		return visit((PLSQLNode) node, data);
165 	}
166 
167 	@Override
168 	public Object visit(ASTMethodDeclarator node, Object data) {
169 		return visit((PLSQLNode) node, data);
170 	}
171 
172 	@Override
173 	public Object visit(ASTFormalParameters node, Object data) {
174 		return visit((PLSQLNode) node, data);
175 	}
176 
177 	@Override
178 	public Object visit(ASTVariableOrConstantDeclarator node, Object data) {
179 		return visit((PLSQLNode) node, data);
180 	}
181 
182 	@Override
183 	public Object visit(ASTVariableOrConstantDeclaratorId node, Object data) {
184 		return visit((PLSQLNode) node, data);
185 	}
186 
187 	@Override
188 	public Object visit(ASTVariableOrConstantInitializer node, Object data) {
189 		return visit((PLSQLNode) node, data);
190 	}
191 
192 	@Override
193 	public Object visit(ASTDatatype node, Object data) {
194 		return visit((PLSQLNode) node, data);
195 	}
196 
197 	@Override
198 	public Object visit(ASTCompilationDataType node, Object data) {
199 		return visit((PLSQLNode) node, data);
200 	}
201 
202 	@Override
203 	public Object visit(ASTCollectionTypeName node, Object data) {
204 		return visit((PLSQLNode) node, data);
205 	}
206 
207 	@Override
208 	public Object visit(ASTScalarDataTypeName node, Object data) {
209 		return visit((PLSQLNode) node, data);
210 	}
211 
212 	@Override
213 	public Object visit(ASTDateTimeLiteral node, Object data) {
214 		return visit((PLSQLNode) node, data);
215 	}
216 
217 	@Override
218 	public Object visit(ASTExceptionHandler node, Object data) {
219 		return visit((PLSQLNode) node, data);
220 	}
221 
222 	@Override
223 	public Object visit(ASTSkip2NextTerminator node, Object data) {
224 		return visit((PLSQLNode) node, data);
225 	}
226 
227 	@Override
228 	public Object visit(ASTSkip2NextOccurrence node, Object data) {
229 		return visit((PLSQLNode) node, data);
230 	}
231 
232 	@Override
233 	public Object visit(ASTSkipPastNextOccurrence node, Object data) {
234 		return visit((PLSQLNode) node, data);
235 	}
236 
237 	@Override
238 	public Object visit(ASTSkip2NextTokenOccurrence node, Object data) {
239 		return visit((PLSQLNode) node, data);
240 	}
241 
242 	@Override
243 	public Object visit(ASTSkipPastNextTokenOccurrence node, Object data) {
244 		return visit((PLSQLNode) node, data);
245 	}
246 
247 	@Override
248 	public Object visit(ASTRead2NextOccurrence node, Object data) {
249 		return visit((PLSQLNode) node, data);
250 	}
251 
252 	@Override
253 	public Object visit(ASTReadPastNextOccurrence node, Object data) {
254 		return visit((PLSQLNode) node, data);
255 	}
256 
257 	@Override
258 	public Object visit(ASTSqlStatement node, Object data) {
259 		return visit((PLSQLNode) node, data);
260 	}
261 
262 	@Override
263 	public Object visit(ASTWrappedObject node, Object data) {
264 		return visit((PLSQLNode) node, data);
265 	}
266 
267 	@Override
268 	public Object visit(ASTUnlabelledStatement node, Object data) {
269 		return visit((PLSQLNode) node, data);
270 	}
271 
272 	@Override
273 	public Object visit(ASTStatement node, Object data) {
274 		return visit((PLSQLNode) node, data);
275 	}
276 
277 	@Override
278 	public Object visit(ASTLabelledStatement node, Object data) {
279 		return visit((PLSQLNode) node, data);
280 	}
281 
282 	@Override
283 	public Object visit(ASTCaseStatement node, Object data) {
284 		return visit((PLSQLNode) node, data);
285 	}
286 
287 	@Override
288 	public Object visit(ASTCaseWhenClause node, Object data) {
289 		return visit((PLSQLNode) node, data);
290 	}
291 
292 	@Override
293 	public Object visit(ASTElseClause node, Object data) {
294 		return visit((PLSQLNode) node, data);
295 	}
296 
297 	@Override
298 	public Object visit(ASTElsifClause node, Object data) {
299 		return visit((PLSQLNode) node, data);
300 	}
301 
302 	@Override
303 	public Object visit(ASTLoopStatement node, Object data) {
304 		return visit((PLSQLNode) node, data);
305 	}
306 
307 	@Override
308 	public Object visit(ASTForStatement node, Object data) {
309 		return visit((PLSQLNode) node, data);
310 	}
311 
312 	@Override
313 	public Object visit(ASTWhileStatement node, Object data) {
314 		return visit((PLSQLNode) node, data);
315 	}
316 
317 	@Override
318 	public Object visit(ASTIfStatement node, Object data) {
319 		return visit((PLSQLNode) node, data);
320 	}
321 
322 	@Override
323 	public Object visit(ASTForIndex node, Object data) {
324 		return visit((PLSQLNode) node, data);
325 	}
326 
327 	@Override
328 	public Object visit(ASTForAllIndex node, Object data) {
329 		return visit((PLSQLNode) node, data);
330 	}
331 
332 	@Override
333 	public Object visit(ASTForAllStatement node, Object data) {
334 		return visit((PLSQLNode) node, data);
335 	}
336 
337 	@Override
338 	public Object visit(ASTGotoStatement node, Object data) {
339 		return visit((PLSQLNode) node, data);
340 	}
341 
342 	@Override
343 	public Object visit(ASTReturnStatement node, Object data) {
344 		return visit((PLSQLNode) node, data);
345 	}
346 
347 	@Override
348 	public Object visit(ASTContinueStatement node, Object data) {
349 		return visit((PLSQLNode) node, data);
350 	}
351 
352 	@Override
353 	public Object visit(ASTExitStatement node, Object data) {
354 		return visit((PLSQLNode) node, data);
355 	}
356 
357 	@Override
358 	public Object visit(ASTRaiseStatement node, Object data) {
359 		return visit((PLSQLNode) node, data);
360 	}
361 
362 	@Override
363 	public Object visit(ASTCloseStatement node, Object data) {
364 		return visit((PLSQLNode) node, data);
365 	}
366 
367 	@Override
368 	public Object visit(ASTOpenStatement node, Object data) {
369 		return visit((PLSQLNode) node, data);
370 	}
371 
372 	@Override
373 	public Object visit(ASTFetchStatement node, Object data) {
374 		return visit((PLSQLNode) node, data);
375 	}
376 
377 	@Override
378 	public Object visit(ASTEmbeddedSqlStatement node, Object data) {
379 		return visit((PLSQLNode) node, data);
380 	}
381 
382 	@Override
383 	public Object visit(ASTPipelineStatement node, Object data) {
384 		return visit((PLSQLNode) node, data);
385 	}
386 
387 	@Override
388 	public Object visit(ASTConditionalCompilationStatement node, Object data) {
389 		return visit((PLSQLNode) node, data);
390 	}
391 
392 	@Override
393 	public Object visit(ASTSubTypeDefinition node, Object data) {
394 		return visit((PLSQLNode) node, data);
395 	}
396 
397 	@Override
398 	public Object visit(ASTFieldDeclaration node, Object data) {
399 		return visit((PLSQLNode) node, data);
400 	}
401 
402 	@Override
403 	public Object visit(ASTCollectionTypeDefinition node, Object data) {
404 		return visit((PLSQLNode) node, data);
405 	}
406 
407 	@Override
408 	public Object visit(ASTCollectionDeclaration node, Object data) {
409 		return visit((PLSQLNode) node, data);
410 	}
411 
412 	@Override
413 	public Object visit(ASTObjectDeclaration node, Object data) {
414 		return visit((PLSQLNode) node, data);
415 	}
416 
417 	@Override
418 	public Object visit(ASTCallSpecTail node, Object data) {
419 		return visit((PLSQLNode) node, data);
420 	}
421 
422 	@Override
423 	public Object visit(ASTCursorUnit node, Object data) {
424 		return visit((PLSQLNode) node, data);
425 	}
426 
427 	@Override
428 	public Object visit(ASTCursorSpecification node, Object data) {
429 		return visit((PLSQLNode) node, data);
430 	}
431 
432 	@Override
433 	public Object visit(ASTCursorBody node, Object data) {
434 		return visit((PLSQLNode) node, data);
435 	}
436 
437 	@Override
438 	public Object visit(ASTExpression node, Object data) {
439 		return visit((PLSQLNode) node, data);
440 	}
441 
442 	@Override
443 	public Object visit(ASTCompilationExpression node, Object data) {
444 		return visit((PLSQLNode) node, data);
445 	}
446 
447 	@Override
448 	public Object visit(ASTAssignment node, Object data) {
449 		return visit((PLSQLNode) node, data);
450 	}
451 
452 	@Override
453 	public Object visit(ASTCaseExpression node, Object data) {
454 		return visit((PLSQLNode) node, data);
455 	}
456 
457 	@Override
458 	public Object visit(ASTLikeExpression node, Object data) {
459 		return visit((PLSQLNode) node, data);
460 	}
461 
462 	@Override
463 	public Object visit(ASTTrimExpression node, Object data) {
464 		return visit((PLSQLNode) node, data);
465 	}
466 
467 	@Override
468 	public Object visit(ASTObjectExpression node, Object data) {
469 		return visit((PLSQLNode) node, data);
470 	}
471 
472 	@Override
473 	public Object visit(ASTConditionalOrExpression node, Object data) {
474 		return visit((PLSQLNode) node, data);
475 	}
476 
477 	@Override
478 	public Object visit(ASTConditionalAndExpression node, Object data) {
479 		return visit((PLSQLNode) node, data);
480 	}
481 
482 	@Override
483 	public Object visit(ASTEqualityExpression node, Object data) {
484 		return visit((PLSQLNode) node, data);
485 	}
486 
487 	@Override
488 	public Object visit(ASTRelationalExpression node, Object data) {
489 		return visit((PLSQLNode) node, data);
490 	}
491 
492 	@Override
493 	public Object visit(ASTAdditiveExpression node, Object data) {
494 		return visit((PLSQLNode) node, data);
495 	}
496 
497 	@Override
498 	public Object visit(ASTStringExpression node, Object data) {
499 		return visit((PLSQLNode) node, data);
500 	}
501 
502 	@Override
503 	public Object visit(ASTMultiplicativeExpression node, Object data) {
504 		return visit((PLSQLNode) node, data);
505 	}
506 
507 	@Override
508 	public Object visit(ASTUnaryExpression node, Object data) {
509 		return visit((PLSQLNode) node, data);
510 	}
511 
512 	@Override
513 	public Object visit(ASTUnaryExpressionNotPlusMinus node, Object data) {
514 		return visit((PLSQLNode) node, data);
515 	}
516 
517 	@Override
518 	public Object visit(ASTPrimaryExpression node, Object data) {
519 		return visit((PLSQLNode) node, data);
520 	}
521 
522 	@Override
523 	public Object visit(ASTPrimaryPrefix node, Object data) {
524 		return visit((PLSQLNode) node, data);
525 	}
526 
527 	@Override
528 	public Object visit(ASTPrimarySuffix node, Object data) {
529 		return visit((PLSQLNode) node, data);
530 	}
531 
532 	@Override
533 	public Object visit(ASTLiteral node, Object data) {
534 		return visit((PLSQLNode) node, data);
535 	}
536 
537 	@Override
538 	public Object visit(ASTStringLiteral node, Object data) {
539 		return visit((PLSQLNode) node, data);
540 	}
541 
542 	@Override
543 	public Object visit(ASTBooleanLiteral node, Object data) {
544 		return visit((PLSQLNode) node, data);
545 	}
546 
547 	@Override
548 	public Object visit(ASTNullLiteral node, Object data) {
549 		return visit((PLSQLNode) node, data);
550 	}
551 
552 	@Override
553 	public Object visit(ASTMultiSetCondition node, Object data) {
554 		return visit((PLSQLNode) node, data);
555 	}
556 
557 	@Override
558 	public Object visit(ASTNumericLiteral node, Object data) {
559 		return visit((PLSQLNode) node, data);
560 	}
561 
562 	@Override
563 	public Object visit(ASTLabel node, Object data) {
564 		return visit((PLSQLNode) node, data);
565 	}
566 
567 	@Override
568 	public Object visit(ASTName node, Object data) {
569 		return visit((PLSQLNode) node, data);
570 	}
571 
572 	@Override
573 	public Object visit(ASTQualifiedName node, Object data) {
574 		return visit((PLSQLNode) node, data);
575 	}
576 
577 	@Override
578 	public Object visit(ASTArguments node, Object data) {
579 		return visit((PLSQLNode) node, data);
580 	}
581 
582 	@Override
583 	public Object visit(ASTArgumentList node, Object data) {
584 		return visit((PLSQLNode) node, data);
585 	}
586 
587 	@Override
588 	public Object visit(ASTArgument node, Object data) {
589 		return visit((PLSQLNode) node, data);
590 	}
591 
592 	@Override
593 	public Object visit(ASTVariableOrConstantDeclaration node, Object data) {
594 		return visit((PLSQLNode) node, data);
595 	}
596 
597 	@Override
598 	public Object visit(ASTDatatypeDeclaration node, Object data) {
599 		return visit((PLSQLNode) node, data);
600 	}
601 
602 	@Override
603 	public Object visit(ASTPragma node, Object data) {
604 		return visit((PLSQLNode) node, data);
605 	}
606 
607 	@Override
608 	public Object visit(ASTExceptionDeclaration node, Object data) {
609 		return visit((PLSQLNode) node, data);
610 	}
611 
612 	@Override
613 	public Object visit(ASTParallelClause node, Object data) {
614 		return visit((PLSQLNode) node, data);
615 	}
616 
617 	@Override
618 	public Object visit(ASTTable node, Object data) {
619 		return visit((PLSQLNode) node, data);
620 	}
621 
622 	@Override
623 	public Object visit(ASTTableColumn node, Object data) {
624 		return visit((PLSQLNode) node, data);
625 	}
626 
627 	@Override
628 	public Object visit(ASTView node, Object data) {
629 		return visit((PLSQLNode) node, data);
630 	}
631 
632 	@Override
633 	public Object visit(ASTSynonym node, Object data) {
634 		return visit((PLSQLNode) node, data);
635 	}
636 
637 	@Override
638 	public Object visit(ASTDirectory node, Object data) {
639 		return visit((PLSQLNode) node, data);
640 	}
641 
642 	@Override
643 	public Object visit(ASTDatabaseLink node, Object data) {
644 		return visit((PLSQLNode) node, data);
645 	}
646 
647 	@Override
648 	public Object visit(ASTViewColumn node, Object data) {
649 		return visit((PLSQLNode) node, data);
650 	}
651 
652 	@Override
653 	public Object visit(ASTComment node, Object data) {
654 		return visit((PLSQLNode) node, data);
655 	}
656 
657 	@Override
658 	public Object visit(ASTTypeMethod node, Object data) {
659 		LOGGER.entering(CLASS_NAME, "visit(ASTTypeMethod)");
660 		return visit((PLSQLNode) node, data);
661 	}
662 
663 	@Override
664 	public Object visit(ASTTypeSpecification node, Object data) {
665 		LOGGER.entering(CLASS_NAME, "visit(ASTTypeSpecification)");
666 		return visit((PLSQLNode) node, data);
667 	}
668 
669 	@Override
670 	public Object visit(ASTAlterTypeSpec node, Object data) {
671 		return visit((PLSQLNode) node, data);
672 	}
673 
674 	@Override
675 	public Object visit(ASTAttributeDeclaration node, Object data) {
676 		return visit((PLSQLNode) node, data);
677 	}
678 
679 	@Override
680 	public Object visit(ASTAttribute node, Object data) {
681 		return visit((PLSQLNode) node, data);
682 	}
683 
684 	@Override
685 	public Object visit(ASTPragmaClause node, Object data) {
686 		return visit((PLSQLNode) node, data);
687 	}
688 
689 	@Override
690 	public Object visit(ASTInlinePragma node, Object data) {
691 		return visit((PLSQLNode) node, data);
692 	}
693 
694 	@Override
695 	public Object visit(ASTTriggerUnit node, Object data) {
696 		LOGGER.entering(CLASS_NAME, "visit(ASTTriggerUnit)");
697 		return visit((PLSQLNode) node, data);
698 	}
699 
700 	@Override
701 	public Object visit(ASTTriggerTimingPointSection node, Object data) {
702 		LOGGER.entering(CLASS_NAME, "visit(ASTTriggerTimingPointSection)");
703 		return visit((PLSQLNode) node, data);
704 	}
705 
706 	@Override
707 	public Object visit(ASTCompoundTriggerBlock node, Object data) {
708 		return visit((PLSQLNode) node, data);
709 	}
710 
711 	@Override
712 	public Object visit(ASTNonDMLTrigger node, Object data) {
713 		return visit((PLSQLNode) node, data);
714 	}
715 
716 	@Override
717 	public Object visit(ASTDDLEvent node, Object data) {
718 		return visit((PLSQLNode) node, data);
719 	}
720 
721 	@Override
722 	public Object visit(ASTDatabaseEvent node, Object data) {
723 		return visit((PLSQLNode) node, data);
724 	}
725 
726 	@Override
727 	public Object visit(ASTNonDMLEvent node, Object data) {
728 		return visit((PLSQLNode) node, data);
729 	}
730 
731 	@Override
732 	public Object visit(ASTAlterTrigger node, Object data) {
733 		return visit((PLSQLNode) node, data);
734 	}
735 
736 	@Override
737 	public Object visit(ASTKEYWORD_RESERVED node, Object data) {
738 		return visit((PLSQLNode) node, data);
739 	}
740 
741 	@Override
742 	public Object visit(ASTKEYWORD_UNRESERVED node, Object data) {
743 		return visit((PLSQLNode) node, data);
744 	}
745 
746 	@Override
747 	public Object visit(ASTID node, Object data) {
748 		return visit((PLSQLNode) node, data);
749 	}
750 
751 	@Override
752 	public Object visit(ASTUnqualifiedID node, Object data) {
753 		return visit((PLSQLNode) node, data);
754 	}
755 
756 	@Override
757 	public Object visit(ASTQualifiedID node, Object data) {
758 		return visit((PLSQLNode) node, data);
759 	}
760 
761 	@Override
762 	public Object visit(ASTTypeKeyword node, Object data) {
763 		return visit((PLSQLNode) node, data);
764 	}
765 
766 	@Override
767 	public Object visit(ASTJavaInterfaceClass node, Object data) {
768 		return visit((PLSQLNode) node, data);
769 	}
770 
771 	@Override
772 	public Object visit(ASTEqualsOldIDNewID node, Object data) {
773 		return visit((PLSQLNode) node, data);
774 	}
775 
776 	@Override
777 	public Object visit(ASTAccessibleByClause node, Object data) {
778 		return visit((PLSQLNode) node, data);
779 	}
780 
781   /*
782    * Treat all Executable Code 
783    */
784   public Object visit(ExecutableCode node, Object data) {
785         LOGGER.entering(CLASS_NAME,"visit(ExecutableCode)");
786         return visit((PLSQLNode) node, data); 
787     }
788 }
789 
790 
791