本文整理汇总了Java中com.sun.source.tree.ModifiersTree类的典型用法代码示例。如果您正苦于以下问题:Java ModifiersTree类的具体用法?Java ModifiersTree怎么用?Java ModifiersTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ModifiersTree类属于com.sun.source.tree包,在下文中一共展示了ModifiersTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addFields
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
public static void addFields(WorkingCopy copy, String[] names, Object[] types, Object[] initialValues, Modifier[] modifiers) {
TreeMaker maker = copy.getTreeMaker();
ClassTree classTree = getTopLevelClassTree(copy);
ClassTree modifiedTree = classTree;
String[] annotations = new String[0];
Object[] annotationAttrs = new Object[0];
for (int i = 0; i < names.length; i++) {
String name = names[i];
Object type = types[i];
Object initialValue = initialValues[i];
// get around Retouche lack support for non-java.lang type Literal ???
if (initialValue instanceof Enum) {
continue;
}
Tree typeTree = createTypeTree(copy, type);
ModifiersTree modifiersTree = createModifiersTree(copy, modifiers, annotations, annotationAttrs);
ExpressionTree init = initialValue == null ? null : maker.Literal(initialValue);
VariableTree variableTree = maker.Variable(modifiersTree, name, typeTree, init);
modifiedTree = maker.insertClassMember(modifiedTree, 0, variableTree);
}
copy.rewrite(classTree, modifiedTree);
}
示例2: createHashCodeMethod
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
private static MethodTree createHashCodeMethod(WorkingCopy wc, Iterable<? extends VariableElement> hashCodeFields, Scope scope) {
TreeMaker make = wc.getTreeMaker();
Set<Modifier> mods = EnumSet.of(Modifier.PUBLIC);
int startNumber = generatePrimeNumber(2, 10);
int multiplyNumber = generatePrimeNumber(10, 100);
List<StatementTree> statements = new ArrayList<>();
//int hash = <startNumber>;
statements.add(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "hash", make.PrimitiveType(TypeKind.INT), make.Literal(startNumber))); //NOI18N
for (VariableElement ve : hashCodeFields) {
TypeMirror tm = ve.asType();
ExpressionTree variableRead = prepareExpression(wc, HASH_CODE_PATTERNS, tm, ve, scope);
statements.add(make.ExpressionStatement(make.Assignment(make.Identifier("hash"), make.Binary(Tree.Kind.PLUS, make.Binary(Tree.Kind.MULTIPLY, make.Literal(multiplyNumber), make.Identifier("hash")), variableRead)))); //NOI18N
}
statements.add(make.Return(make.Identifier("hash"))); //NOI18N
BlockTree body = make.Block(statements, false);
ModifiersTree modifiers = prepareModifiers(wc, mods,make);
return make.Method(modifiers, "hashCode", make.PrimitiveType(TypeKind.INT), Collections.<TypeParameterTree> emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), body, null); //NOI18N
}
示例3: createMainMethod
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
/**
* Creates a public static {@code main(String[])} method
* with the body taken from settings.
*
* @param maker {@code TreeMaker} to use for creating the method
* @return created {@code main(...)} method,
* or {@code null} if the method body would be empty
*/
private MethodTree createMainMethod(TreeMaker maker) {
String initialMainMethodBody = getInitialMainMethodBody();
if (initialMainMethodBody.length() == 0) {
return null;
}
ModifiersTree modifiers = maker.Modifiers(
createModifierSet(Modifier.PUBLIC, Modifier.STATIC));
VariableTree param = maker.Variable(
maker.Modifiers(Collections.<Modifier>emptySet()),
"argList", //NOI18N
maker.Identifier("String[]"), //NOI18N
null); //initializer - not used in params
MethodTree mainMethod = maker.Method(
modifiers, //public static
"main", //method name "main"//NOI18N
maker.PrimitiveType(TypeKind.VOID), //return type "void"
Collections.<TypeParameterTree>emptyList(), //type params
Collections.<VariableTree>singletonList(param), //method param
Collections.<ExpressionTree>emptyList(), //throws-list
'{' + initialMainMethodBody + '}', //body text
null); //only for annotations
return mainMethod;
}
示例4: testPositionForEnumModifiers
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
@Test
void testPositionForEnumModifiers() throws IOException {
final String theString = "public";
String code = "package test; " + theString + " enum Test {A;}";
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();
SourcePositions pos = Trees.instance(ct).getSourcePositions();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
ModifiersTree mt = clazz.getModifiers();
int spos = code.indexOf(theString);
int epos = spos + theString.length();
assertEquals("testPositionForEnumModifiers",
spos, pos.getStartPosition(cut, mt));
assertEquals("testPositionForEnumModifiers",
epos, pos.getEndPosition(cut, mt));
}
示例5: performRewrite
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) {
WorkingCopy wc = ctx.getWorkingCopy();
GeneratorUtilities gu = GeneratorUtilities.get(wc);
TreePath path = ctx.getPath();
final ClassTree cls = (ClassTree) path.getLeaf();
gu.importComments(cls, wc.getCompilationUnit());
final TreeMaker treeMaker = wc.getTreeMaker();
ModifiersTree mods = cls.getModifiers();
if (mods.getFlags().contains(Modifier.ABSTRACT)) {
Set<Modifier> modifiers = EnumSet.copyOf(mods.getFlags());
modifiers.remove(Modifier.ABSTRACT);
ModifiersTree nmods = treeMaker.Modifiers(modifiers, mods.getAnnotations());
gu.copyComments(mods, nmods, true);
gu.copyComments(mods, nmods, false);
mods = nmods;
}
Tree nue = treeMaker.Interface(mods, cls.getSimpleName(), cls.getTypeParameters(), cls.getImplementsClause(), cls.getMembers());
gu.copyComments(cls, nue, true);
gu.copyComments(cls, nue, false);
wc.rewrite(path.getLeaf(), nue);
}
示例6: getModifierTree
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
private ModifiersTree getModifierTree(TreePath treePath) {
Kind kind = treePath.getLeaf().getKind();
switch (kind) {
case ANNOTATION_TYPE:
case CLASS:
case ENUM:
case INTERFACE:
return ((ClassTree) treePath.getLeaf()).getModifiers();
case VARIABLE:
return ((VariableTree) treePath.getLeaf()).getModifiers();
case METHOD:
return ((MethodTree) treePath.getLeaf()).getModifiers();
default:
throw new UnsupportedOperationException("kind " + kind + " not yet suppported");
}
}
示例7: createPropertySetterMethod
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
/**
* Creates a new public property setter method.
*
* @param modifiersTree the method modifiers; cannot be null.
* @param propertyType the property type; cannot be null.
* @param propertyName the property name; cannot be null.
* @return the new method; never null.
*/
public MethodTree createPropertySetterMethod(ModifiersTree modifiersTree, String propertyName, Tree propertyType) {
Parameters.notNull("modifiersTree", modifiersTree); // NOI18N
Parameters.javaIdentifier("propertyName", propertyName); // NOI18N
Parameters.notNull("propertyType", propertyType); // NOI18N
TreeMaker make = getTreeMaker();
return make.Method(
modifiersTree,
createPropertyAccessorName(propertyName, false),
make.PrimitiveType(TypeKind.VOID),
Collections.<TypeParameterTree>emptyList(),
Collections.singletonList(createVariable(propertyName, propertyType)),
Collections.<ExpressionTree>emptyList(),
"{ this." + propertyName + " = " + propertyName + "; }", // NOI18N
null);
}
示例8: createAssignmentConstructor
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
/**
* Creates a constructor which assigns its parameters to fields with the
* same names. For example it can be used to generate:
*
* <pre>
* public void Constructor(String field1, Object field2) {
* this.field1 = field1;
* this.field2 = field2;
* }
* </pre>
*
* @param modifiersTree the constructor modifiers.
* @param constructorName the constructor name; cannot be null, it's not used inside except for assertion since 2007 (or before, TODO: remove?)
* @param parameters the constructor parameters; cannot be null.
* @return the new constructor; never null.
*/
public MethodTree createAssignmentConstructor(ModifiersTree modifiersTree, String constructorName, List<VariableTree> parameters) {
Parameters.notNull("modifiersTree", modifiersTree);
Parameters.javaIdentifier("constructorName", constructorName); // NOI18N
Parameters.notNull("parameters", parameters); // NOI18N
StringBuilder body = new StringBuilder(parameters.size() * 30);
body.append("{"); // NOI18N
for (VariableTree parameter : parameters) {
String parameterName = parameter.getName().toString();
body.append("this." + parameterName + " = " + parameterName + ";"); // NOI18N
}
body.append("}"); // NOI18N
TreeMaker make = getTreeMaker();
return make.Constructor(
modifiersTree,
Collections.<TypeParameterTree>emptyList(),
parameters,
Collections.<ExpressionTree>emptyList(),
body.toString());
}
示例9: composeNewTestClass
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
/**
*/
@Override
protected ClassTree composeNewTestClass(WorkingCopy workingCopy,
String name,
List<? extends Tree> members) {
final TreeMaker maker = workingCopy.getTreeMaker();
ModifiersTree modifiers = maker.Modifiers(
Collections.<Modifier>singleton(PUBLIC));
return maker.Class(
modifiers, //modifiers
name, //name
Collections.<TypeParameterTree>emptyList(),//type params
null, //extends
Collections.<ExpressionTree>emptyList(), //implements
members); //members
}
示例10: performRewrite
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
WorkingCopy wc = ctx.getWorkingCopy();
Tree.Kind k = ctx.getPath().getLeaf().getKind();
if (!TreeUtilities.CLASS_TREE_KINDS.contains(k)) {
// TODO: report
return;
}
ClassTree ct = (ClassTree)ctx.getPath().getLeaf();
ModifiersTree mt = ct.getModifiers();
Set<Modifier> mods = new HashSet<>(mt.getFlags());
mods.remove(Modifier.FINAL);
mods.add(Modifier.ABSTRACT);
ModifiersTree newMt = wc.getTreeMaker().Modifiers(mods, mt.getAnnotations());
wc.rewrite(mt, newMt);
}
示例11: createGetter
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
private MethodTree createGetter(ModifiersTree mods, TypeMirror valueType) {
StringBuilder getterName = GeneratorUtils.getCapitalizedName(config.getName());
getterName.insert(0, valueType.getKind() == TypeKind.BOOLEAN ? "is" : "get");
ReturnTree returnTree = make.Return(make.MethodInvocation(Collections.EMPTY_LIST, make.MemberSelect(make.Identifier(config.getName()), hasGet ? "get" : "getValue"), Collections.EMPTY_LIST));
BlockTree getterBody = make.Block(Collections.singletonList(returnTree), false);
Tree valueTree;
if (valueType.getKind() == TypeKind.DECLARED) {
valueTree = make.QualIdent(((DeclaredType) valueType).asElement());
} else if (valueType.getKind().isPrimitive()) {
valueTree = make.PrimitiveType(valueType.getKind());
} else {
valueTree = make.Identifier(valueType.toString());
}
MethodTree getter = make.Method(mods, getterName, valueTree, Collections.EMPTY_LIST, Collections.EMPTY_LIST, Collections.EMPTY_LIST, getterBody, null);
return getter;
}
示例12: createSetter
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
private MethodTree createSetter(ModifiersTree mods, TypeMirror valueType) {
StringBuilder getterName = GeneratorUtils.getCapitalizedName(config.getName());
getterName.insert(0, "set");
Tree valueTree;
if (valueType.getKind() == TypeKind.DECLARED) {
valueTree = make.QualIdent(((DeclaredType) valueType).asElement());
} else if (valueType.getKind().isPrimitive()) {
valueTree = make.PrimitiveType(valueType.getKind());
} else {
valueTree = make.Identifier(valueType.toString());
}
StatementTree statement = make.ExpressionStatement(make.MethodInvocation(Collections.EMPTY_LIST, make.MemberSelect(make.Identifier(config.getName()), hasGet ? "set" : "setValue"), Collections.singletonList(make.Identifier("value"))));
BlockTree getterBody = make.Block(Collections.singletonList(statement), false);
VariableTree var = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "value", valueTree, null);
MethodTree getter = make.Method(mods, getterName, make.PrimitiveType(TypeKind.VOID), Collections.EMPTY_LIST, Collections.singletonList(var), Collections.EMPTY_LIST, getterBody, null);
return getter;
}
示例13: performRewrite
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) {
WorkingCopy wc = ctx.getWorkingCopy();
TreePath tp = ctx.getPath();
VariableTree vt = (VariableTree) tp.getLeaf();
ModifiersTree mt = vt.getModifiers();
Set<Modifier> modifiers = EnumSet.noneOf(Modifier.class);
modifiers.addAll(mt.getFlags());
modifiers.add(Modifier.FINAL);
modifiers.add(Modifier.STATIC);
ModifiersTree newMod = wc.getTreeMaker().Modifiers(modifiers, mt.getAnnotations());
wc.rewrite(mt, newMod);
}
示例14: generate
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
public ClassTree generate() {
ClassTree modifiedClazz = getClassTree();
ModifiersTree methodModifiers = getTreeMaker().Modifiers(
getGenerationOptions().getModifiers(),
Collections.<AnnotationTree>emptyList()
);
FieldInfo em = getEntityManagerFieldInfo();
if (!em.isExisting()){
modifiedClazz = createEntityManager(Initialization.INIT);
}
MethodTree newMethod = getTreeMaker().Method(
methodModifiers,
computeMethodName(),
getTreeMaker().PrimitiveType(TypeKind.VOID),
Collections.<TypeParameterTree>emptyList(),
getParameterList(),
Collections.<ExpressionTree>emptyList(),
"{ " + getMethodBody(em)+ "}",
null
);
return getTreeMaker().addClassMember(modifiedClazz, importFQNs(newMethod));
}
开发者ID:apache,项目名称:incubator-netbeans,代码行数:28,代码来源:ApplicationManagedResourceTransactionInjectableInEJB.java
示例15: implement
import com.sun.source.tree.ModifiersTree; //导入依赖的package包/类
public ChangeInfo implement(){
CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>(){
public void cancel() {}
public void run(WorkingCopy workingCopy) throws Exception {
workingCopy.toPhase(JavaSource.Phase.RESOLVED);
TypeElement clazz = classHandle.resolve(workingCopy);
if (clazz != null){
ClassTree clazzTree = workingCopy.getTrees().getTree(clazz);
TreeMaker make = workingCopy.getTreeMaker();
Set<Modifier> flags = new HashSet<Modifier>(clazzTree.getModifiers().getFlags());
flags.add(Modifier.PUBLIC);
ModifiersTree newModifiers = make.Modifiers(flags, clazzTree.getModifiers().getAnnotations());
workingCopy.rewrite(clazzTree.getModifiers(), newModifiers);
}
}
};
JavaSource javaSource = JavaSource.forFileObject(fileObject);
try{
javaSource.runModificationTask(task).commit();
} catch (IOException e){
JPAProblemFinder.LOG.log(Level.SEVERE, e.getMessage(), e);
}
return null;
}