1 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 ; 25 import java.util.List ; 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 37 public abstract class Rule<E> { 38 protected List <? 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 <? extends Predicate> preConditions){ 54 this.preConditions = preConditions; 55 } 56 57 65 protected ErrorDescription[] apply(E subject, ProblemContext ctx){ 66 if (isApplicable(subject, ctx)) { 67 if (!predicate.evaluate(subject)) { return new ErrorDescription[]{createProblem((Element)subject, ctx)}; 70 } 71 } 72 73 return null; 74 } 75 76 79 public Severity getSeverity(){ 80 return Severity.ERROR; 81 } 82 83 public abstract String 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 <Fix> fixes){ 106 ErrorDescription err = null; 107 List <Fix> fixList = fixes == null ? Collections.EMPTY_LIST : fixes; 108 109 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 |