Java Code Examples for com.sun.tools.javac.code.Symbol#getAnnotationMirrors()

The following examples show how to use com.sun.tools.javac.code.Symbol#getAnnotationMirrors() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavacElements.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns all annotations of an element, whether
 * inherited or directly present.
 *
 * @param e  the element being examined
 * @return all annotations of the element
 */
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public List<Attribute.Compound> getAllAnnotationMirrors(Element e) {
    Symbol sym = cast(Symbol.class, e);
    List<Attribute.Compound> annos = sym.getAnnotationMirrors();
    while (sym.getKind() == ElementKind.CLASS) {
        Type sup = ((ClassSymbol) sym).getSuperclass();
        if (!sup.hasTag(CLASS) || sup.isErroneous() ||
                sup.tsym == syms.objectType.tsym) {
            break;
        }
        sym = sup.tsym;
        List<Attribute.Compound> oldAnnos = annos;
        List<Attribute.Compound> newAnnos = sym.getAnnotationMirrors();
        for (Attribute.Compound anno : newAnnos) {
            if (isInherited(anno.type) &&
                    !containsAnnoOfType(oldAnnos, anno.type)) {
                annos = annos.prepend(anno);
            }
        }
    }
    return annos;
}
 
Example 2
Source File: UTemplater.java    From Refaster with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static ImmutableClassToInstanceMap<Annotation> annotationMap(Symbol symbol) {
  ImmutableClassToInstanceMap.Builder<Annotation> builder = ImmutableClassToInstanceMap.builder();
  for (Compound compound : symbol.getAnnotationMirrors()) {
    Name qualifiedAnnotationType =
        ((TypeElement) compound.getAnnotationType().asElement()).getQualifiedName();
    try {
      Class<? extends Annotation> annotationClazz = 
          Class.forName(qualifiedAnnotationType.toString()).asSubclass(Annotation.class);
      builder.put((Class) annotationClazz,
          AnnotationProxyMaker.generateAnnotation(compound, annotationClazz));
    } catch (ClassNotFoundException e) {
      throw new IllegalArgumentException("Unrecognized annotation type", e);
    }
  }
  return builder.build();
}
 
Example 3
Source File: AnnotationChecker.java    From grpc-java-api-checker with Apache License 2.0 5 votes vote down vote up
/**
 * Returns non-null if api is annotated.
 */
private AnnotationMirror findAnnotatedApi(Symbol symbol) {
  if (symbol == null) {
    return null;
  }
  for (AnnotationMirror annotation : symbol.getAnnotationMirrors()) {
    if (annotation.getAnnotationType().toString().equals(annotationType)) {
      return annotation;
    }
  }
  // recursive
  return findAnnotatedApi(symbol.owner);
}
 
Example 4
Source File: AnnotatedApiUsageChecker.java    From guava-beta-checker with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the given symbol is annotated with the annotation or if it's a member of a type
 * annotated with the annotation.
 */
private boolean isAnnotatedApi(Symbol symbol) {
  Name name = symbol.getQualifiedName();
  if (name != null && isIgnoredType(name.toString())) {
    return false;
  }

  for (AnnotationMirror annotation : symbol.getAnnotationMirrors()) {
    if (annotation.getAnnotationType().toString().equals(annotationType)) {
      return true;
    }
  }

  return isMemberOfAnnotatedApi(symbol);
}
 
Example 5
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 5 votes vote down vote up
private boolean isExtensionMethod( Symbol sym )
{
  if( sym instanceof Symbol.MethodSymbol )
  {
    for( Attribute.Compound annotation: sym.getAnnotationMirrors() )
    {
      if( annotation.type.toString().equals( ExtensionMethod.class.getName() ) )
      {
        return true;
      }
    }
  }
  return false;
}
 
Example 6
Source File: TypeUtil.java    From manifold with Apache License 2.0 5 votes vote down vote up
public static boolean isStructuralInterface( TypeProcessor tp, Symbol sym )
{
  if( sym == null )
  {
    return false;
  }

  if( (!sym.isInterface() || !sym.hasAnnotations()) && !(sym instanceof Symbol.TypeVariableSymbol) )
  {
    return false;
  }

  // use the raw type
  Type type = tp.getTypes().erasure( sym.type );
  sym = type.tsym;
  if( !sym.isInterface() || !sym.hasAnnotations() )
  {
    return false;
  }

  for( Attribute.Compound annotation : sym.getAnnotationMirrors() )
  {
    if( annotation.type.toString().equals( Structural.class.getName() ) )
    {
      return true;
    }
  }
  return false;
}
 
