]> source.dussan.org Git - aspectj.git/blob
f92d37877d7e9254041f4c7941c1b728e43e3dad
[aspectj.git] /
1 /* *******************************************************************
2  * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
3  * All rights reserved. 
4  * This program and the accompanying materials are made available 
5  * under the terms of the Eclipse Public License v1.0 
6  * which accompanies this distribution and is available at 
7  * http://www.eclipse.org/legal/epl-v10.html 
8  *  
9  * Contributors: 
10  *     PARC     initial implementation 
11  * ******************************************************************/
12
13
14 package org.aspectj.ajdt.internal.core.builder;
15
16 import java.io.File;
17
18 import org.aspectj.ajdt.internal.compiler.lookup.EclipseSourceLocation;
19 import org.aspectj.bridge.ISourceLocation;
20 import org.aspectj.bridge.SourceLocation;
21 import org.aspectj.org.eclipse.jdt.core.compiler.IProblem;
22 import org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult;
23 import org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult.ProblemsForRemovalFilter;
24 import org.aspectj.weaver.IEclipseSourceContext;
25 import org.aspectj.weaver.IHasPosition;
26 import org.aspectj.weaver.Member;
27
28
29
30 public class EclipseSourceContext implements IEclipseSourceContext {
31         
32         CompilationResult result;
33         int offset = 0;
34
35         public EclipseSourceContext(CompilationResult result) {
36                 this.result = result;
37         }
38         
39         public EclipseSourceContext(CompilationResult result, int offset) {
40                 this.result = result;
41                 this.offset = offset;
42         }
43         
44         public int getOffset() {
45                 return offset;
46         }
47         
48         private File getSourceFile() {
49                 return new File(new String(result.fileName));
50         }
51
52         public ISourceLocation makeSourceLocation(IHasPosition position) {
53                 return new EclipseSourceLocation(result, position.getStart(), position.getEnd());
54         }
55
56     public ISourceLocation makeSourceLocation(int line, int offset) {
57                 SourceLocation sl = new SourceLocation(getSourceFile(), line);
58
59         if (offset > 0) {
60             sl.setOffset(offset);
61         } else {
62             // compute the offset
63             //TODO AV - should we do it lazily?
64             int[] offsets = result.lineSeparatorPositions;
65             int likelyOffset = 0;
66             if (line > 0 && line < offsets.length) {
67                 //1st char of given line is next char after previous end of line
68                 likelyOffset = offsets[line-1];//FIXME may be need -2
69             }
70             sl.setOffset(likelyOffset);
71         }
72         return sl;
73         }
74     
75     public void tidy() {
76           result=null;
77     }
78
79         public void removeUnnecessaryProblems(Member member, int problemLineNumber) {
80                 if (result == null) return; 
81                 IProblem[] probs = result.getProblems();
82                 if (probs!=null) {
83                         for (int i = 0; i < probs.length; i++) {
84                                 IProblem problem = probs[i];
85                                 if (problem == null) continue;
86                                 if (problem.getID() == IProblem.UnusedMethodDeclaredThrownException 
87                                                 || problem.getID() == IProblem.UnusedConstructorDeclaredThrownException) {
88                                         if (problem.getSourceLineNumber() == problemLineNumber) {
89                                                 UnusedDeclaredThrownExceptionFilter filter = 
90                                                         new UnusedDeclaredThrownExceptionFilter(problem);
91                                                 result.removeProblems(filter);  
92                                         }
93                                 }
94                         }
95                 }
96         }
97
98         private class UnusedDeclaredThrownExceptionFilter implements ProblemsForRemovalFilter { 
99                 private IProblem problemToRemove;
100
101                 public UnusedDeclaredThrownExceptionFilter(IProblem p) {
102                         problemToRemove = p;
103                 }
104
105                 public boolean accept(IProblem p) {
106                         if (p.equals(problemToRemove)) {
107                                 return true;
108                         }
109                         return false;
110                 }
111
112         }
113 }