本文整理汇总了Java中com.sun.source.tree.ClassTree类的典型用法代码示例。如果您正苦于以下问题:Java ClassTree类的具体用法?Java ClassTree怎么用?Java ClassTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ClassTree类属于com.sun.source.tree包,在下文中一共展示了ClassTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getClassTree
import com.sun.source.tree.ClassTree; //导入依赖的package包/类
public static ClassTree getClassTree(
TreeMaker make,
WorkingCopy workingCopy,
String name,
String superClass,
List<ExpressionTree> implementsList,
Set<Modifier> modifiers) {
Tree extendsTree = make.QualIdent(workingCopy.getElements().getTypeElement(superClass));
Map<String,String> params = new HashMap<String, String>();
params.put("param1", "String");
MethodTree mt = Common.createMethod(make, "method", params);
VariableTree vt = Common.createField(make, "variable", EnumSet.of(Modifier.PROTECTED), "double");
List<Tree> members = new ArrayList<Tree>();
members.add(mt);
members.add(vt);
members.add(make.Block(Collections.EMPTY_LIST, false));
ClassTree innerClass = make.Class(
make.Modifiers(modifiers),
name,
Collections.EMPTY_LIST,
extendsTree,
implementsList,
members);
return innerClass;
}
示例2: testGetterNamingConvention0
import com.sun.source.tree.ClassTree; //导入依赖的package包/类
public void testGetterNamingConvention0() throws Exception {//#165241
performTest("package test;\npublic class Test {\nprivate int eMai;\npublic Test(){\n}\n }\n", new GetterSetterTask(34, false), new Validator() {
public void validate(CompilationInfo info) {
ClassTree ct = (ClassTree) info.getCompilationUnit().getTypeDecls().get(0);
for (Tree member : ct.getMembers()) {
if (member.getKind() == Kind.METHOD) {
String name = ((MethodTree) member).getName().toString();
if (!name.equals("<init>")) {
assertEquals(name, "seteMai");
}
}
}
}
});
}
示例3: instantiateStrategy
import com.sun.source.tree.ClassTree; //导入依赖的package包/类
private EntityManagerGenerationStrategy instantiateStrategy(Class<? extends EntityManagerGenerationStrategy> strategy, WorkingCopy workingCopy,
TreeMaker make, ClassTree clazz, GenerationOptions options){
EntityManagerGenerationStrategy result = null;
try{
result = strategy.newInstance();
result.setClassTree(clazz);
result.setWorkingCopy(workingCopy);
result.setGenerationOptions(options);
result.setTreeMaker(make);
result.setPersistenceUnit(getPersistenceUnit());
} catch (IllegalAccessException iae){
throw new RuntimeException(iae); //TODO
} catch (InstantiationException ie){
throw new RuntimeException(ie); //TODO
}
return result;
}
示例4: run
import com.sun.source.tree.ClassTree; //导入依赖的package包/类
public void run(WorkingCopy workingCopy) throws Exception {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
for (Tree typeDeclaration : cut.getTypeDecls()){
if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDeclaration.getKind())){
ClassTree clazz = (ClassTree) typeDeclaration;
EntityManagerGenerationStrategySupport strategy =
(EntityManagerGenerationStrategySupport) getStrategy(workingCopy, make, clazz, new GenerationOptions());
doAsserts(strategy);
} else {
fail("No class found"); // should not happen
}
}
}
示例5: findTopClasses
import com.sun.source.tree.ClassTree; //导入依赖的package包/类
/**
*
* @return list of top classes, or an empty list of none were found
*/
static List<ClassTree> findTopClasses(
CompilationUnitTree compilationUnit,
TreeUtilities treeUtils) {
List<? extends Tree> typeDecls = compilationUnit.getTypeDecls();
if ((typeDecls == null) || typeDecls.isEmpty()) {
return Collections.<ClassTree>emptyList();
}
List<ClassTree> result = new ArrayList<ClassTree>(typeDecls.size());
for (Tree typeDecl : typeDecls) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) {
ClassTree clsTree = (ClassTree) typeDecl;
if (isTestable(clsTree, treeUtils)) {
result.add(clsTree);
}
}
}
return result;
}
示例6: findMainClass
import com.sun.source.tree.ClassTree; //导入依赖的package包/类
/**
* Finds a main class.
*
* @param compInfo defines scope in which the class is to be found
* @param className name of the class to be found
* @return the found class; or <code>null</code> if the class was not
* found (e.g. because of a broken source file)
*/
public static ClassTree findMainClass(final CompilationInfo compInfo) {
final String className = compInfo.getFileObject().getName();
CompilationUnitTree compUnitTree = compInfo.getCompilationUnit();
String shortClassName = getSimpleName(className);
for (Tree typeDecl : compUnitTree.getTypeDecls()) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) {
ClassTree clazz = (ClassTree) typeDecl;
if (clazz.getSimpleName().toString().equals(shortClassName)) {
return clazz;
}
}
}
return null;
}
示例7: visitClass
import com.sun.source.tree.ClassTree; //导入依赖的package包/类
@Override
public synchronized Void visitClass(ClassTree t, Void v) {
Element el = info.getTrees().getElement(getCurrentPath());
if (el == null) {
return null;
}
TypeElement te = (TypeElement) el;
List<VariableElement> fields = ElementFilter.fieldsIn(te.getEnclosedElements());
enumConsts = new LinkedList<String>();
for (VariableElement field : fields) {
if (field.getKind() == ElementKind.ENUM_CONSTANT) {
String name = field.getSimpleName().toString();
enumConsts.add(name);
}
}
return null;
}
示例8: composeNewTestClass
import com.sun.source.tree.ClassTree; //导入依赖的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
}
示例9: testVariableInitWrapped
import com.sun.source.tree.ClassTree; //导入依赖的package包/类
public void testVariableInitWrapped() throws Exception {
String code = "package hierbas.del.litoral;\n\n" +
"import java.util.concurrent.atomic.AtomicBoolean;\n\n" +
"public class Test {\n" +
" public void t() {\n" +
" new AtomicBoolean();\n" +
" }\n" +
"}\n";
runWrappingTest(code, 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);
MethodTree method = (MethodTree) clazz.getMembers().get(1);
ExpressionStatementTree init = (ExpressionStatementTree) method.getBody().getStatements().get(0);
VariableTree nue = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "ab", make.Type("java.util.concurrent.atomic.AtomicBoolean"), init.getExpression());
workingCopy.rewrite(init, nue);
}
}, FmtOptions.wrapAssignOps, WrapStyle.WRAP_IF_LONG.name());
}
示例10: composeNewTestClass
import com.sun.source.tree.ClassTree; //导入依赖的package包/类
/**
*/
protected ClassTree composeNewTestClass(WorkingCopy workingCopy,
String name,
List<? extends Tree> members) {
final TreeMaker maker = workingCopy.getTreeMaker();
ModifiersTree modifiers = maker.Modifiers(
Collections.<Modifier>singleton(PUBLIC));
Tree extendsClause = getClassIdentifierTree(TEST_CASE, workingCopy);
return maker.Class(
modifiers, //modifiers
name, //name
Collections.<TypeParameterTree>emptyList(),//type params
extendsClause, //extends
Collections.<ExpressionTree>emptyList(), //implements
members); //members
}
示例11: performArithmeticTest
import com.sun.source.tree.ClassTree; //导入依赖的package包/类
private void performArithmeticTest(String orig, String nue) throws Exception {
String code = replace("0");
prepareTest("Test.java", code);
ClassTree clazz = (ClassTree) info.getCompilationUnit().getTypeDecls().get(0);
VariableTree variable = (VariableTree) clazz.getMembers().get(1);
ExpressionTree init = variable.getInitializer();
TreePath tp = new TreePath(new TreePath(new TreePath(new TreePath(info.getCompilationUnit()), clazz), variable), init);
Fix fix = JavaFixUtilities.rewriteFix(info, "A", tp, orig, Collections.<String, TreePath>emptyMap(), Collections.<String, Collection<? extends TreePath>>emptyMap(), Collections.<String, String>emptyMap(), Collections.<String, TypeMirror>emptyMap(), Collections.<String, String>emptyMap());
fix.implement();
String golden = replace(nue);
String out = doc.getText(0, doc.getLength());
assertEquals(golden, out);
LifecycleManager.getDefault().saveAll();
}
示例12: visitAnnotationType
import com.sun.source.tree.ClassTree; //导入依赖的package包/类
public void visitAnnotationType(ClassTree node) {
sync(node);
builder.open(ZERO);
visitAndBreakModifiers(node.getModifiers(), Direction.VERTICAL, Optional.<BreakTag>absent());
builder.open(ZERO);
token("@");
token("interface");
builder.breakOp(" ");
visit(node.getSimpleName());
builder.close();
builder.close();
if (node.getMembers() == null) {
builder.open(plusFour);
token(";");
builder.close();
} else {
addBodyDeclarations(node.getMembers(), BracesOrNot.YES, FirstDeclarationsOrNot.YES);
}
builder.guessToken(";");
}
示例13: implement
import com.sun.source.tree.ClassTree; //导入依赖的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);
GenerationUtils genUtils = GenerationUtils.newInstance(workingCopy);
ClassTree modifiedClazz = genUtils.addImplementsClause(clazzTree, "java.io.Serializable"); // NOI18N
workingCopy.rewrite(clazzTree, modifiedClazz);
}
}
};
JavaSource javaSource = JavaSource.forFileObject(fileObject);
try{
javaSource.runModificationTask(task).commit();
} catch (IOException e){
JPAProblemFinder.LOG.log(Level.SEVERE, e.getMessage(), e);
}
return null;
}
示例14: createSetter
import com.sun.source.tree.ClassTree; //导入依赖的package包/类
/**
* Creates a setter method for a field.
*
* @param clazz the class to create the setter within
* @param field field to create setter for
* @return the setter method
* @since 0.20
*/
public MethodTree createSetter(ClassTree clazz, VariableTree field) {
assert clazz != null && field != null;
TreeMaker make = copy.getTreeMaker();
Set<Modifier> mods = EnumSet.of(Modifier.PUBLIC);
boolean isStatic = field.getModifiers().getFlags().contains(Modifier.STATIC);
if (isStatic)
mods.add(Modifier.STATIC);
CharSequence name = field.getName();
assert name.length() > 0;
CodeStyle cs = DiffContext.getCodeStyle(copy);
String propName = removeFieldPrefixSuffix(field, cs);
String setterName = CodeStyleUtils.computeSetterName(field.getName(), isStatic, cs);
String paramName = addParamPrefixSuffix(propName, cs);
List<VariableTree> params = Collections.singletonList(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), paramName, field.getType(), null));
BlockTree body = make.Block(Collections.singletonList(make.ExpressionStatement(make.Assignment(make.MemberSelect(isStatic? make.Identifier(clazz.getSimpleName()) : make.Identifier("this"), name), make.Identifier(paramName)))), false); //NOI18N
return make.Method(make.Modifiers(mods), setterName, make.Type(copy.getTypes().getNoType(TypeKind.VOID)), Collections.<TypeParameterTree>emptyList(), params, Collections.<ExpressionTree>emptyList(), body, null);
}
示例15: testEnsureNoArgConstructorMakesConstructorPublic
import com.sun.source.tree.ClassTree; //导入依赖的package包/类
public void testEnsureNoArgConstructorMakesConstructorPublic() throws Exception {
TestUtilities.copyStringToFileObject(testFO,
"package foo;" +
"public class TestClass {" +
" private TestClass() {" +
" }" +
"}");
runModificationTask(testFO, new Task<WorkingCopy>() {
public void run(WorkingCopy copy) throws Exception {
GenerationUtils genUtils = GenerationUtils.newInstance(copy);
ClassTree classTree = (ClassTree)copy.getCompilationUnit().getTypeDecls().get(0);
ClassTree newClassTree = genUtils.ensureNoArgConstructor(classTree);
copy.rewrite(classTree, newClassTree);
}
}).commit();
runUserActionTask(testFO, new Task<CompilationController>() {
public void run(CompilationController controller) throws Exception {
TypeElement typeElement = SourceUtils.getPublicTopLevelElement(controller);
assertTrue(SourceUtils.getNoArgConstructor(controller, typeElement).getModifiers().contains(Modifier.PUBLIC));
}
});
}