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