本文整理汇总了Java中com.github.javaparser.ast.body.TypeDeclaration类的典型用法代码示例。如果您正苦于以下问题:Java TypeDeclaration类的具体用法?Java TypeDeclaration怎么用?Java TypeDeclaration使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TypeDeclaration类属于com.github.javaparser.ast.body包,在下文中一共展示了TypeDeclaration类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: brewJava
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
public static void brewJava(File rFile, File outputDir, String packageName, String className)
throws Exception {
CompilationUnit compilationUnit = JavaParser.parse(rFile);
TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);
TypeSpec.Builder result =
TypeSpec.classBuilder(className).addModifiers(PUBLIC).addModifiers(FINAL);
for (Node node : resourceClass.getChildrenNodes()) {
if (node instanceof TypeDeclaration) {
addResourceType(Arrays.asList(SUPPORTED_TYPES), result, (TypeDeclaration) node);
}
}
JavaFile finalR = JavaFile.builder(packageName, result.build())
.addFileComment("Generated code from Butter Knife gradle plugin. Do not modify!")
.build();
finalR.writeTo(outputDir);
}
示例2: addResourceType
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
private static void addResourceType(List<String> supportedTypes, TypeSpec.Builder result,
TypeDeclaration node) {
if (!supportedTypes.contains(node.getName())) {
return;
}
String type = node.getName();
TypeSpec.Builder resourceType = TypeSpec.classBuilder(type).addModifiers(PUBLIC, STATIC, FINAL);
for (BodyDeclaration field : node.getMembers()) {
if (field instanceof FieldDeclaration) {
addResourceField(resourceType, ((FieldDeclaration) field).getVariables().get(0),
getSupportAnnotationClass(type));
}
}
result.addType(resourceType.build());
}
示例3: extractClassName
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
@Nullable
private static String extractClassName( String code, PrintStream err ) {
InputStream inputStream = new ByteArrayInputStream( code.getBytes( StandardCharsets.UTF_8 ) );
try {
CompilationUnit compilationUnit = JavaParser.parse( inputStream );
List<TypeDeclaration> types = compilationUnit.getTypes();
if ( types.size() == 1 ) {
String simpleType = types.get( 0 ).getName();
return Optional.ofNullable( compilationUnit.getPackage() )
.map( PackageDeclaration::getPackageName )
.map( it -> it + "." + simpleType )
.orElse( simpleType );
} else if ( types.size() == 0 ) {
err.println( "No class definition found" );
} else {
err.println( "Too many class definitions found. Only one class can be defined at a time." );
}
} catch ( ParseException e ) {
// ignore error, let the compiler provide an error message
return "Err";
}
return null;
}
示例4: doIsEquals
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
@Override
public boolean doIsEquals(CompilationUnit first, CompilationUnit second) {
// 检测包声明
if (!isEqualsUseMerger(first.getPackage(), second.getPackage())) return false;
// 检查公共类声明
for (TypeDeclaration outer : first.getTypes()) {
for (TypeDeclaration inner : second.getTypes()) {
if (ModifierSet.isPublic(outer.getModifiers()) && ModifierSet.isPublic(inner.getModifiers())) {
if (outer.getName().equals(inner.getName())) {
return true;
}
}
}
}
return false;
}
示例5: extractInformation
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
private void extractInformation(String filename) throws ClassParserException {
List<TypeDeclaration> types = cu.getTypes();
for (TypeDeclaration type : types) {
if (type instanceof ClassOrInterfaceDeclaration) {
clazz = (ClassOrInterfaceDeclaration) type;
}
List<BodyDeclaration> members = type.getMembers();
for (BodyDeclaration member : members) {
if (member instanceof FieldDeclaration) {
fields.add((FieldDeclaration) member);
}
else if (member instanceof ConstructorDeclaration) {
constructors.add((ConstructorDeclaration) member);
}
else if (member instanceof MethodDeclaration) {
methods.add((MethodDeclaration) member);
}
}
}
if (clazz == null) {
throw new ClassParserException("No toplevel type declaration found in " + filename + ".");
}
}
示例6: visit
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
@Override public void visit(final CompilationUnit n, final Object arg) {
printJavaComment(n.getComment(), arg);
if (n.getPackage() != null) {
n.getPackage().accept(this, arg);
}
if (!isNullOrEmpty(n.getImports())) {
for (final ImportDeclaration i : n.getImports()) {
i.accept(this, arg);
}
printer.printLn();
}
if (!isNullOrEmpty(n.getTypes())) {
for (final Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext();) {
i.next().accept(this, arg);
printer.printLn();
if (i.hasNext()) {
printer.printLn();
}
}
}
printOrphanCommentsEnding(n);
}
示例7: visit
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
@Override public void visit(final CompilationUnit n, final A arg) {
visitComment(n.getComment(), arg);
if (n.getPackage() != null) {
n.getPackage().accept(this, arg);
}
if (n.getImports() != null) {
for (final ImportDeclaration i : n.getImports()) {
i.accept(this, arg);
}
}
if (n.getTypes() != null) {
for (final TypeDeclaration typeDeclaration : n.getTypes()) {
typeDeclaration.accept(this, arg);
}
}
}
示例8: visit
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
@Override public void visit(final CompilationUnit n, final A arg) {
visitComment(n.getComment(), arg);
if (n.getPackage() != null) {
n.getPackage().accept(this, arg);
}
if (n.getImports() != null) {
for (final ImportDeclaration i : n.getImports()) {
i.accept(this, arg);
}
}
if (n.getTypes() != null) {
for (final TypeDeclaration typeDeclaration : n.getTypes()) {
typeDeclaration.accept(this, arg);
}
}
}
示例9: visit
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
@Override public Node visit(final CompilationUnit n, final A arg) {
if (n.getPackage() != null) {
n.setPackage((PackageDeclaration) n.getPackage().accept(this, arg));
}
final List<ImportDeclaration> imports = n.getImports();
if (imports != null) {
for (int i = 0; i < imports.size(); i++) {
imports.set(i, (ImportDeclaration) imports.get(i).accept(this, arg));
}
removeNulls(imports);
}
final List<TypeDeclaration> types = n.getTypes();
if (types != null) {
for (int i = 0; i < types.size(); i++) {
types.set(i, (TypeDeclaration) types.get(i).accept(this, arg));
}
removeNulls(types);
}
return n;
}
示例10: getMethodByPositionAndClassPosition
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
public static MethodDeclaration getMethodByPositionAndClassPosition(CompilationUnit compilationUnit,
int methodPosition, int classPosition) {
TypeDeclaration type = compilationUnit.getTypes().get(classPosition -1);
int memberCount = 0;
int methodCount = 0;
for(BodyDeclaration bodyDeclaration : type.getMembers()) {
if(bodyDeclaration instanceof MethodDeclaration){
if(methodCount == methodPosition -1) {
return (MethodDeclaration) type.getMembers().get(memberCount);
}
methodCount++;
}
memberCount++;
}
throw new IllegalArgumentException("Method not found at position " +methodPosition+ "in class " + classPosition);
}
示例11: locateTypeDeclaration
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
/**
* Searches a compilation unit and locates the declaration with the given
* type's simple name.
*
* @param compilationUnit to scan (required)
* @param javaType the target to locate (required)
* @return the located type declaration or null if it could not be found
*/
public static TypeDeclaration locateTypeDeclaration(
final com.github.javaparser.ast.CompilationUnit compilationUnit, final JavaType javaType) {
Validate.notNull(compilationUnit, "Compilation unit required");
Validate.notNull(javaType, "Java type to search for required");
if (compilationUnit.getTypes() == null) {
return null;
}
for (final TypeDeclaration candidate : compilationUnit.getTypes()) {
if (javaType.getSimpleTypeName().equals(candidate.getName())) {
// We have the required type declaration
return candidate;
}
}
return null;
}
示例12: visit
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
@Override public void visit(final CompilationUnit n, final A arg) {
visitComment(n.getComment(), arg);
if (n.getPackage() != null) {
n.getPackage().accept(this, arg);
}
if (n.getImports() != null) {
for (final ImportDeclaration i : n.getImports()) {
i.accept(this, arg);
}
}
if (n.getTypes() != null) {
for (final TypeDeclaration<?> typeDeclaration : n.getTypes()) {
typeDeclaration.accept(this, arg);
}
}
}
示例13: visit
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
@Override public Node visit(final CompilationUnit n, final A arg) {
visitComment(n, arg);
if (n.getPackage() != null) {
n.setPackage((PackageDeclaration) n.getPackage().accept(this, arg));
}
final List<ImportDeclaration> imports = n.getImports();
if (imports != null) {
for (int i = 0; i < imports.size(); i++) {
imports.set(i, (ImportDeclaration) imports.get(i).accept(this, arg));
}
removeNulls(imports);
}
final List<TypeDeclaration<?>> types = n.getTypes();
if (types != null) {
for (int i = 0; i < types.size(); i++) {
types.set(i, (TypeDeclaration<?>) types.get(i).accept(this, arg));
}
removeNulls(types);
}
return n;
}
示例14: visit
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
@Override public void visit(final CompilationUnit n, final Object arg) {
printJavaComment(n.getComment(), arg);
if (n.getPackage() != null) {
n.getPackage().accept(this, arg);
}
if (n.getImports() != null) {
for (final ImportDeclaration i : n.getImports()) {
i.accept(this, arg);
}
printer.printLn();
}
if (n.getTypes() != null) {
for (final Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext();) {
i.next().accept(this, arg);
printer.printLn();
if (i.hasNext()) {
printer.printLn();
}
}
}
printOrphanCommentsEnding(n);
}
示例15: brewJava
import com.github.javaparser.ast.body.TypeDeclaration; //导入依赖的package包/类
public static void brewJava(File rFile, File outputDir, String packageName, String className)
throws Exception {
CompilationUnit compilationUnit = JavaParser.parse(rFile);
TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);
TypeSpec.Builder result =
TypeSpec.classBuilder(className).addModifiers(PUBLIC).addModifiers(FINAL);
for (Node node : resourceClass.getChildNodes()) {
if (node instanceof ClassOrInterfaceDeclaration) {
addResourceType(Arrays.asList(SUPPORTED_TYPES), result, (ClassOrInterfaceDeclaration) node);
}
}
JavaFile finalR = JavaFile.builder(packageName, result.build())
.addFileComment("Generated code from Butter Knife gradle plugin. Do not modify!")
.build();
finalR.writeTo(outputDir);
}