ilia@18417
|
1 |
/*
|
ilia@18417
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
ilia@18417
|
3 |
*
|
ilia@18417
|
4 |
* Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
|
ilia@18417
|
5 |
*
|
ilia@18417
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
ilia@18417
|
7 |
* Other names may be trademarks of their respective owners.
|
ilia@18417
|
8 |
*
|
ilia@18417
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
ilia@18417
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
ilia@18417
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
ilia@18417
|
12 |
* "License"). You may not use this file except in compliance with the
|
ilia@18417
|
13 |
* License. You can obtain a copy of the License at
|
ilia@18417
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
ilia@18417
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
ilia@18417
|
16 |
* specific language governing permissions and limitations under the
|
ilia@18417
|
17 |
* License. When distributing the software, include this License Header
|
ilia@18417
|
18 |
* Notice in each file and include the License file at
|
ilia@18417
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
ilia@18417
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
ilia@18417
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
ilia@18417
|
22 |
* accompanied this code. If applicable, add the following below the
|
ilia@18417
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
ilia@18417
|
24 |
* your own identifying information:
|
ilia@18417
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
ilia@18417
|
26 |
*
|
ilia@18417
|
27 |
* If you wish your version of this file to be governed by only the CDDL
|
ilia@18417
|
28 |
* or only the GPL Version 2, indicate your decision by adding
|
ilia@18417
|
29 |
* "[Contributor] elects to include this software in this distribution
|
ilia@18417
|
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
ilia@18417
|
31 |
* single choice of license, a recipient has the option to distribute
|
ilia@18417
|
32 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
ilia@18417
|
33 |
* to extend the choice of license to its licensees as provided above.
|
ilia@18417
|
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
ilia@18417
|
35 |
* Version 2 license, then the option applies only if the new code is
|
ilia@18417
|
36 |
* made subject to such option by the copyright holder.
|
ilia@18417
|
37 |
*
|
ilia@18417
|
38 |
* Contributor(s):
|
ilia@18417
|
39 |
*/
|
ilia@18417
|
40 |
package com.sun.tools.ide.analysis.modernize.impl;
|
ilia@18417
|
41 |
|
ilia@18417
|
42 |
import com.sun.tools.ide.analysis.modernize.impl.ModernizeAnalyzerImpl.ModernizeResponse;
|
ilia@18417
|
43 |
import java.util.ArrayList;
|
ilia@18417
|
44 |
import java.util.HashMap;
|
ilia@18417
|
45 |
import java.util.List;
|
ilia@18417
|
46 |
import java.util.Map;
|
ilia@18417
|
47 |
import java.util.Optional;
|
ilia@18417
|
48 |
import java.util.stream.Collectors;
|
ilia@18417
|
49 |
import org.netbeans.modules.cnd.analysis.api.AnalyzerResponse;
|
ilia@18417
|
50 |
import org.netbeans.spi.editor.hints.ErrorDescription;
|
ilia@18417
|
51 |
import org.openide.filesystems.FileObject;
|
ilia@18417
|
52 |
|
ilia@18417
|
53 |
/**
|
ilia@18417
|
54 |
*
|
ilia@18417
|
55 |
* @author Ilia Gromov
|
ilia@18417
|
56 |
*/
|
ilia@18417
|
57 |
public class AnalyzerResponseMerger {
|
ilia@18417
|
58 |
|
ilia@18417
|
59 |
private static class ErrorDesc {
|
ilia@18417
|
60 |
|
ilia@18417
|
61 |
final ModernizeErrorInfo info;
|
ilia@18417
|
62 |
final FileObject fo;
|
ilia@18417
|
63 |
|
ilia@18417
|
64 |
public ErrorDesc(ModernizeErrorInfo info, FileObject fo) {
|
ilia@18417
|
65 |
this.info = info;
|
ilia@18417
|
66 |
this.fo = fo;
|
ilia@18417
|
67 |
}
|
ilia@18417
|
68 |
|
ilia@18417
|
69 |
public boolean isSame(ErrorDesc o2) {
|
ilia@18417
|
70 |
return isSame(this.info, o2.info) && this.fo.equals(o2.fo);
|
ilia@18417
|
71 |
}
|
ilia@18417
|
72 |
|
ilia@18417
|
73 |
public boolean isSame(ModernizeErrorInfo o1, ModernizeErrorInfo o2) {
|
ilia@18417
|
74 |
return o1.getStartOffset() == o2.getStartOffset()
|
ilia@18417
|
75 |
&& o1.getEndOffset() == o2.getEndOffset()
|
ilia@18417
|
76 |
&& o1.getDiagnostics().getCheckName().equals(o2.getDiagnostics().getCheckName());
|
ilia@18417
|
77 |
}
|
ilia@18417
|
78 |
}
|
ilia@18417
|
79 |
|
ilia@18417
|
80 |
private final List<ErrorDesc> errors = new ArrayList<>();
|
ilia@18417
|
81 |
private final ModernizeResponse delegate;
|
ilia@18417
|
82 |
|
ilia@18417
|
83 |
public AnalyzerResponseMerger(ModernizeResponse delegate) {
|
ilia@18417
|
84 |
this.delegate = delegate;
|
ilia@18417
|
85 |
}
|
ilia@18417
|
86 |
|
ilia@18417
|
87 |
public void addError(ModernizeErrorInfo info, FileObject fo) {
|
ilia@18417
|
88 |
ErrorDesc errorDesc = new ErrorDesc(info, fo);
|
ilia@18417
|
89 |
Optional<ErrorDesc> found = errors.stream()
|
ilia@18417
|
90 |
.filter(o1 -> o1.isSame(errorDesc))
|
ilia@18417
|
91 |
.findAny();
|
ilia@18417
|
92 |
|
ilia@18417
|
93 |
if (found.isPresent()) {
|
ilia@18417
|
94 |
found.get().info.addMessageInfixes(errorDesc.info.getMessageInfixes());
|
ilia@18417
|
95 |
} else {
|
ilia@18417
|
96 |
errors.add(errorDesc);
|
ilia@18417
|
97 |
}
|
ilia@18417
|
98 |
}
|
ilia@18417
|
99 |
|
ilia@18417
|
100 |
public List<ErrorDescription> done() {
|
ilia@18417
|
101 |
return errors.stream()
|
ilia@18417
|
102 |
.map(error -> delegate.addErrorImpl(error.info, error.fo))
|
ilia@18417
|
103 |
.collect(Collectors.toList());
|
ilia@18417
|
104 |
}
|
ilia@18417
|
105 |
}
|