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

The following examples show how to use com.sun.tools.javac.code.Symbol#getKind() . 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: JavacTrees.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 2
Source File: JavacTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 3
Source File: JavacTrees.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 4
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 5
Source File: JavacTrees.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 6
Source File: JavacTrees.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 7
Source File: JavacTrees.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 8
Source File: JavacTrees.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 9
Source File: JavacTrees.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 10
Source File: JavacTrees.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 11
Source File: UTemplater.java    From Refaster with Apache License 2.0 5 votes vote down vote up
@Override
public UExpression visitIdentifier(IdentifierTree tree, Void v) {
  Symbol sym = ASTHelpers.getSymbol(tree);
  if (sym instanceof ClassSymbol) {
    return UClassIdent.create((ClassSymbol) sym);
  } else if (sym.isStatic()) {
    return staticMember(sym);
  } else if (freeVariables.containsKey(tree.getName().toString())) {
    VarSymbol symbol = freeVariables.get(tree.getName().toString());
    checkState(symbol == sym);
    UExpression ident = UFreeIdent.create(tree.getName().toString());
    Matches matches = ASTHelpers.getAnnotation(symbol, Matches.class);
    if (matches != null) {
      ident = UMatches.create(getValue(matches), true, ident);
    }
    NotMatches notMatches = ASTHelpers.getAnnotation(symbol, NotMatches.class);
    if (notMatches != null) {
      ident = UMatches.create(getValue(notMatches), false, ident);
    }
    OfKind hasKind = ASTHelpers.getAnnotation(symbol, OfKind.class);
    if (hasKind != null) {
      EnumSet<Kind> allowed = EnumSet.copyOf(Arrays.asList(hasKind.value()));
      ident = UOfKind.create(ident, ImmutableSet.copyOf(allowed));
    }
    // @Repeated annotations need to be checked last.
    Repeated repeated = ASTHelpers.getAnnotation(symbol, Repeated.class);
    if (repeated != null) {
      ident = URepeated.create(tree.getName().toString(), ident);
    }
    return ident;
  }
  switch (sym.getKind()) {
    case TYPE_PARAMETER:
      return UTypeVarIdent.create(tree.getName().toString());
    default:
      return ULocalVarIdent.create(tree.getName().toString());
  }
}
 
Example 12
Source File: HandleExtensionMethod.java    From EasyMPermission with MIT License 5 votes vote down vote up
public Extension getExtension(final JavacNode typeNode, final ClassType extensionMethodProviderType) {
	List<MethodSymbol> extensionMethods = new ArrayList<MethodSymbol>();
	TypeSymbol tsym = extensionMethodProviderType.asElement();
	if (tsym != null) for (Symbol member : tsym.getEnclosedElements()) {
		if (member.getKind() != ElementKind.METHOD) continue;
		MethodSymbol method = (MethodSymbol) member;
		if ((method.flags() & (STATIC | PUBLIC)) == 0) continue;
		if (method.params().isEmpty()) continue;
		extensionMethods.add(method);
	}
	return new Extension(extensionMethods, tsym);
}
 
Example 13
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 14
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private Expression convertIdent(JCIdent identifier) {
  if (isThisExpression(identifier)) {
    return new ThisReference(getCurrentType().getTypeDescriptor());
  }
  if (isSuperExpression(identifier)) {
    return new SuperReference(getCurrentType().getTypeDescriptor());
  }
  Symbol symbol = identifier.sym;
  if (symbol instanceof ClassSymbol) {
    return new JavaScriptConstructorReference(
        environment.createDeclarationForType((ClassSymbol) identifier.sym));
  }
  if (symbol instanceof MethodSymbol) {
    return NullLiteral.get().withComment(identifier.toString());
  }

  VarSymbol varSymbol = (VarSymbol) symbol;
  if (symbol.getKind() == ElementKind.LOCAL_VARIABLE
      || symbol.getKind() == ElementKind.RESOURCE_VARIABLE
      || symbol.getKind() == ElementKind.PARAMETER
      || symbol.getKind() == ElementKind.EXCEPTION_PARAMETER) {
    Variable variable = variableByVariableElement.get(symbol);
    return resolveVariableReference(variable);
  }

  FieldDescriptor fieldDescriptor = environment.createFieldDescriptor(varSymbol, identifier.type);
  Expression qualifier =
      fieldDescriptor.isStatic()
          ? null
          : resolveOuterClassReference(fieldDescriptor.getEnclosingTypeDescriptor(), false);
  return FieldAccess.newBuilder()
      .setQualifier(qualifier)
      .setTargetFieldDescriptor(fieldDescriptor)
      .build();
}
 
