KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jpa > verification > common > Rule


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.jpa.verification.common;
21
22 import com.sun.source.tree.Tree;
23 import com.sun.source.util.SourcePositions;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.lang.model.element.Element;
27 import org.netbeans.modules.j2ee.jpa.verification.JPAProblemFinder;
28 import org.netbeans.spi.editor.hints.ErrorDescription;
29 import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
30 import org.netbeans.spi.editor.hints.Fix;
31 import org.netbeans.spi.editor.hints.Severity;
32
33 /**
34  * @author Tomasz.Slota@Sun.COM
35  * @author Sanjeeb.Sahoo@Sun.COM
36  */

37 public abstract class Rule<E> {
38     protected List JavaDoc<? extends Predicate> preConditions;
39     protected Predicate predicate;
40     
41     public final ErrorDescription[] execute(E subject, ProblemContext ctx){
42         if (isApplicable(subject, ctx)){
43             return apply(subject, ctx);
44         }
45         
46         return null;
47     }
48     
49     protected void setPredicate(Predicate predicate){
50         this.predicate = predicate;
51     }
52     
53     protected void setPreConditions(List JavaDoc<? extends Predicate> preConditions){
54         this.preConditions = preConditions;
55     }
56     
57     /**
58      * A rule is applied to an individual element, called subject.
59      *
60      * @param subject the element where the rule will be applied.
61      * @param ctx additional information passed onto this test.
62      * @return a problem object which represents a violation of a rule. It
63      * returns null, if no violation was detected.
64      */

65     protected ErrorDescription[] apply(E subject, ProblemContext ctx){
66         if (isApplicable(subject, ctx)) {
67             if (!predicate.evaluate(subject)) { // found a violation
68
// TODO: remove this hack (cast)
69
return new ErrorDescription[]{createProblem((Element)subject, ctx)};
70             }
71         }
72         
73         return null;
74     }
75     
76     /**
77      * How critical is the failure.
78      */

79     public Severity getSeverity(){
80         return Severity.ERROR;
81     }
82     
83     public abstract String JavaDoc getDescription();
84     
85     protected boolean isApplicable(E subject, ProblemContext ctx) {
86         boolean result = true;
87         
88         if (preConditions != null){
89             for (Predicate pred : preConditions) {
90                 result = result && pred.evaluate(subject);
91             }
92         }
93         
94         return result;
95     }
96     
97     protected ErrorDescription createProblem(Element subject, ProblemContext ctx){
98         return createProblem(subject, ctx, Collections.EMPTY_LIST);
99     }
100     
101     protected ErrorDescription createProblem(Element subject, ProblemContext ctx, Fix fix){
102         return createProblem(subject, ctx, Collections.singletonList(fix));
103     }
104     
105     protected ErrorDescription createProblem(Element subject, ProblemContext ctx, List JavaDoc<Fix> fixes){
106         ErrorDescription err = null;
107         List JavaDoc<Fix> fixList = fixes == null ? Collections.EMPTY_LIST : fixes;
108         
109         // by default place error annotation on the element being checked
110
Tree elementTree = ctx.getElementToAnnotate() == null ?
111             ctx.getCompilationInfo().getTrees().getTree(subject) : ctx.getElementToAnnotate();
112         
113         if (elementTree != null){
114             SourcePositions srcPositions = ctx.getCompilationInfo().getTrees().getSourcePositions();
115             
116             Utilities.TextSpan underlineSpan = Utilities.getUnderlineSpan(
117                     ctx.getCompilationInfo(), elementTree);
118             
119             err = ErrorDescriptionFactory.createErrorDescription(
120                     getSeverity(), getDescription(), fixList, ctx.getFileObject(),
121                     underlineSpan.getStartOffset(), underlineSpan.getEndOffset());
122         }
123         else{
124             JPAProblemFinder.LOG.severe(getClass().getName() + " could not create ErrorDescription: " +
125                     "failed to find tree for " + subject);
126         }
127         
128         return err;
129     }
130 }
131
Popular Tags