Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/transform/FixWarningsVisitor.java @ 21951:9c8c0937da41
Moving all sources into truffle subdirectory
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 17 Jun 2015 10:58:08 +0200 |
parents | graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/transform/FixWarningsVisitor.java@62cfffca9be2 |
children | dc83cc1f94f2 |
comparison
equal
deleted
inserted
replaced
21950:2a5011c7e641 | 21951:9c8c0937da41 |
---|---|
1 /* | |
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.dsl.processor.java.transform; | |
24 | |
25 import static com.oracle.truffle.dsl.processor.java.ElementUtils.*; | |
26 import static javax.lang.model.element.Modifier.*; | |
27 | |
28 import java.io.*; | |
29 import java.util.*; | |
30 | |
31 import javax.annotation.processing.*; | |
32 import javax.lang.model.element.*; | |
33 import javax.lang.model.type.*; | |
34 | |
35 import com.oracle.truffle.dsl.processor.java.*; | |
36 import com.oracle.truffle.dsl.processor.java.model.*; | |
37 | |
38 public class FixWarningsVisitor extends CodeElementScanner<Void, Void> { | |
39 | |
40 private final Set<String> symbolsUsed = new HashSet<>(); | |
41 | |
42 private final ProcessingEnvironment processingEnv; | |
43 private final DeclaredType unusedAnnotation; | |
44 private final DeclaredType overrideType; | |
45 | |
46 public FixWarningsVisitor(ProcessingEnvironment processingEnv, DeclaredType unusedAnnotation, DeclaredType overrideType) { | |
47 this.processingEnv = processingEnv; | |
48 this.unusedAnnotation = unusedAnnotation; | |
49 this.overrideType = overrideType; | |
50 } | |
51 | |
52 @Override | |
53 public Void visitType(CodeTypeElement e, Void p) { | |
54 List<TypeElement> superTypes = ElementUtils.getSuperTypes(e); | |
55 for (TypeElement type : superTypes) { | |
56 String qualifiedName = ElementUtils.getQualifiedName(type); | |
57 if (qualifiedName.equals(Serializable.class.getCanonicalName())) { | |
58 if (!e.containsField("serialVersionUID")) { | |
59 e.add(new CodeVariableElement(modifiers(PRIVATE, STATIC, FINAL), ElementUtils.getType(processingEnv, long.class), "serialVersionUID", "1L")); | |
60 } | |
61 break; | |
62 } | |
63 } | |
64 | |
65 return super.visitType(e, p); | |
66 } | |
67 | |
68 @Override | |
69 public Void visitExecutable(CodeExecutableElement e, Void p) { | |
70 if (e.getParameters().isEmpty()) { | |
71 return null; | |
72 } else if (e.getModifiers().contains(Modifier.ABSTRACT)) { | |
73 return null; | |
74 } else if (containsOverride(e)) { | |
75 return null; | |
76 } | |
77 | |
78 symbolsUsed.clear(); | |
79 super.visitExecutable(e, p); | |
80 | |
81 for (VariableElement parameter : e.getParameters()) { | |
82 if (!symbolsUsed.contains(parameter.getSimpleName().toString())) { | |
83 e.getAnnotationMirrors().add(createUnusedAnnotationMirror()); | |
84 break; | |
85 } | |
86 } | |
87 return null; | |
88 } | |
89 | |
90 private boolean containsOverride(CodeExecutableElement e) { | |
91 for (AnnotationMirror mirror : e.getAnnotationMirrors()) { | |
92 if (ElementUtils.typeEquals(overrideType, mirror.getAnnotationType())) { | |
93 return true; | |
94 } | |
95 } | |
96 return false; | |
97 } | |
98 | |
99 private CodeAnnotationMirror createUnusedAnnotationMirror() { | |
100 CodeAnnotationMirror mirror = new CodeAnnotationMirror(unusedAnnotation); | |
101 mirror.setElementValue(mirror.findExecutableElement("value"), new CodeAnnotationValue("unused")); | |
102 return mirror; | |
103 } | |
104 | |
105 @Override | |
106 public void visitTree(CodeTree e, Void p, Element enclosingElement) { | |
107 if (e.getString() != null) { | |
108 computeSymbols(e.getString()); | |
109 } | |
110 super.visitTree(e, p, enclosingElement); | |
111 } | |
112 | |
113 private void computeSymbols(String s) { | |
114 // TODO there should not be any need for a StringTokenizer if we have a real AST for | |
115 // method bodies. Also the current solution is not perfect. What if one token | |
116 // is spread across multiple CodeTree instances? But for now that works. | |
117 StringTokenizer tokenizer = new StringTokenizer(s, ".= :,()[];{}\"\"'' ", false); | |
118 while (tokenizer.hasMoreElements()) { | |
119 String token = tokenizer.nextToken().trim(); | |
120 if (token.length() > 0) { | |
121 symbolsUsed.add(token); | |
122 } | |
123 } | |
124 } | |
125 | |
126 } |