Example 15
Source File: JsInteropAnnotationUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
/** Determines whether the annotation is an annotation on the symbol {@code sym}. */
private static boolean isAnnotationOnType(Symbol sym, TypeAnnotationPosition position) {
  if (!position.location.isEmpty()) {
    return false;
  }
  switch (sym.getKind()) {
    case LOCAL_VARIABLE:
      return position.type == TargetType.LOCAL_VARIABLE;
    case FIELD:
      return position.type == TargetType.FIELD;
    case CONSTRUCTOR:
    case METHOD:
      return position.type == TargetType.METHOD_RETURN;
    case PARAMETER:
      switch (position.type) {
        case METHOD_FORMAL_PARAMETER:
          return ((MethodSymbol) sym.owner).getParameters().indexOf(sym)
              == position.parameter_index;
        default:
          return false;
      }
    case CLASS:
      // There are no type annotations on the top-level type of the class being declared, only
      // on other types in the signature (e.g. `class Foo extends Bar<@A Baz> {}`).
      return false;
    default:
      throw new InternalCompilerError(
          "Unsupported element kind in MoreAnnotation#isAnnotationOnType: %s.", sym.getKind());
  }
}
 
Example 16
Source File: JsInteropAnnotationUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to recover annotations through looking at the internal representation.
 *
 * <p>TODO(b/136507005): Javac does not keep annotations accessible for types outside compilation.
 * boundaries.
 */
public static Stream<Compound> getDeclarationAndTypeAttributes(Symbol sym) {
  Symbol typeAnnotationOwner;
  switch (sym.getKind()) {
    case PARAMETER:
      // If sym is a parameter, the annotations are stored in the "owner", i.e. the method.
      typeAnnotationOwner = sym.owner;
      break;
    default:
      typeAnnotationOwner = sym;
  }
  // Even though javac does not load annotations for types/elements accross a compilation
  // boundaries, a list of all the annotations related to the type/element are stored. These
  // annotations are stored with a position mark to know whether, for example, they apply to
  // a parameter, etc. We filter out all the annotations that are NOT related to the sym.
  return Streams.concat(
          sym.getRawAttributes().stream(),
          typeAnnotationOwner.getRawTypeAttributes().stream()
              .filter(anno -> isAnnotationOnType(sym, anno.position)))
      // Dedup annotations, since the annotation might be returned multiple times (but
      // represented as different instances) just group them by qualified name and return the
      // first one of each.
      .collect(
          groupingBy(c -> c.type.asElement().getQualifiedName(), LinkedHashMap::new, toList()))
      .values()
      .stream()
      .map(c -> c.get(0));
}
 
Example 17
Source File: NullAway.java    From NullAway with MIT License 5 votes vote down vote up
@Override
public Description matchAssignment(AssignmentTree tree, VisitorState state) {
  if (!matchWithinClass) {
    return Description.NO_MATCH;
  }
  Type lhsType = ASTHelpers.getType(tree.getVariable());
  if (lhsType != null && lhsType.isPrimitive()) {
    return doUnboxingCheck(state, tree.getExpression());
  }
  Symbol assigned = ASTHelpers.getSymbol(tree.getVariable());
  if (assigned == null || assigned.getKind() != ElementKind.FIELD) {
    // not a field of nullable type
    return Description.NO_MATCH;
  }

  if (Nullness.hasNullableAnnotation(assigned, config)) {
    // field already annotated
    return Description.NO_MATCH;
  }
  ExpressionTree expression = tree.getExpression();
  if (mayBeNullExpr(state, expression)) {
    String message = "assigning @Nullable expression to @NonNull field";
    return errorBuilder.createErrorDescriptionForNullAssignment(
        new ErrorMessage(MessageTypes.ASSIGN_FIELD_NULLABLE, message),
        expression,
        buildDescription(tree),
        state);
  }
  return Description.NO_MATCH;
}
 
Example 18
Source File: ElementUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Find the element of the method descriptor associated to the functional interface.
 * 
 * @param origin functional interface element
 * @return associated method descriptor element or <code>null</code> if the <code>origin</code> is not a functional interface.
 * @since 2.14
 */
public ExecutableElement getDescriptorElement(TypeElement origin) {
    com.sun.tools.javac.code.Types types = com.sun.tools.javac.code.Types.instance(info.impl.getJavacTask().getContext());
    if (types.isFunctionalInterface((TypeSymbol)origin)) {
        Symbol sym = types.findDescriptorSymbol((TypeSymbol)origin);
        if (sym != null && sym.getKind() == ElementKind.METHOD) {
            return (ExecutableElement)sym;
        }
    }
    return null;
}