comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/codewriter/FixWarningsVisitor.java @ 10597:79041ab43660

Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
author Christian Humer <christian.humer@gmail.com>
date Mon, 01 Jul 2013 20:58:32 +0200
parents
children bd28da642eea
comparison
equal deleted inserted replaced
10596:f43eb2f1bbbc 10597:79041ab43660
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.codewriter;
24
25 import static com.oracle.truffle.dsl.processor.Utils.*;
26 import static javax.lang.model.element.Modifier.*;
27
28 import java.io.*;
29 import java.util.*;
30
31 import javax.lang.model.element.*;
32 import javax.lang.model.type.*;
33
34 import com.oracle.truffle.dsl.processor.*;
35 import com.oracle.truffle.dsl.processor.ast.*;
36
37 public class FixWarningsVisitor extends CodeElementScanner<Void, Void> {
38
39 private final Set<String> symbolsUsed = new HashSet<>();
40
41 private final ProcessorContext context;
42 private final DeclaredType unusedAnnotation;
43 private final DeclaredType overrideType;
44
45 public FixWarningsVisitor(ProcessorContext context, DeclaredType unusedAnnotation, DeclaredType overrideType) {
46 this.context = context;
47 this.unusedAnnotation = unusedAnnotation;
48 this.overrideType = overrideType;
49 }
50
51 @Override
52 public Void visitType(CodeTypeElement e, Void p) {
53 List<TypeElement> superTypes = Utils.getSuperTypes(e);
54 for (TypeElement type : superTypes) {
55 String qualifiedName = Utils.getQualifiedName(type);
56 if (qualifiedName.equals(Serializable.class.getCanonicalName())) {
57 if (!e.containsField("serialVersionUID")) {
58 e.add(new CodeVariableElement(modifiers(PRIVATE, STATIC, FINAL), context.getType(long.class), "serialVersionUID", "1L"));
59 }
60 break;
61 }
62 }
63
64 return super.visitType(e, p);
65 }
66
67 @Override
68 public Void visitExecutable(CodeExecutableElement e, Void p) {
69 if (e.getParameters().isEmpty()) {
70 return null;
71 } else if (e.getModifiers().contains(Modifier.ABSTRACT)) {
72 return null;
73 } else if (containsOverride(e)) {
74 return null;
75 }
76
77 symbolsUsed.clear();
78 super.visitExecutable(e, p);
79 if (e.getBodyTree() == null && e.getBody() != null) {
80 computeSymbols(e.getBody());
81 }
82
83 for (VariableElement parameter : e.getParameters()) {
84 if (!symbolsUsed.contains(parameter.getSimpleName().toString())) {
85 e.getAnnotationMirrors().add(createUnusedAnnotationMirror());
86 break;
87 }
88 }
89 return null;
90 }
91
92 private boolean containsOverride(CodeExecutableElement e) {
93 for (AnnotationMirror mirror : e.getAnnotationMirrors()) {
94 if (Utils.typeEquals(overrideType, mirror.getAnnotationType())) {
95 return true;
96 }
97 }
98 return false;
99 }
100
101 private CodeAnnotationMirror createUnusedAnnotationMirror() {
102 CodeAnnotationMirror mirror = new CodeAnnotationMirror(unusedAnnotation);
103 mirror.setElementValue(mirror.findExecutableElement("value"), new CodeAnnotationValue("unused"));
104 return mirror;
105 }
106
107 @Override
108 public void visitTree(CodeTree e, Void p) {
109 if (e.getString() != null) {
110 computeSymbols(e.getString());
111 }
112 super.visitTree(e, p);
113 }
114
115 private void computeSymbols(String s) {
116 // TODO there should not be any need for a StringTokenizer if we have a real AST for
117 // method bodies. Also the current solution is not perfect. What if one token
118 // is spread across multiple CodeTree instances? But for now that works.
119 StringTokenizer tokenizer = new StringTokenizer(s, ".= :,()[];{}\"\"'' ", false);
120 while (tokenizer.hasMoreElements()) {
121 String token = tokenizer.nextToken().trim();
122 if (token.length() > 0) {
123 symbolsUsed.add(token);
124 }
125 }
126 }
127
128 }