Example 7
Source File: HandleDelegate.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void addMethodBindings(List<MethodSig> signatures, ClassType ct, JavacTypes types, Set<String> banList) throws DelegateRecursion {
	TypeSymbol tsym = ct.asElement();
	if (tsym == null) return;
	
	for (Symbol member : tsym.getEnclosedElements()) {
		for (Compound am : member.getAnnotationMirrors()) {
			String name = null;
			try {
				name = am.type.tsym.flatName().toString();
			} catch (Exception ignore) {}
			
			if ("lombok.Delegate".equals(name) || "lombok.experimental.Delegate".equals(name)) {
				throw new DelegateRecursion(ct.tsym.name.toString(), member.name.toString());
			}
		}
		if (member.getKind() != ElementKind.METHOD) continue;
		if (member.isStatic()) continue;
		if (member.isConstructor()) continue;
		ExecutableElement exElem = (ExecutableElement)member;
		if (!exElem.getModifiers().contains(Modifier.PUBLIC)) continue;
		ExecutableType methodType = (ExecutableType) types.asMemberOf(ct, member);
		String sig = printSig(methodType, member.name, types);
		if (!banList.add(sig)) continue; //If add returns false, it was already in there
		boolean isDeprecated = (member.flags() & DEPRECATED) != 0;
		signatures.add(new MethodSig(member.name, methodType, isDeprecated, exElem));
	}
	
	if (ct.supertype_field instanceof ClassType) addMethodBindings(signatures, (ClassType) ct.supertype_field, types, banList);
	if (ct.interfaces_field != null) for (Type iface : ct.interfaces_field) {
		if (iface instanceof ClassType) addMethodBindings(signatures, (ClassType) iface, types, banList);
	}
}
 
Example 8
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 4 votes vote down vote up
private Symbol.MethodSymbol findExtMethod( JCTree.JCMethodInvocation tree )
{
  Symbol sym = null;
  if( tree.meth instanceof JCTree.JCFieldAccess )
  {
    sym = ((JCTree.JCFieldAccess)tree.meth).sym;
  }
  else if( tree.meth instanceof JCTree.JCIdent )
  {
    sym = ((JCTree.JCIdent)tree.meth).sym;
  }

  if( sym == null || !sym.hasAnnotations() )
  {
    return null;
  }

  for( Attribute.Compound annotation: sym.getAnnotationMirrors() )
  {
    if( annotation.type.toString().equals( ExtensionMethod.class.getName() ) )
    {
      String extensionClass = (String)annotation.values.get( 0 ).snd.getValue();
      boolean isStatic = (boolean)annotation.values.get( 1 ).snd.getValue();
      BasicJavacTask javacTask = (BasicJavacTask)_tp.getJavacTask(); //JavacHook.instance() != null ? (JavacTaskImpl)JavacHook.instance().getJavacTask_PlainFileMgr() : ClassSymbols.instance( _sp.getModule() ).getJavacTask_PlainFileMgr();
      Pair<Symbol.ClassSymbol, JCTree.JCCompilationUnit> classSymbol = ClassSymbols.instance( _sp.getModule() ).getClassSymbol( javacTask, _tp, extensionClass );
      if( classSymbol == null )
      {
        // In module mode if a package in another module is not exported, classes in the package
        // will not be accessible to other modules, hence the null classSymbol
        continue;
      }

      Symbol.ClassSymbol extClassSym = classSymbol.getFirst();
      if( extClassSym == null )
      {
        // This can happen during bootstrapping with Dark Java classes from Manifold itself
        // So we short-circuit that here (ManClassFinder_9 or any other darkj class used during bootstrapping doesn't really need to use extensions)
        return null;
      }
      Types types = Types.instance( javacTask.getContext() );
      outer:
      for( Symbol elem: IDynamicJdk.instance().getMembers( extClassSym ) )
      {
        if( elem instanceof Symbol.MethodSymbol && elem.flatName().toString().equals( sym.name.toString() ) )
        {
          Symbol.MethodSymbol extMethodSym = (Symbol.MethodSymbol)elem;
          List<Symbol.VarSymbol> extParams = extMethodSym.getParameters();
          List<Symbol.VarSymbol> calledParams = ((Symbol.MethodSymbol)sym).getParameters();
          int thisOffset = isStatic ? 0 : 1;
          if( extParams.size() - thisOffset != calledParams.size() )
          {
            continue;
          }
          for( int i = thisOffset; i < extParams.size(); i++ )
          {
            Symbol.VarSymbol extParam = extParams.get( i );
            Symbol.VarSymbol calledParam = calledParams.get( i - thisOffset );
            if( !types.isSameType( types.erasure( extParam.type ), types.erasure( calledParam.type ) ) )
            {
              continue outer;
            }
          }
          return extMethodSym;
        }
      }
    }
  }
  return null;
}