本文整理汇总了Java中javax.lang.model.element.Modifier类的典型用法代码示例。如果您正苦于以下问题:Java Modifier类的具体用法?Java Modifier怎么用?Java Modifier使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Modifier类属于javax.lang.model.element包,在下文中一共展示了Modifier类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isSynced
import javax.lang.model.element.Modifier; //导入依赖的package包/类
private static boolean isSynced(HintContext ctx, TreePath inspect) {
while (inspect != null && !TreeUtilities.CLASS_TREE_KINDS.contains(inspect.getLeaf().getKind())) {
if (inspect.getLeaf().getKind() == Kind.SYNCHRONIZED) {
return true;
}
if (inspect.getLeaf().getKind() == Kind.METHOD) {
if (((MethodTree) inspect.getLeaf()).getModifiers().getFlags().contains(Modifier.SYNCHRONIZED)) {
return true;
}
break;
}
inspect = inspect.getParentPath();
}
return false;
}
示例2: createToJSONMethod
import javax.lang.model.element.Modifier; //导入依赖的package包/类
private MethodSpec createToJSONMethod() {
return MethodSpec.methodBuilder("toJSON")
.addModifiers(Modifier.FINAL)
.addAnnotation(ClassNames.GWT_INCOMPATIBLE)
.addParameter(Object.class, "object")
.returns(String.class)
.beginControlFlow("if (!isJsObject(object.getClass()))")
.addStatement("throw new $T($S + object.getClass() + $S)", IllegalStateException.class,
"Class ", " isn't a JavaScript object")
.endControlFlow()
.addStatement("$T writer = new $T()", StringWriter.class, StringWriter.class)
.beginControlFlow("try")
.addStatement("writeJSON(writer, object)")
.nextControlFlow("catch ($T e)", IOException.class)
.addStatement("throw new $T(e)", RuntimeException.class)
.endControlFlow()
.addStatement("return writer.toString()")
.build();
}
示例3: buildFields
import javax.lang.model.element.Modifier; //导入依赖的package包/类
@Nonnull
@Override
protected List<FieldSpec> buildFields() {
return getProperties()
.entrySet()
.stream()
.map(property -> {
final String name = property.getKey();
final TypeName type = property.getValue();
final String fieldName = fieldNamePolicy.convert(name, type);
return FieldSpec.builder(type, fieldName)
.addModifiers(Modifier.PRIVATE, Modifier.FINAL)
.build();
})
.collect(Collectors.toList());
}
示例4: testImportGetterSetter
import javax.lang.model.element.Modifier; //导入依赖的package包/类
public void testImportGetterSetter() throws IOException {
testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest8.java");
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
ExpressionTree type = make.QualIdent(workingCopy.getElements().getTypeElement("java.awt.geom.Point2D.Double"));
VariableTree vt = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "test",type, null);
workingCopy.rewrite(clazz, make.addClassMember(clazz, vt));
}
};
src.runModificationTask(task).commit();
assertFiles("testImportGetterSetter.pass");
}
示例5: modelCopyConstructor
import javax.lang.model.element.Modifier; //导入依赖的package包/类
private MethodSpec modelCopyConstructor() {
MethodSpec.Builder copyBuilderCtor = MethodSpec.constructorBuilder()
.addModifiers(Modifier.PRIVATE)
.addParameter(classToBuild(), "model");
shapeModel.getNonStreamingMembers().forEach(m -> {
String name = m.getVariable().getVariableName();
copyBuilderCtor.addStatement("$N(model.$N)", m.getFluentSetterMethodName(), name);
});
if (isException()) {
copyBuilderCtor.addStatement("this.message = model.getMessage()");
}
return copyBuilderCtor.build();
}
示例6: visitNewClass
import javax.lang.model.element.Modifier; //导入依赖的package包/类
@Override
public Tree visitNewClass(NewClassTree node, Trees p) {
Tree t = super.visitNewClass(node, p);
// new class tree > expression statement tree > block. Does not accept anonymous classes for ctor references.
if (node.getClassBody() == null && singleStatementLambdaMethodBody == getCurrentPath().getParentPath().getParentPath().getLeaf()) {
Tree parent = getCurrentPath().getParentPath().getLeaf();
Element el = info.getTrees().getElement(getCurrentPath());
if (el == null || el.getKind() != ElementKind.CONSTRUCTOR || !el.getEnclosingElement().getKind().isClass()) {
return t;
}
el = el.getEnclosingElement();
if (parent.getKind() == Tree.Kind.EXPRESSION_STATEMENT || parent.getKind() == Tree.Kind.RETURN) {
ExpressionTree et = node.getEnclosingExpression();
if (et != null) {
if (el.getModifiers().contains(Modifier.STATIC) || !isMeaninglessQualifier(new TreePath(getCurrentPath().getParentPath(), et))) {
return t;
}
}
foundConstructorReferenceCandidate = true;
}
}
return t;
}
示例7: test157566b
import javax.lang.model.element.Modifier; //导入依赖的package包/类
public void test157566b() throws IOException {
testFile = getFile(getSourceDir(), "org/netbeans/test/codegen/imports157566/b/Test.java");
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
final TypeElement foo = workingCopy.getElements().getTypeElement("org.netbeans.test.codegen.imports157566.b.String");
assertNotNull(foo);
Tree type = make.QualIdent(foo);
VariableTree vt = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "test", type, null);
workingCopy.rewrite(clazz, make.addClassMember(clazz, vt));
}
};
src.runModificationTask(task).commit();
assertFiles("testImports157566b.pass");
}
示例8: generate
import javax.lang.model.element.Modifier; //导入依赖的package包/类
public JavaFile generate(List<RegistrationAnnotatedClass> annotatedClasses) throws ProcessingException {
MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC);
for (RegistrationAnnotatedClass annotatedClass : annotatedClasses) {
ClassName annotatedClassName = ClassName.get(annotatedClass.getClassElement());
ClassName screenClassName = ClassName.bestGuess(annotatedClass.getScreenClassName());
if (annotatedClass.getScreenResultClassName() == null) {
String registrationMethod = getRegistrationMethod(annotatedClass.getScreenType());
constructorBuilder.addStatement(registrationMethod, screenClassName, annotatedClassName);
} else {
String registrationForResultMethod = getRegistrationForResultMethod(annotatedClass.getScreenType());
ClassName screenResultClassName = ClassName.bestGuess(annotatedClass.getScreenResultClassName());
constructorBuilder.addStatement(registrationForResultMethod, screenClassName, annotatedClassName, screenResultClassName);
}
}
TypeSpec navigationFactory = TypeSpec.classBuilder(CLASS_NAME)
.addModifiers(Modifier.PUBLIC)
.superclass(ClassName.get(PACKAGE, SUPERCLASS_NAME))
.addMethod(constructorBuilder.build())
.build();
return JavaFile.builder(PACKAGE, navigationFactory).build();
}
示例9: defineNewHandler
import javax.lang.model.element.Modifier; //导入依赖的package包/类
/**
* Defines a new method as event handler, and marks it with @FXML annotation.
*
* @param h handler definition
*/
private void defineNewHandler(CharSequence handlerName, TypeElement eventType) {
TreeMaker mk = wcopy.getTreeMaker();
// @FXML private void {handlerName}({eventType} event);
MethodTree handler = mk.Method(
mk.Modifiers(Collections.singleton(Modifier.PRIVATE),
Collections.singletonList(fxmlAnnotationTree)
),
handlerName,
mk.PrimitiveType(TypeKind.VOID),
Collections.<TypeParameterTree>emptyList(),
Collections.singletonList(
mk.Variable(
mk.Modifiers(Collections.<Modifier>emptySet()),
"event", mk.Type(eventType.asType()), null)
),
Collections.<ExpressionTree>emptyList(),
mk.Block(Collections.<StatementTree>emptyList(), false),
null);
// add to class
controllerClass = genUtils().insertClassMember(controllerClass, handler);
addMethod(handlerName.toString(), eventType.asType());
}
示例10: testConstantFix208072c
import javax.lang.model.element.Modifier; //导入依赖的package包/类
public void testConstantFix208072c() throws Exception {
Preferences prefs = CodeStylePreferences.get((FileObject) null, JavacParser.MIME_TYPE).getPreferences();
prefs.put("classMembersOrder", "STATIC_INIT;STATIC METHOD;INSTANCE_INIT;CONSTRUCTOR;METHOD;STATIC CLASS;CLASS;STATIC FIELD;FIELD");
prefs.put("classMemberInsertionPoint", "LAST_IN_CATEGORY");
performFixTest("package test;\n" +
"import java.util.logging.Level;\n" +
"import java.util.logging.Logger;\n" +
"public class Test {\n" +
" {\n" +
" int ii = |1 + 2 * 3|;\n" +
" }\n" +
" private static final int II = 0;\n" +
"}\n",
("package test;\n" +
"import java.util.logging.Level;\n" +
"import java.util.logging.Logger;\n" +
"public class Test {\n" +
" {\n" +
" int ii = ZZ;\n" +
" }\n" +
" private static final int II = 0;\n" +
" private static final int ZZ = 1 + 2 * 3;\n" +
"}\n").replaceAll("[ \t\n]+", " "),
new DialogDisplayerImpl("ZZ", true, true, true, EnumSet.of(Modifier.PRIVATE)),
5, 1);
}
示例11: addInputParamFields
import javax.lang.model.element.Modifier; //导入依赖的package包/类
public static void addInputParamFields(JavaSource source,
final List<ParameterInfo> params,
final javax.lang.model.element.Modifier[] modifier) throws IOException {
ModificationResult result = source.runModificationTask(new AbstractTask<WorkingCopy>() {
public void run(WorkingCopy copy) throws IOException {
copy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
List<ParameterInfo> addList = new ArrayList<ParameterInfo>();
for (ParameterInfo p : params) {
if (JavaSourceHelper.getField(copy, Util.getParameterName(p, true, true, true)) == null) {
addList.add(p);
}
}
JavaSourceHelper.addFields(copy, Util.getParamNames(addList),
Util.getParamTypeNames(addList), Util.getParamValues(addList), modifier);
}
});
result.commit();
}
示例12: testIntroduceMethodReplaceDuplicatesSimpleRemapNotUseAfterMethod
import javax.lang.model.element.Modifier; //导入依赖的package包/类
public void testIntroduceMethodReplaceDuplicatesSimpleRemapNotUseAfterMethod() throws Exception {
performFixTest("package test;\n" +
"public class Test {\n" +
" public static void test1() {\n" +
" int i = 0;\n" +
" |System.err.println(i);|\n" +
" }\n" +
" public static void test2() {\n" +
" int a = 0;\n" +
" System.err.println(a);\n" +
" }\n" +
"}",
"package test; public class Test { public static void test1() { int i = 0; name(i); } public static void test2() { int a = 0; name(a); } private static void name(int i) { System.err.println(i); } }",
new DialogDisplayerImpl3("name", EnumSet.of(Modifier.PRIVATE), true, true),
1, 0);
}
示例13: testAddFirstMember
import javax.lang.model.element.Modifier; //导入依赖的package包/类
private void testAddFirstMember(String code, String golden) throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile, code);
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(final WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
final TreeMaker make = workingCopy.getTreeMaker();
ModifiersTree mods = make.Modifiers(EnumSet.of(Modifier.PUBLIC));
Tree ret = make.Type(workingCopy.getTypes().getNoType(TypeKind.VOID));
MethodTree mt = make.Method(mods, "run", ret, Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), "{}", null);
ClassTree en = (ClassTree) cut.getTypeDecls().get(0);
workingCopy.rewrite(en, make.addClassMember(en, mt));
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
示例14: create
import javax.lang.model.element.Modifier; //导入依赖的package包/类
@Override
public String create() {
MethodSpec method = MethodSpec.methodBuilder("onBind")
.addModifiers(Modifier.PUBLIC)
.addParameter(viewType, "target")
.addParameter(valueType, "value")
.returns(void.class)
.addStatement("(($T)$N.getAdapter()).$N(($T)value)", adapterType, "target", getPropertySetter(property), valueType)
.build();
TypeSpec anonymous = TypeSpec.anonymousClassBuilder("")
.addSuperinterface(ParameterizedTypeName
.get(TypeUtils.SYNC_ON_BIND, viewType, valueType))
.addMethod(method)
.build();
return anonymous.toString();
}
示例15: generateHashCode
import javax.lang.model.element.Modifier; //导入依赖的package包/类
private static void generateHashCode(TypeSpec.Builder typeBuilder, Collection<FieldData> fields,
Collection<FieldData> superFields, boolean hasSuperClass){
if(fields.isEmpty()){
return;
}
MethodSpec.Builder hashBuilder = MethodSpec.methodBuilder("hashCode")
.returns(TypeName.INT)
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class);
if(hasSuperClass && !superFields.isEmpty()){
hashBuilder.addStatement("int result = super.hashCode()");
}else{
hashBuilder.addStatement("int result = 0");
}
for(FieldData fd : fields){
addStatementForHashcode(hashBuilder, fd);
}
hashBuilder.addStatement("return result");
sTemp_defined = false;
typeBuilder.addMethod(hashBuilder.build());
}