01: /*
02: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
03: *
04: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
05: *
06: * The contents of this file are subject to the terms of either the GNU
07: * General Public License Version 2 only ("GPL") or the Common
08: * Development and Distribution License("CDDL") (collectively, the
09: * "License"). You may not use this file except in compliance with the
10: * License. You can obtain a copy of the License at
11: * http://www.netbeans.org/cddl-gplv2.html
12: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13: * specific language governing permissions and limitations under the
14: * License. When distributing the software, include this License Header
15: * Notice in each file and include the License file at
16: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17: * particular file as subject to the "Classpath" exception as provided
18: * by Sun in the GPL Version 2 section of the License file that
19: * accompanied this code. If applicable, add the following below the
20: * License Header, with the fields enclosed by brackets [] replaced by
21: * your own identifying information:
22: * "Portions Copyrighted [year] [name of copyright owner]"
23: *
24: * Contributor(s):
25: *
26: * Portions Copyrighted 2007 Sun Microsystems, Inc.
27: */
28: package org.netbeans.modules.java.hints.spi.support;
29:
30: import com.sun.source.tree.Tree.Kind;
31: import com.sun.source.util.TreePath;
32: import java.util.EnumSet;
33: import java.util.Set;
34: import org.netbeans.api.java.source.CompilationInfo;
35: import org.netbeans.api.java.source.TreePathHandle;
36: import org.netbeans.modules.java.hints.errors.SuppressWarningsFixer;
37: import org.netbeans.spi.editor.hints.Fix;
38:
39: /** Factory for creating fixes, which add @SuppresWarning to given Element
40: *
41: * @author Petr Hrebejk
42: */
43: public final class FixFactory {
44:
45: private static final Set<Kind> DECLARATION = EnumSet.of(Kind.CLASS,
46: Kind.METHOD, Kind.VARIABLE);
47:
48: private FixFactory() {
49: }
50:
51: /** Creates a fix, which when invoked adds @SuppresWarnings(keys) to
52: * nearest declaration.
53: * @param compilationInfo CompilationInfo to work on
54: * @param treePath TreePath to a tree. The method will find nearest outer
55: * decaration. (type, method, field or local variable)
56: * @param keys keys to be contained in the SuppresWarnings annotation. E.g.
57: * @SuppresWarnings( "key" ) or @SuppresWarnings( {"key1", "key2", ..., "keyN" } ).
58: * @throws IllegalArgumentException if keys are null or empty or id no suitable element
59: * to put the annotation on is found (e.g. if TreePath to CompilationUnit is given")
60: */
61: public static Fix createSuppressWarnings(
62: CompilationInfo compilationInfo, TreePath treePath,
63: String... keys) {
64:
65: if (keys == null || keys.length == 0) {
66: throw new IllegalArgumentException(
67: "key must not be null or empty"); // NOI18N
68: }
69:
70: while (treePath.getLeaf().getKind() != Kind.COMPILATION_UNIT
71: && !DECLARATION.contains(treePath.getLeaf().getKind())) {
72: treePath = treePath.getParentPath();
73: }
74:
75: if (treePath.getLeaf().getKind() != Kind.COMPILATION_UNIT) {
76: return new SuppressWarningsFixer.FixImpl(TreePathHandle
77: .create(treePath, compilationInfo), compilationInfo
78: .getFileObject(), keys);
79: } else {
80: throw new IllegalArgumentException(
81: "Can't find Declaration for treePath " + treePath);
82: }
83: }
84:
85: }
|