Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.api.java.source;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.Scope;
import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;
import com.sun.tools.javac.api.JavacScope;
import com.sun.tools.javac.api.JavacTaskImpl;
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Source;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.Enter;
import com.sun.tools.javac.comp.Env;
import com.sun.tools.javac.comp.Resolve;
import com.sun.tools.javac.model.JavacElements;
import com.sun.tools.javac.model.JavacTypes;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Names;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.RecordComponentElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVariable;
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.lang.model.util.SimpleElementVisitor9;
import javax.lang.model.util.Types;
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.api.annotations.common.NonNull;
import org.netbeans.api.annotations.common.NullAllowed;
import org.netbeans.modules.java.source.builder.ElementsService;
import org.netbeans.modules.java.source.base.SourceLevelUtils;
import org.openide.util.Parameters;
/**
*
* @author Jan Lahoda, Dusan Balek, Tomas Zezula
*/
public final class ElementUtilities {
private static final ElementAcceptor ALL_ACCEPTOR = (Element e, TypeMirror type) -> true;
private final Context ctx;
private final ElementsService delegate;
private final CompilationInfo info;
/** Creates a new instance of ElementUtilities */
ElementUtilities(@NonNull final CompilationInfo info) {
this((JavacTaskImpl)info.impl.getJavacTask(), info);
}
ElementUtilities(@NonNull final JavacTaskImpl jt) {
this(jt, null);
}
private ElementUtilities(@NonNull final JavacTaskImpl jt, @NullAllowed final CompilationInfo info) {
this.ctx = jt.getContext();
this.delegate = ElementsService.instance(ctx);
this.info = info;
}
/**
* Returns the type element within which this member or constructor
* is declared. Does not accept packages
* If this is the declaration of a top-level type (a non-nested class
* or interface), returns null.
*
* @return the type declaration within which this member or constructor
* is declared, or null if there is none
* @throws IllegalArgumentException if the provided element is a package element
*/
public TypeElement enclosingTypeElement( Element element ) throws IllegalArgumentException {
return enclosingTypeElementImpl(element);
}
static TypeElement enclosingTypeElementImpl( Element element ) throws IllegalArgumentException {
if( element.getKind() == ElementKind.PACKAGE ) {
throw new IllegalArgumentException();
}
element = element.getEnclosingElement();
if (element.getKind() == ElementKind.PACKAGE) {
//element is a top level class, returning null according to the contract:
return null;
}
while(element != null && !(element.getKind().isClass() || element.getKind().isInterface())) {
element = element.getEnclosingElement();
}
return (TypeElement)element;
}
/**
*
* The outermost TypeElement which indirectly encloses this element.
*/
public TypeElement outermostTypeElement(Element element) {
return delegate.outermostTypeElement(element);
}
/**
* Returns the implementation of a method in class origin; null if none exists.
*/
public Element getImplementationOf(ExecutableElement method, TypeElement origin) {
return delegate.getImplementationOf(method, origin);
}
/**Returns true if the given element is synthetic.
*
* @param element to check
* @return true if and only if the given element is synthetic, false otherwise
*/
public boolean isSynthetic(Element element) {
return (((Symbol) element).flags() & Flags.SYNTHETIC) != 0 || (((Symbol) element).flags() & Flags.GENERATEDCONSTR) != 0;
}
/**Returns true if the given module is open.
*
* @param element to check
* @return true if and only if the given module is open, false otherwise
*/
public boolean isOpen(ModuleElement element) {
return ((Symbol.ModuleSymbol) element).flags.contains(Symbol.ModuleFlags.OPEN);
}
/**
* Returns true if this element represents a method which overrides a
* method in one of its superclasses.
*/
public boolean overridesMethod(ExecutableElement element) {
return delegate.overridesMethod(element);
}
/**
* Returns a binary name of a type.
* @param element for which the binary name should be returned
* @return the binary name, see Java Language Specification 13.1
* @throws IllegalArgumentException when the element is not a javac element
*/
public static String getBinaryName (TypeElement element) throws IllegalArgumentException {
if (element instanceof Symbol.TypeSymbol) {
return ((Symbol.TypeSymbol)element).flatName().toString();
}
else {
throw new IllegalArgumentException ();
}
}
/**
* Returns all members of a type, whether inherited or
* declared directly. For a class the result also includes its
* constructors, but not local or anonymous classes.
*
* @param type the type being examined
* @param acceptor to filter the members
* @return all members in the type
* @see Elements#getAllMembers
*/
public Iterable extends Element> getMembers(TypeMirror type, ElementAcceptor acceptor) {
List membersList = new ArrayList<>();
Map> membersMap = new HashMap<>();
if (type != null) {
if (acceptor == null) {
acceptor = ALL_ACCEPTOR;
}
Elements elements = JavacElements.instance(ctx);
Types types = JavacTypes.instance(ctx);
switch (type.getKind()) {
case DECLARED:
case UNION:
case INTERSECTION:
TypeElement te = (TypeElement)((DeclaredType)type).asElement();
if (te == null) break;
for (Element member : elements.getAllMembers(te)) {
if (acceptor.accept(member, type)) {
addIfNotHidden(member, membersList, membersMap, elements, types);
}
}
if (te.getKind().isClass() || te.getKind().isInterface() && SourceLevelUtils.allowDefaultMethods(Source.instance(ctx))) {
VarSymbol thisPseudoMember = new VarSymbol(Flags.FINAL | Flags.HASINIT, Names.instance(ctx)._this, (ClassType)te.asType(), (ClassSymbol)te);
if (acceptor.accept(thisPseudoMember, type)) {
addAlways(thisPseudoMember, membersList, membersMap);
}
if (te.getSuperclass().getKind() == TypeKind.DECLARED) {
VarSymbol superPseudoMember = new VarSymbol(Flags.FINAL | Flags.HASINIT, Names.instance(ctx)._super, (ClassType)te.getSuperclass(), (ClassSymbol)te);
if (acceptor.accept(superPseudoMember, type)) {
addAlways(superPseudoMember, membersList, membersMap);
}
}
}
case BOOLEAN:
case BYTE:
case CHAR:
case DOUBLE:
case FLOAT:
case INT:
case LONG:
case SHORT:
case VOID:
Type t = Symtab.instance(ctx).classType;
com.sun.tools.javac.util.List typeargs = com.sun.tools.javac.util.List.of((Type)type);
t = new ClassType(t.getEnclosingType(), typeargs, t.tsym);
Element classPseudoMember = new VarSymbol(Flags.STATIC | Flags.PUBLIC | Flags.FINAL, Names.instance(ctx)._class, t, ((Type)type).tsym);
if (acceptor.accept(classPseudoMember, type)) {
addAlways(classPseudoMember, membersList, membersMap);
}
break;
case ARRAY:
for (Element member : elements.getAllMembers((TypeElement)((Type)type).tsym)) {
if (acceptor.accept(member, type)) {
addAlways(member, membersList, membersMap);
}
}
t = Symtab.instance(ctx).classType;
typeargs = com.sun.tools.javac.util.List.of((Type)type);
t = new ClassType(t.getEnclosingType(), typeargs, t.tsym);
classPseudoMember = new VarSymbol(Flags.STATIC | Flags.PUBLIC | Flags.FINAL, Names.instance(ctx)._class, t, ((Type)type).tsym);
if (acceptor.accept(classPseudoMember, type)) {
addAlways(classPseudoMember, membersList, membersMap);
}
break;
}
}
return membersList;
}
/**
* Finds symbols which satisfy the acceptor visible in the passed scope. The method returns a Map keyed by the
* found Elements. Each Element is mapped to the closest Scope which introduced the Element. For example, a field declared
* by an outer class will map to that outer class' scope. An accessible field inherited from outer class' superclass
* will also map to the outer class' scope. The caller can then determine, based on {@link Element#getEnclosingElement()} and
* the mapped Scope whether the symbol is directly declared, or inherited. Non-member symbols (variables, parameters, try resources, ...)
* map to Scope of their defining Method.
*
* If an Element from outer Scope is hidden by a similar Element
* in inner scope, only the Element visible to the passed Scope is returned. For example, if both the starting (inner) class and its outer class
* define method m(), only InnerClass.m() will be returned.
*
* Note that {@link Scope#getEnclosingMethod()} returns non-null even for class scopes of local or anonymous classes; check both {@link Scope#getEnclosingClass()}
* and {@link Scope#getEnclosingMethod()} and their relationship to get the appropriate Element associated with the Scope.
*
* @param scope the initial search scope
* @param acceptor the element filter.
* @return Mapping of visible and accessible Elements to their defining {@link Scope}s (which introduced them).
* @see Scope
* @since 2.16
*/
public @NonNull Map extends Element, Scope> findElementsAndOrigins(@NonNull Scope scope, ElementAcceptor acceptor) {
Parameters.notNull("scope", scope); // NOI18N
final Map result = new HashMap<>();
if (acceptor == null) {
acceptor = ALL_ACCEPTOR;
}
Map> members = null;
Elements elements = JavacElements.instance(ctx);
Types types = JavacTypes.instance(ctx);
TypeElement cls;
Deque outerScopes = new ArrayDeque();
Deque