本文整理汇总了Java中javax.lang.model.element.VariableElement.getSimpleName方法的典型用法代码示例。如果您正苦于以下问题:Java VariableElement.getSimpleName方法的具体用法?Java VariableElement.getSimpleName怎么用?Java VariableElement.getSimpleName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.lang.model.element.VariableElement
的用法示例。
在下文中一共展示了VariableElement.getSimpleName方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: KeyDefinition
import javax.lang.model.element.VariableElement; //导入方法依赖的package包/类
KeyDefinition(@NonNull ProcessingContext context, @NonNull VariableElement element) {
this.context = context;
this.element = element;
fieldTypeName = TypeName.get(element.asType());
capitalizedName = Strings.upperFirst(element.getSimpleName().toString());
key = element.getAnnotation(Key.class);
prefsAdapter = PrefsAdapter.find(this);
if (prefsAdapter == null) {
// ex) Can not find available PrefsAdapter for admin field(type: Boolean) of User class
throw new ProcessingException(
"Can not find available PrefsAdapter for "
+ element.getSimpleName() + " field(type: " + getFieldTypeName().toString() + ")"
+ " of " + element.getEnclosingElement().getSimpleName() + " class.", element);
}
}
示例2: createVariables
import javax.lang.model.element.VariableElement; //导入方法依赖的package包/类
/**
* Crates method formal parameters, following code style conventions.
* The trees in 'statements' will be rewritten to use the new identifiers.
*
* @param copy working copy
* @param parameters variables to turn into parameters
* @param statements trees that should refer to parameters
* @return
*/
static List<VariableTree> createVariables(WorkingCopy copy, List<VariableElement> parameters,
TreePath targetParent,
List<TreePath> statements) {
final TreeMaker make = copy.getTreeMaker();
List<VariableTree> formalArguments = new LinkedList<VariableTree>();
CodeStyle cs = CodeStyle.getDefault(copy.getFileObject());
String prefix = cs.getParameterNamePrefix();
String suffix = cs.getParameterNameSuffix();
Map<VariableElement, CharSequence> renamedVariables = new HashMap<VariableElement, CharSequence>();
Set<Name> changedNames = new HashSet<Name>();
for (VariableElement p : parameters) {
TypeMirror tm = p.asType();
Tree type = make.Type(tm);
Name formalArgName = p.getSimpleName();
Set<Modifier> formalArgMods = EnumSet.noneOf(Modifier.class);
if (p.getModifiers().contains(Modifier.FINAL)) {
formalArgMods.add(Modifier.FINAL);
}
String strippedName = Utilities.stripVariableName(cs, p);
CharSequence codeStyleName = Utilities.guessName(copy, strippedName, targetParent, prefix, suffix, p.getKind() == ElementKind.PARAMETER);
if (!formalArgName.contentEquals(codeStyleName)) {
renamedVariables.put(p, codeStyleName);
changedNames.add(formalArgName);
} else {
codeStyleName = formalArgName;
}
formalArguments.add(make.Variable(make.Modifiers(formalArgMods), codeStyleName, type, null));
}
if (!changedNames.isEmpty()) {
VariableRenamer renamer = new VariableRenamer(copy, renamedVariables, changedNames);
for (TreePath stPath : statements) {
renamer.scan(stPath, null);
}
}
return formalArguments;
}
示例3: equals
import javax.lang.model.element.VariableElement; //导入方法依赖的package包/类
private static boolean equals(VariableElement var1, VariableElement var2) {
if ( var1.getSimpleName() == var2.getSimpleName()
&& var1.getSimpleName().contentEquals("class")) {
return var1.getEnclosingElement().equals(var2.getEnclosingElement());
} else {
return var1.equals(var2);
}
}
示例4: getParamName
import javax.lang.model.element.VariableElement; //导入方法依赖的package包/类
private String getParamName(VariableElement methodParam) {
Param paramAnnotation = methodParam.getAnnotation(Param.class);
if (paramAnnotation != null) {
return paramAnnotation.value();
}
throw new IllegalArgumentException("Parameter " + methodParam.getSimpleName() + " doesn't have " + Param.class.getSimpleName() + " annotation with name.");
}
示例5: run
import javax.lang.model.element.VariableElement; //导入方法依赖的package包/类
void run() {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
List<String> opts = Arrays.asList("-parameters");
JavacTask task = (JavacTask) compiler.getTask(null, null, null, opts, null, null);
TypeElement t = task.getElements().getTypeElement("T");
ExecutableElement testMethod = ElementFilter.methodsIn(t.getEnclosedElements()).get(0);
VariableElement param = testMethod.getParameters().get(0);
Name paramName = param.getSimpleName();
if (!paramName.contentEquals("p")) {
throw new AssertionError("Wrong parameter name: " + paramName);
}
}
示例6: getMatchingParams
import javax.lang.model.element.VariableElement; //导入方法依赖的package包/类
private List<List<String>> getMatchingParams(CompilationInfo info, TypeMirror type, Iterable<? extends Element> elements, String name, TypeMirror[] argTypes, Types types) {
List<List<String>> ret = new ArrayList<>();
TypeUtilities tu = info.getTypeUtilities();
for (Element e : elements) {
if ((e.getKind() == CONSTRUCTOR || e.getKind() == METHOD) && name.contentEquals(e.getSimpleName())) {
List<? extends VariableElement> params = ((ExecutableElement) e).getParameters();
int parSize = params.size();
boolean varArgs = ((ExecutableElement) e).isVarArgs();
if (!varArgs && (parSize < argTypes.length)) {
continue;
}
if (parSize == 0) {
ret.add(Collections.<String>singletonList(NbBundle.getMessage(JavaCompletionTask.class, "JCP-no-parameters")));
} else {
ExecutableType eType = (ExecutableType) asMemberOf(e, type, types);
Iterator<? extends TypeMirror> parIt = eType.getParameterTypes().iterator();
TypeMirror param = null;
for (int i = 0; i <= argTypes.length; i++) {
if (parIt.hasNext()) {
param = parIt.next();
if (!parIt.hasNext() && param.getKind() == TypeKind.ARRAY) {
param = ((ArrayType) param).getComponentType();
}
} else if (!varArgs) {
break;
}
if (i == argTypes.length) {
List<String> paramStrings = new ArrayList<>(parSize);
Iterator<? extends TypeMirror> tIt = eType.getParameterTypes().iterator();
for (Iterator<? extends VariableElement> it = params.iterator(); it.hasNext();) {
VariableElement ve = it.next();
StringBuilder sb = new StringBuilder();
sb.append(tu.getTypeName(tIt.next()));
if (varArgs && !tIt.hasNext()) {
sb.delete(sb.length() - 2, sb.length()).append("..."); //NOI18N
}
CharSequence veName = ve.getSimpleName();
if (veName != null && veName.length() > 0) {
sb.append(" "); // NOI18N
sb.append(veName);
}
if (it.hasNext()) {
sb.append(", "); // NOI18N
}
paramStrings.add(sb.toString());
}
ret.add(paramStrings);
break;
}
if (argTypes[i] == null || argTypes[i].getKind() != TypeKind.ERROR && !types.isAssignable(argTypes[i], param)) {
break;
}
}
}
}
}
return ret.isEmpty() ? null : ret;
}
示例7: compare
import javax.lang.model.element.VariableElement; //导入方法依赖的package包/类
@Hint(
displayName = "#DN_ComparatorParameterNotUsed",
description = "#DESC_ComparatorParameterNotUsed",
category = "bugs",
suppressWarnings = { "ComparatorMethodParameterNotUsed" },
enabled = true
)
@TriggerPattern("public int compare($v1, $v2) { $stmts$; } ")
public static List<ErrorDescription> run(HintContext ctx) {
CompilationInfo ci = ctx.getInfo();
Element me = ci.getTrees().getElement(ctx.getPath());
if (me == null) {
return null;
}
Element clazz = me.getEnclosingElement();
if (clazz == null || !(
clazz.getKind().isClass() ||
/* JDK8 */ (clazz.getKind().isInterface() && me.getModifiers().contains(Modifier.DEFAULT)))
) {
// method bod not valid at this point
return null;
}
TypeMirror tm = clazz.asType();
TypeElement comparableIface = ci.getElements().getTypeElement("java.lang.Comparable"); // NOI18N
// the eclosing type must implement or extend comparable
if (comparableIface == null ||
!ci.getTypes().isSubtype(tm, comparableIface.asType())) {
return null;
}
Set<VariableElement> vars = new HashSet<VariableElement>(2);
ExecutableElement ee = (ExecutableElement)me;
vars.addAll(ee.getParameters());
ComparatorParameterNotUsed v = new ComparatorParameterNotUsed(ci, vars);
MethodTree mt = (MethodTree)ctx.getPath().getLeaf();
if (mt.getBody() == null) {
return null;
}
try {
v.scan(new TreePath(ctx.getPath(), mt.getBody()), v);
} catch (StopProcessing ex) {
// nothing, just fast interrupt
}
if (v.unusedVars.isEmpty()) {
return null;
}
// the method has exactly 2 parameters:
VariableTree par1 = mt.getParameters().get(0);
VariableTree par2 = mt.getParameters().get(1);
List<? extends VariableElement> ll = new ArrayList<VariableElement>(v.unusedVars);
List<ErrorDescription> res = new ArrayList<>(ll.size());
Collections.sort(ll, Collator.getInstance());
for (VariableElement ve : ll) {
Tree vt = ve.getSimpleName() == par1.getName() ? par1 : par2;
res.add(
ErrorDescriptionFactory.forName(ctx, vt,
TEXT_ComparatorParameterNotUsed(ve.getSimpleName().toString())
));
}
return res;
}
示例8: determineEventHandlers
import javax.lang.model.element.VariableElement; //导入方法依赖的package包/类
private void determineEventHandlers( @Nonnull final ComponentDescriptor descriptor )
{
final List<EventHandlerDescriptor> eventHandlers =
ProcessorUtil.getMethods( descriptor.getElement(), processingEnv.getTypeUtils() ).stream()
.filter( m -> null != ProcessorUtil.findAnnotationByType( m, Constants.EVENT_HANDLER_ANNOTATION_CLASSNAME ) )
.map( m -> createEventHandlerDescriptor( descriptor, m ) )
.collect( Collectors.toList() );
for ( final EventHandlerDescriptor eventHandler : eventHandlers )
{
final ExecutableElement method = eventHandler.getMethod();
final TypeElement handlerType = getEventHandlerType( method );
if ( ElementKind.INTERFACE != handlerType.getKind() )
{
throw new ReactProcessorException( "The @EventHandler specified an invalid type that is not an interface.",
eventHandler.getMethod() );
}
if ( null == ProcessorUtil.findAnnotationByType( handlerType, Constants.JS_FUNCTION_CLASSNAME ) )
{
throw new ReactProcessorException( "The @EventHandler specified an invalid type that is not annotated " +
"with the annotation jsinterop.annotations.JsFunction.",
eventHandler.getMethod() );
}
final EventHandlerDescriptor matched = eventHandlers.stream()
.filter( h -> h != eventHandler && h.getName().equals( eventHandler.getName() ) )
.findAny().orElse( null );
if ( null != matched )
{
throw new ReactProcessorException( "The @EventHandler has the same name as the event handler defined by " +
matched.getMethod() + ".", eventHandler.getMethod() );
}
final EventHandlerDescriptor matched2 = eventHandlers.stream()
.filter( h -> h != eventHandler &&
h.getMethod().getSimpleName().equals( eventHandler.getMethod().getSimpleName() ) )
.findAny().orElse( null );
if ( null != matched2 )
{
throw new ReactProcessorException( "The @EventHandler has the same method name as the event handler defined " +
"by " + matched2.getMethod() + ".", eventHandler.getMethod() );
}
final ExecutableType methodType = eventHandler.getMethodType();
final List<? extends TypeMirror> parameters = methodType.getParameterTypes();
if ( !parameters.isEmpty() )
{
// Our annotated handler method has parameters so they should exactly align
// in count and type with the parameters in the event handler method
final ExecutableElement target = eventHandler.getEventHandlerMethod();
final List<? extends VariableElement> targetParameters = target.getParameters();
if ( targetParameters.size() != parameters.size() )
{
throw new ReactProcessorException( "The @EventHandler target has " + parameters.size() + " parameters " +
"but the type parameter specified a handler with method type " +
eventHandler.getEventHandlerType().getQualifiedName() + " that has " +
"handler method with " + targetParameters.size() + " parameters. The " +
"@EventHandler target should have zero parameters or match the number " +
"of parameter in the target method " + target.getSimpleName() + ".",
eventHandler.getMethod() );
}
for ( int i = 0; i < parameters.size(); i++ )
{
final TypeMirror parameterType = parameters.get( i );
final VariableElement element = targetParameters.get( i );
final TypeMirror targetParameterType = element.asType();
final TypeMirror targetErased = processingEnv.getTypeUtils().erasure( targetParameterType );
final TypeMirror parameterErased = processingEnv.getTypeUtils().erasure( parameterType );
if ( !processingEnv.getTypeUtils().isAssignable( targetErased, parameterErased ) )
{
throw new ReactProcessorException( "The @EventHandler target parameter named " +
eventHandler.getMethod().getParameters().get( i ).getSimpleName() +
" of type " + parameterType + " is not assignable from target type " +
targetParameterType + " of parameter " + element.getSimpleName() +
" in method " + eventHandler.getEventHandlerType().getQualifiedName() +
"." + target.getSimpleName() + ".",
eventHandler.getMethod() );
}
}
}
}
descriptor.setEventHandlers( eventHandlers );
}
示例9: addStaticStructMember
import javax.lang.model.element.VariableElement; //导入方法依赖的package包/类
protected String addStaticStructMember(VariableElement field, String cname) {
String res = null;
Object exp = null;
if (!field.getModifiers().contains(Modifier.STATIC))
return res;
if (!field.getModifiers().contains(Modifier.FINAL))
return res;
exp = field.getConstantValue();
if (exp != null) {
/* Constant. */
String cn = cname + "_" + field.getSimpleName();
String suffix = null;
long val = 0;
/* Can only handle int, long, float, and double fields. */
if (exp instanceof Byte
|| exp instanceof Short
|| exp instanceof Integer) {
suffix = "L";
val = ((Number)exp).intValue();
}
else if (exp instanceof Long) {
// Visual C++ supports the i64 suffix, not LL
suffix = isWindows ? "i64" : "LL";
val = ((Long)exp).longValue();
}
else if (exp instanceof Float) suffix = "f";
else if (exp instanceof Double) suffix = "";
else if (exp instanceof Character) {
suffix = "L";
Character ch = (Character) exp;
val = ((int) ch) & 0xffff;
}
if (suffix != null) {
// Some compilers will generate a spurious warning
// for the integer constants for Integer.MIN_VALUE
// and Long.MIN_VALUE so we handle them specially.
if ((suffix.equals("L") && (val == Integer.MIN_VALUE)) ||
(suffix.equals("LL") && (val == Long.MIN_VALUE))) {
res = " #undef " + cn + lineSep
+ " #define " + cn
+ " (" + (val + 1) + suffix + "-1)" + lineSep;
} else if (suffix.equals("L") || suffix.endsWith("LL")) {
res = " #undef " + cn + lineSep
+ " #define " + cn + " " + val + suffix + lineSep;
} else {
res = " #undef " + cn + lineSep
+ " #define " + cn + " " + exp + suffix + lineSep;
}
}
}
return res;
}
示例10: defineForStatic
import javax.lang.model.element.VariableElement; //导入方法依赖的package包/类
protected String defineForStatic(TypeElement c, VariableElement f)
throws Util.Exit {
CharSequence cnamedoc = c.getQualifiedName();
CharSequence fnamedoc = f.getSimpleName();
String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);
if (!f.getModifiers().contains(Modifier.STATIC))
util.bug("tried.to.define.non.static");
if (f.getModifiers().contains(Modifier.FINAL)) {
Object value = null;
value = f.getConstantValue();
if (value != null) { /* so it is a ConstantExpression */
String constString = null;
if ((value instanceof Integer)
|| (value instanceof Byte)
|| (value instanceof Short)) {
/* covers byte, short, int */
constString = value.toString() + "L";
} else if (value instanceof Boolean) {
constString = ((Boolean) value) ? "1L" : "0L";
} else if (value instanceof Character) {
Character ch = (Character) value;
constString = String.valueOf(((int) ch) & 0xffff) + "L";
} else if (value instanceof Long) {
// Visual C++ supports the i64 suffix, not LL.
if (isWindows)
constString = value.toString() + "i64";
else
constString = value.toString() + "LL";
} else if (value instanceof Float) {
/* bug for bug */
float fv = ((Float)value).floatValue();
if (Float.isInfinite(fv))
constString = ((fv < 0) ? "-" : "") + "Inff";
else
constString = value.toString() + "f";
} else if (value instanceof Double) {
/* bug for bug */
double d = ((Double)value).doubleValue();
if (Double.isInfinite(d))
constString = ((d < 0) ? "-" : "") + "InfD";
else
constString = value.toString();
}
if (constString != null) {
StringBuilder s = new StringBuilder("#undef ");
s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
s.append("#define "); s.append(cname); s.append("_");
s.append(fname); s.append(" "); s.append(constString);
return s.toString();
}
}
}
return null;
}
示例11: defineForStatic
import javax.lang.model.element.VariableElement; //导入方法依赖的package包/类
protected String defineForStatic(TypeElement c, VariableElement f) {
CharSequence cnamedoc = c.getQualifiedName();
CharSequence fnamedoc = f.getSimpleName();
String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);
Assert.check(f.getModifiers().contains(Modifier.STATIC));
if (f.getModifiers().contains(Modifier.FINAL)) {
Object value = null;
value = f.getConstantValue();
if (value != null) { /* so it is a ConstantExpression */
String constString = null;
if ((value instanceof Integer)
|| (value instanceof Byte)
|| (value instanceof Short)) {
/* covers byte, short, int */
constString = value.toString() + "L";
} else if (value instanceof Boolean) {
constString = ((Boolean) value) ? "1L" : "0L";
} else if (value instanceof Character) {
Character ch = (Character) value;
constString = String.valueOf(((int) ch) & 0xffff) + "L";
} else if (value instanceof Long) {
// Visual C++ supports the i64 suffix, not LL.
if (isWindows)
constString = value.toString() + "i64";
else
constString = value.toString() + "LL";
} else if (value instanceof Float) {
/* bug for bug */
float fv = ((Float)value).floatValue();
if (Float.isInfinite(fv))
constString = ((fv < 0) ? "-" : "") + "Inff";
else
constString = value.toString() + "f";
} else if (value instanceof Double) {
/* bug for bug */
double d = ((Double)value).doubleValue();
if (Double.isInfinite(d))
constString = ((d < 0) ? "-" : "") + "InfD";
else
constString = value.toString();
}
if (constString != null) {
StringBuilder s = new StringBuilder("#undef ");
s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
s.append("#define "); s.append(cname); s.append("_");
s.append(fname); s.append(" "); s.append(constString);
return s.toString();
}
}
}
return null;
}
示例12: getFieldKey
import javax.lang.model.element.VariableElement; //导入方法依赖的package包/类
/**
* Returns a (probably) unique Bundle key for a field.
*/
private String getFieldKey(VariableElement field) {
return "KEY_" + info.className.reflectionName() + "_" + field.getSimpleName();
}