View | Details | Raw Unified | Return to bug 328575 | Differences between
and this patch

Collapse All | Expand All

(-)processors/META-INF/services/javax.annotation.processing.Processor (+1 lines)
Lines 10-12 Link Here
10
org.eclipse.jdt.compiler.apt.tests.processors.typemirror.TypeMirrorProc
10
org.eclipse.jdt.compiler.apt.tests.processors.typemirror.TypeMirrorProc
11
org.eclipse.jdt.compiler.apt.tests.processors.typeutils.TypeUtilsProc
11
org.eclipse.jdt.compiler.apt.tests.processors.typeutils.TypeUtilsProc
12
org.eclipse.jdt.compiler.apt.tests.processors.negative.NegativeModelProc
12
org.eclipse.jdt.compiler.apt.tests.processors.negative.NegativeModelProc
13
org.eclipse.jdt.compiler.apt.tests.processors.inherited.ArgsConstructorProcessor
(-)processors/org/eclipse/jdt/compiler/apt/tests/annotations/ArgsConstructor.java (+28 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jdt.compiler.apt.tests.annotations;
12
13
import java.lang.annotation.Documented;
14
import java.lang.annotation.Inherited;
15
import java.lang.annotation.Retention;
16
import java.lang.annotation.Target;
17
18
import static java.lang.annotation.ElementType.TYPE;
19
import static java.lang.annotation.RetentionPolicy.SOURCE;
20
21
@Inherited
22
@Documented
23
@Retention(SOURCE)
24
@Target(TYPE)
25
public @interface ArgsConstructor {
26
27
	Class<?>[] value();
28
}
(-)processors/org/eclipse/jdt/compiler/apt/tests/processors/inherited/ArgsConstructorProcessor.java (+151 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jdt.compiler.apt.tests.processors.inherited;
12
13
import java.util.ArrayList;
14
import java.util.List;
15
import java.util.Map.Entry;
16
import java.util.Set;
17
18
import javax.annotation.processing.AbstractProcessor;
19
import javax.annotation.processing.RoundEnvironment;
20
import javax.lang.model.element.Element;
21
import javax.lang.model.element.TypeElement;
22
import javax.annotation.processing.*;
23
import javax.lang.model.SourceVersion;
24
import javax.lang.model.element.*;
25
import javax.lang.model.type.*;
26
import javax.lang.model.util.SimpleTypeVisitor6;
27
import javax.lang.model.util.Types;
28
import javax.tools.Diagnostic.Kind;
29
30
import org.eclipse.jdt.compiler.apt.tests.annotations.ArgsConstructor;
31
32
@SupportedAnnotationTypes("org.eclipse.jdt.compiler.apt.tests.annotations.ArgsConstructor")
33
@SupportedSourceVersion(SourceVersion.RELEASE_6)
34
public class ArgsConstructorProcessor extends AbstractProcessor {
35
36
	@Override
37
	public boolean process(Set<? extends TypeElement> annotations,
38
			RoundEnvironment env) {
39
40
		for (TypeElement type : annotations) {
41
			processArgsConstructorClasses(env, type);
42
		}
43
		return true;
44
45
	}
46
47
	private void processArgsConstructorClasses(RoundEnvironment env,
48
			TypeElement type) {
49
		for (Element element : env.getElementsAnnotatedWith(type)) {
50
			processClass(element);
51
			processingEnv.getMessager().printMessage(Kind.NOTE,
52
					"Class " + element + " is processed");
53
		}
54
	}
55
56
	private void processClass(Element element) {
57
58
		String actionName = ArgsConstructor.class.getName();
59
		AnnotationValue action = null;
60
		for (AnnotationMirror am : processingEnv.getElementUtils()
61
				.getAllAnnotationMirrors(element)) {
62
			if (actionName.equals(am.getAnnotationType().toString())) {
63
				for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : am
64
						.getElementValues().entrySet()) {
65
					if ("value".equals(entry.getKey().getSimpleName()
66
							.toString())) {
67
						action = entry.getValue();
68
						break;
69
					}
70
71
				}
72
73
			}
74
		}
75
76
		if (action == null) {
77
			processingEnv.getMessager()
78
					.printMessage(
79
							Kind.WARNING,
80
							"Class " + element
81
									+ " lacks a annotation with required args",
82
							element);
83
			return;
84
		}
85
86
		List<TypeMirror> mirrors = new ArrayList<TypeMirror>();
87
		for (Object val : (List<?>) action.getValue()) {
88
			AnnotationValue v = (AnnotationValue) val;
89
			TypeMirror m = (TypeMirror) v.getValue();
90
			mirrors.add(m);
91
		}
92
93
		if (!doesClassContainArgsConstructor(element, mirrors)) {
94
			String s = "";
95
			for (TypeMirror tm : mirrors) {
96
				if (!s.isEmpty()) {
97
					s += ",";
98
				}
99
				s += tm.toString();
100
101
			}
102
			processingEnv.getMessager().printMessage(
103
					Kind.ERROR,
104
					"Class " + element
105
							+ " lacks a public constructor with args: " + s,
106
					element);
107
		} else {
108
			processingEnv.getMessager().printMessage(Kind.NOTE,
109
					"Processed type: " + element);
110
		}
111
	}
112
113
	private boolean doesClassContainArgsConstructor(Element el,
114
			List<TypeMirror> annotTypes) {
115
		for (Element subelement : el.getEnclosedElements()) {
116
			if (subelement.getKind() == ElementKind.CONSTRUCTOR
117
					&& subelement.getModifiers().contains(Modifier.PUBLIC)) {
118
				TypeMirror mirror = subelement.asType();
119
				if (mirror.accept(argsVisitor, annotTypes))
120
					return true;
121
			}
122
		}
123
		return false;
124
	}
125
126
	private final TypeVisitor<Boolean, List<TypeMirror>> argsVisitor = new SimpleTypeVisitor6<Boolean, List<TypeMirror>>() {
127
		public Boolean visitExecutable(ExecutableType t,
128
				List<TypeMirror> annotatedTypes) {
129
130
			List<? extends TypeMirror> types = t.getParameterTypes();
131
			if (annotatedTypes.size() != types.size()) {
132
				return false;
133
			}
134
			Types tutil = processingEnv.getTypeUtils();
135
136
			for (int i = 0; i < types.size(); i++) {
137
				TypeMirror test = tutil.erasure(types.get(i));// because same
138
																// type bad
139
																// Map<String,String>
140
																// != Map
141
				TypeMirror expected = tutil.erasure(annotatedTypes.get(i));
142
143
				if (!tutil.isAssignable(expected, test)) {
144
					return false;
145
				}
146
			}
147
			return true;
148
		}
149
	};
150
151
}
(-)resources/targets/inherited/TestGeneric.java (+22 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package targets.inherited;
12
13
import java.awt.Point;
14
15
import org.eclipse.jdt.compiler.apt.tests.annotations.ArgsConstructor;
16
17
@ArgsConstructor({ Point.class })
18
public class TestGeneric<K extends Point> {
19
20
	public TestGeneric(K k) {
21
	}
22
}
(-)resources/targets/inherited/TestGenericChild.java (+19 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package targets.inherited;
12
13
public class TestGenericChild extends TestGeneric<TestPoint> {
14
15
	public TestGenericChild(int i, TestPoint k) {
16
		super(k);
17
	}
18
19
}
(-)resources/targets/inherited/TestNormal.java (+22 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package targets.inherited;
12
13
import java.awt.Point;
14
15
import org.eclipse.jdt.compiler.apt.tests.annotations.ArgsConstructor;
16
17
@ArgsConstructor({ Point.class })
18
public class TestNormal {
19
20
	public TestNormal(Point p) {
21
	}
22
}
(-)resources/targets/inherited/TestNormalChild.java (+20 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package targets.inherited;
12
13
import java.awt.Point;
14
15
public class TestNormalChild extends TestNormal {
16
17
	public TestNormalChild(Point p, int i) {
18
		super(p);
19
	}
20
}
(-)resources/targets/inherited/TestPoint.java (+17 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package targets.inherited;
12
13
import java.awt.Point;
14
15
public class TestPoint extends Point {
16
17
}
(-)src/org/eclipse/jdt/compiler/apt/tests/NegativeTests.java (-3 / +52 lines)
Lines 12-19 Link Here
12
12
13
package org.eclipse.jdt.compiler.apt.tests;
13
package org.eclipse.jdt.compiler.apt.tests;
14
14
15
import java.io.ByteArrayOutputStream;
15
import java.io.File;
16
import java.io.File;
16
import java.io.IOException;
17
import java.io.IOException;
18
import java.io.PrintWriter;
17
import java.util.ArrayList;
19
import java.util.ArrayList;
18
import java.util.Collection;
20
import java.util.Collection;
19
import java.util.Collections;
21
import java.util.Collections;
Lines 21-27 Link Here
21
import java.util.Set;
23
import java.util.Set;
22
24
23
import javax.lang.model.SourceVersion;
25
import javax.lang.model.SourceVersion;
26
import javax.tools.Diagnostic;
27
import javax.tools.DiagnosticListener;
24
import javax.tools.JavaCompiler;
28
import javax.tools.JavaCompiler;
29
import javax.tools.JavaFileObject;
25
import javax.tools.ToolProvider;
30
import javax.tools.ToolProvider;
26
31
27
import junit.framework.TestCase;
32
import junit.framework.TestCase;
Lines 29-36 Link Here
29
/**
34
/**
30
 * Test cases for annotation processing behavior when code contains semantic errors
35
 * Test cases for annotation processing behavior when code contains semantic errors
31
 */
36
 */
32
public class NegativeTests extends TestCase
37
public class NegativeTests extends TestCase {
33
{
38
	static class TestDiagnosticListener implements DiagnosticListener<JavaFileObject> {
39
		public static final int NONE = 0;
40
		public static final int ERROR = 1;
41
		public static final int INFO = 2;
42
		public static final int WARNING = 4;
43
		
44
		private PrintWriter err;
45
		public int errorCounter;
46
		
47
		public TestDiagnosticListener(PrintWriter err) {
48
			this.err = err;
49
		}
50
		@Override
51
		public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
52
			err.println(diagnostic);
53
			switch(diagnostic.getKind()) {
54
				case ERROR :
55
					this.errorCounter++;
56
					break;
57
			}
58
		}
59
	}
60
34
	// See corresponding usages in the NegativeModelProc class
61
	// See corresponding usages in the NegativeModelProc class
35
	private static final String NEGATIVEMODELPROCNAME = "org.eclipse.jdt.compiler.apt.tests.processors.negative.NegativeModelProc";
62
	private static final String NEGATIVEMODELPROCNAME = "org.eclipse.jdt.compiler.apt.tests.processors.negative.NegativeModelProc";
36
	private static final String IGNOREJAVACBUGS = "ignoreJavacBugs";
63
	private static final String IGNOREJAVACBUGS = "ignoreJavacBugs";
Lines 149-154 Link Here
149
		JavaCompiler compiler = BatchTestUtils.getEclipseCompiler();
176
		JavaCompiler compiler = BatchTestUtils.getEclipseCompiler();
150
		internalTestNegativeModel(compiler, 9, null);
177
		internalTestNegativeModel(compiler, 9, null);
151
	}
178
	}
179
180
	/*
181
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=328575 
182
	 */
183
	public void testNegativeModel10WithEclipseCompiler() throws IOException {
184
		JavaCompiler compiler = BatchTestUtils.getEclipseCompiler();
185
		System.clearProperty(NEGATIVEMODELPROCNAME);
186
		File targetFolder = TestUtils.concatPath(BatchTestUtils.getSrcFolderName(), "targets", "inherited");
187
		BatchTestUtils.copyResources("targets/inherited", targetFolder);
188
189
		// Turn on the NegativeModelProc - without this, it will just return without doing anything
190
		List<String> options = new ArrayList<String>();
191
192
		// Invoke processing by compiling the targets.model resources
193
		ByteArrayOutputStream errBuffer = new ByteArrayOutputStream();
194
		PrintWriter printWriter = new PrintWriter(errBuffer);
195
		TestDiagnosticListener diagnosticListener = new TestDiagnosticListener(printWriter);
196
		boolean success = BatchTestUtils.compileTreeWithErrors(compiler, options, targetFolder, diagnosticListener);
197
		
198
		assertTrue("Compilation should have failed due to expected errors, but it didn't", !success);
199
		assertEquals("Two errors should be reported", 2, diagnosticListener.errorCounter);
200
	}
201
152
	/**
202
	/**
153
	 * Attempt to report errors on various elements.
203
	 * Attempt to report errors on various elements.
154
	 * @throws IOException
204
	 * @throws IOException
Lines 168-174 Link Here
168
		boolean success = BatchTestUtils.compileTreeWithErrors(compiler, options, targetFolder, null);
218
		boolean success = BatchTestUtils.compileTreeWithErrors(compiler, options, targetFolder, null);
169
		
219
		
170
		assertTrue("Compilation should have failed due to expected errors, but it didn't", !success);
220
		assertTrue("Compilation should have failed due to expected errors, but it didn't", !success);
171
172
		// If it succeeded, the processor will have set this property to "succeeded";
221
		// If it succeeded, the processor will have set this property to "succeeded";
173
		// if not, it will set it to an error value.
222
		// if not, it will set it to an error value.
174
		String property = System.getProperty(NEGATIVEMODELPROCNAME);
223
		String property = System.getProperty(NEGATIVEMODELPROCNAME);
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedTypeBinding.java (-2 / +6 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2010 IBM Corporation and others.
2
 * Copyright (c) 2005, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 438-446 Link Here
438
	 * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#getAnnotationTagBits()
438
	 * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#getAnnotationTagBits()
439
	 */
439
	 */
440
	public long getAnnotationTagBits() {
440
	public long getAnnotationTagBits() {
441
		return this.type.getAnnotationTagBits();
441
		return genericType().getAnnotationTagBits();
442
	}
442
	}
443
443
444
	public AnnotationBinding[] getAnnotations() {
445
		return genericType().getAnnotations();
446
	}
447
	
444
	public int getEnclosingInstancesSlotSize() {
448
	public int getEnclosingInstancesSlotSize() {
445
		return genericType().getEnclosingInstancesSlotSize();
449
		return genericType().getEnclosingInstancesSlotSize();
446
	}
450
	}

Return to bug 328575