langtools/test/tools/javac/processing/rounds/MethodsDroppedBetweenRounds.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 23810 b92eb80925f0
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 23810
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     4
 *
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     8
 *
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    14
 *
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    18
 *
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    21
 * questions.
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    22
 */
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    23
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    24
/*
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    25
 * @test
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    26
 * @bug 8038455
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    27
 * @summary Ensure that Symbols for members (methods and fields) are dropped across annotation
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    28
 *          processing rounds. ClassSymbols need to be kept.
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    29
 * @library /tools/javac/lib
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 23810
diff changeset
    30
 * @modules java.compiler
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 23810
diff changeset
    31
 *          jdk.compiler
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    32
 * @build JavacTestingAbstractProcessor MethodsDroppedBetweenRounds
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    33
 * @compile/process -processor MethodsDroppedBetweenRounds MethodsDroppedBetweenRounds.java
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    34
 */
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    35
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    36
import java.lang.ref.WeakReference;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    37
import java.util.*;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    38
import javax.annotation.processing.*;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    39
import javax.lang.model.element.*;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    40
import javax.lang.model.util.ElementFilter;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    41
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    42
public class MethodsDroppedBetweenRounds extends JavacTestingAbstractProcessor {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    43
    private static TypeElement currentClassSymbol;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    44
    private static WeakReference<ExecutableElement> keptMethod = null;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    45
    public boolean process(Set<? extends TypeElement> annos,RoundEnvironment rEnv) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    46
        if (keptMethod != null) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    47
            //force GC:
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    48
            List<byte[]> hold = new ArrayList<>();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    49
            try {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    50
                while (true)
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    51
                    hold.add(new byte[1024 * 1024 * 1024]);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    52
            } catch (OutOfMemoryError err) { }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    53
            hold.clear();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    54
            if (keptMethod.get() != null) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    55
                throw new IllegalStateException("Holds method across rounds.");
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    56
            }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    57
        }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    58
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    59
        TypeElement currentClass = elements.getTypeElement("MethodsDroppedBetweenRounds");
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    60
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    61
        if (currentClassSymbol != null && currentClassSymbol != currentClass) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    62
            throw new IllegalStateException("Different ClassSymbols across rounds");
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    63
        }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    64
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    65
        ExecutableElement method = ElementFilter.methodsIn(currentClass.getEnclosedElements()).get(0);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    66
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    67
        keptMethod = new WeakReference<>(method);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    68
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    69
        return true;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    70
    }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    71
}