001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.jpa.verification.common;
043:
044: import com.sun.source.tree.Tree;
045: import com.sun.source.util.SourcePositions;
046: import java.util.Collections;
047: import java.util.List;
048: import javax.lang.model.element.Element;
049: import org.netbeans.modules.j2ee.jpa.verification.JPAProblemFinder;
050: import org.netbeans.spi.editor.hints.ErrorDescription;
051: import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
052: import org.netbeans.spi.editor.hints.Fix;
053: import org.netbeans.spi.editor.hints.Severity;
054:
055: /**
056: * @author Tomasz.Slota@Sun.COM
057: * @author Sanjeeb.Sahoo@Sun.COM
058: */
059: public abstract class Rule<E> {
060: protected List<? extends Predicate> preConditions;
061: protected Predicate predicate;
062:
063: public final ErrorDescription[] execute(E subject,
064: ProblemContext ctx) {
065: if (isApplicable(subject, ctx)) {
066: return apply(subject, ctx);
067: }
068:
069: return null;
070: }
071:
072: protected void setPredicate(Predicate predicate) {
073: this .predicate = predicate;
074: }
075:
076: protected void setPreConditions(
077: List<? extends Predicate> preConditions) {
078: this .preConditions = preConditions;
079: }
080:
081: /**
082: * A rule is applied to an individual element, called subject.
083: *
084: * @param subject the element where the rule will be applied.
085: * @param ctx additional information passed onto this test.
086: * @return a problem object which represents a violation of a rule. It
087: * returns null, if no violation was detected.
088: */
089: protected ErrorDescription[] apply(E subject, ProblemContext ctx) {
090: if (isApplicable(subject, ctx)) {
091: if (!predicate.evaluate(subject)) { // found a violation
092: // TODO: remove this hack (cast)
093: //return new ErrorDescription[]{createProblem((Element)subject, ctx)};
094: }
095: }
096:
097: return null;
098: }
099:
100: protected boolean isApplicable(E subject, ProblemContext ctx) {
101: boolean result = true;
102:
103: if (preConditions != null) {
104: for (Predicate pred : preConditions) {
105: result = result && pred.evaluate(subject);
106: }
107: }
108:
109: return result;
110: }
111:
112: public static ErrorDescription createProblem(Element subject,
113: ProblemContext ctx, String description) {
114: return createProblem(subject, ctx, description, Severity.ERROR,
115: Collections.<Fix> emptyList());
116: }
117:
118: public static ErrorDescription createProblem(Element subject,
119: ProblemContext ctx, String description, Severity severity) {
120: return createProblem(subject, ctx, description, severity,
121: Collections.<Fix> emptyList());
122: }
123:
124: public static ErrorDescription createProblem(Element subject,
125: ProblemContext ctx, String description, Severity severity,
126: Fix fix) {
127: return createProblem(subject, ctx, description, severity,
128: Collections.singletonList(fix));
129: }
130:
131: public static ErrorDescription createProblem(Element subject,
132: ProblemContext ctx, String description, Fix fix) {
133: return createProblem(subject, ctx, description, Severity.ERROR,
134: Collections.singletonList(fix));
135: }
136:
137: public static ErrorDescription createProblem(Element subject,
138: ProblemContext ctx, String description, Severity severity,
139: List<Fix> fixes) {
140: ErrorDescription err = null;
141: List<Fix> fixList = fixes == null ? Collections
142: .<Fix> emptyList() : fixes;
143:
144: // by default place error annotation on the element being checked
145: Tree elementTree = ctx.getElementToAnnotate() == null ? ctx
146: .getCompilationInfo().getTrees().getTree(subject) : ctx
147: .getElementToAnnotate();
148:
149: if (elementTree != null) {
150: Utilities.TextSpan underlineSpan = Utilities
151: .getUnderlineSpan(ctx.getCompilationInfo(),
152: elementTree);
153:
154: err = ErrorDescriptionFactory.createErrorDescription(
155: severity, description, fixList,
156: ctx.getFileObject(),
157: underlineSpan.getStartOffset(), underlineSpan
158: .getEndOffset());
159: } else {
160: JPAProblemFinder.LOG
161: .severe(" could not create ErrorDescription: "
162: + "failed to find tree for " + subject);
163: }
164:
165: return err;
166: }
167: }
|