KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > apt > dispatch > BaseMessagerImpl


1 /*******************************************************************************
2  * Copyright (c) 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * wharley@bea.com - derived base class from BatchMessagerImpl
10  *
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.compiler.apt.dispatch;
13
14 import javax.lang.model.element.Element;
15 import javax.tools.Diagnostic.Kind;
16
17 import org.eclipse.jdt.internal.compiler.CompilationResult;
18 import org.eclipse.jdt.internal.compiler.apt.model.ExecutableElementImpl;
19 import org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl;
20 import org.eclipse.jdt.internal.compiler.apt.model.VariableElementImpl;
21 import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
22 import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
23 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
24 import org.eclipse.jdt.internal.compiler.impl.ReferenceContext;
25 import org.eclipse.jdt.internal.compiler.lookup.Binding;
26 import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
27 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
28 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
29 import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
30 import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;
31 import org.eclipse.jdt.internal.compiler.util.Util;
32
33 public class BaseMessagerImpl {
34
35     static final String JavaDoc[] NO_ARGUMENTS = new String JavaDoc[0];
36
37     /**
38      * Create a CategorizedProblem that can be reported to an ICompilerRequestor, etc.
39      *
40      * @param e the element against which to report the message. If the element is not
41      * in the set of elements being compiled in the current round, the reference context
42      * and filename will be set to null.
43      * @return
44      */

45     public static AptProblem createProblem(Kind kind, CharSequence JavaDoc msg, Element e) {
46         ReferenceContext referenceContext = null;
47         int startPosition = 0;
48         int endPosition = 0;
49         if (e != null) {
50             switch(e.getKind()) {
51                 case ANNOTATION_TYPE :
52                 case INTERFACE :
53                 case CLASS :
54                 case ENUM :
55                     TypeElementImpl typeElementImpl = (TypeElementImpl) e;
56                     Binding typeBinding = typeElementImpl._binding;
57                     if (typeBinding instanceof SourceTypeBinding) {
58                         SourceTypeBinding sourceTypeBinding = (SourceTypeBinding) typeBinding;
59                         TypeDeclaration typeDeclaration = (TypeDeclaration) sourceTypeBinding.scope.referenceContext();
60                         referenceContext = typeDeclaration;
61                         startPosition = typeDeclaration.sourceStart;
62                         endPosition = typeDeclaration.sourceEnd;
63                     }
64                     break;
65                 case PACKAGE :
66                     // nothing to do: there is no reference context for a package
67
break;
68                 case CONSTRUCTOR :
69                 case METHOD :
70                     ExecutableElementImpl executableElementImpl = (ExecutableElementImpl) e;
71                     Binding binding = executableElementImpl._binding;
72                     if (binding instanceof MethodBinding) {
73                         MethodBinding methodBinding = (MethodBinding) binding;
74                         AbstractMethodDeclaration sourceMethod = methodBinding.sourceMethod();
75                         if (sourceMethod != null) {
76                             referenceContext = sourceMethod;
77                             startPosition = sourceMethod.sourceStart;
78                             endPosition = sourceMethod.sourceEnd;
79                         }
80                     }
81                     break;
82                 case ENUM_CONSTANT :
83                     break;
84                 case EXCEPTION_PARAMETER :
85                     break;
86                 case FIELD :
87                     VariableElementImpl variableElementImpl = (VariableElementImpl) e;
88                     binding = variableElementImpl._binding;
89                     if (binding instanceof FieldBinding) {
90                         FieldBinding fieldBinding = (FieldBinding) binding;
91                         FieldDeclaration fieldDeclaration = fieldBinding.sourceField();
92                         if (fieldDeclaration != null) {
93                             ReferenceBinding declaringClass = fieldBinding.declaringClass;
94                             if (declaringClass instanceof SourceTypeBinding) {
95                                 SourceTypeBinding sourceTypeBinding = (SourceTypeBinding) declaringClass;
96                                 TypeDeclaration typeDeclaration = (TypeDeclaration) sourceTypeBinding.scope.referenceContext();
97                                 referenceContext = typeDeclaration;
98                             }
99                             startPosition = fieldDeclaration.sourceStart;
100                             endPosition = fieldDeclaration.sourceEnd;
101                         }
102                     }
103                     break;
104                 case INSTANCE_INIT :
105                 case STATIC_INIT :
106                     break;
107                 case LOCAL_VARIABLE :
108                     break;
109                 case PARAMETER :
110                     break;
111                 case TYPE_PARAMETER :
112             }
113         }
114         StringBuilder JavaDoc builder = new StringBuilder JavaDoc(msg);
115         int lineNumber = 0;
116         int columnNumber = 1;
117         char[] fileName = null;
118         if (referenceContext != null) {
119             CompilationResult result = referenceContext.compilationResult();
120             fileName = result.fileName;
121             int[] lineEnds = null;
122             lineNumber = startPosition >= 0
123                     ? Util.getLineNumber(startPosition, lineEnds = result.getLineSeparatorPositions(), 0, lineEnds.length-1)
124                     : 0;
125             columnNumber = startPosition >= 0
126                     ? Util.searchColumnNumber(result.getLineSeparatorPositions(), lineNumber,startPosition)
127                     : 0;
128         }
129         int severity;
130         switch(kind) {
131             case ERROR :
132                 severity = ProblemSeverities.Error;
133                 break;
134             default :
135                 // There is no "INFO" equivalent in JDT
136
severity = ProblemSeverities.Warning;
137                 break;
138         }
139         return new AptProblem(
140                 referenceContext,
141                 fileName,
142                 String.valueOf(builder),
143                 0,
144                 NO_ARGUMENTS,
145                 severity,
146                 startPosition,
147                 endPosition,
148                 lineNumber,
149                 columnNumber);
150     }
151
152     public BaseMessagerImpl() {
153         super();
154     }
155
156 }
Popular Tags