本文整理汇总了Java中japa.parser.ast.CompilationUnit.getTypes方法的典型用法代码示例。如果您正苦于以下问题:Java CompilationUnit.getTypes方法的具体用法?Java CompilationUnit.getTypes怎么用?Java CompilationUnit.getTypes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类japa.parser.ast.CompilationUnit
的用法示例。
在下文中一共展示了CompilationUnit.getTypes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: Fact
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public Fact(File file, CompilationUnit compilationUnit) throws FileNotFoundException {
if (compilationUnit.getPackage() != null)
packageName = compilationUnit.getPackage().getName().toString().replace("package", "");
comment = extractTopComment(file);
imports = new HashSet<String>();
if (compilationUnit.getImports() != null)
for (ImportDeclaration imp : compilationUnit.getImports()) {
String str = imp.getName().toString();
if (!imp.isAsterisk())
str = str.substring(0, str.lastIndexOf("."));
imports.add(str);
}
declarations = new ArrayList<Declaration>();
if (compilationUnit.getTypes() != null)
for (TypeDeclaration decl : compilationUnit.getTypes()) {
if (decl instanceof ClassOrInterfaceDeclaration)
declarations.add(new Declaration(decl));
}
}
示例2: locateTypeDeclaration
import japa.parser.ast.CompilationUnit; //导入方法依赖的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 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;
}
示例3: parseType
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
private JType parseType(String type, JCodeModel codeModel) {
final String text = "public class Ignored extends " + type + " {}";
try {
CompilationUnit compilationUnit = JavaParser.parse(
new ByteArrayInputStream(text.getBytes("UTF-8")), "UTF-8");
final List<TypeDeclaration> typeDeclarations = compilationUnit
.getTypes();
final TypeDeclaration typeDeclaration = typeDeclarations.get(0);
final ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
final List<ClassOrInterfaceType> _extended = classDeclaration
.getExtends();
final ClassOrInterfaceType classOrInterfaceType = _extended.get(0);
return classOrInterfaceType.accept(
this.typeToJTypeConvertingVisitor, codeModel);
} catch (ParseException pex) {
throw new IllegalArgumentException(
"Could not parse the type definition [" + type + "].", pex);
} catch (UnsupportedEncodingException uex) {
throw new UnsupportedOperationException(uex);
}
}
示例4: getNode
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public static Node getNode(CompilationUnit unit, String fqName) {
String pkg = getQualifiedName(unit.getPackage().getName());
if (!fqName.startsWith(pkg)) {
return null;
}
if (fqName.equals(pkg)) {
return unit.getPackage();
}
String name = fqName.substring(pkg.length() + 1);
for (TypeDeclaration typeDecl : unit.getTypes()) {
if (name.startsWith(typeDecl.getName())) {
if (name.length() == typeDecl.getName().length()) {
return typeDecl;
} else {
return getNode(typeDecl, name.substring(typeDecl.getName().length() + 1));
}
}
}
return null;
}
示例5: find
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
private static TypeDeclaration find( CompilationUnit compunit , String[ ] nestedTypeNames )
{
for( TypeDeclaration decl : compunit.getTypes( ) )
{
if( decl.getName( ).equals( nestedTypeNames[ 0 ] ) )
{
if( nestedTypeNames.length == 1 )
{
return decl;
}
else
{
return find( decl , nestedTypeNames , 1 );
}
}
}
return null;
}
示例6: visit
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public R visit(CompilationUnit n, A arg) {
if (n.getPackage() != null) {
n.getPackage().accept(this, arg);
}
if (n.getImports() != null) {
for (ImportDeclaration i : n.getImports()) {
i.accept(this, arg);
}
}
if (n.getTypes() != null) {
for (TypeDeclaration typeDeclaration : n.getTypes()) {
typeDeclaration.accept(this, arg);
}
}
return null;
}
示例7: visit
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public void visit(CompilationUnit n, Object arg) {
if (n.getPackage() != null) {
n.getPackage().accept(this, arg);
}
if (n.getImports() != null) {
for (ImportDeclaration i : n.getImports()) {
i.accept(this, arg);
}
printer.printLn();
}
if (n.getTypes() != null) {
for (Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext();) {
i.next().accept(this, arg);
printer.printLn();
if (i.hasNext()) {
printer.printLn();
}
}
}
}
示例8: visit
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public Node visit(CompilationUnit n, A arg) {
if (n.getPackage() != null) {
n.setPackage((PackageDeclaration) n.getPackage().accept(this, arg));
}
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);
}
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;
}
示例9: changeMethods
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
private static void changeMethods(CompilationUnit cu, String rootClass) {
addImport(cu, rootClass);
String rootClassShort = rootClass
.substring(rootClass.lastIndexOf(".") + 1);
List<TypeDeclaration> types = cu.getTypes();
for (TypeDeclaration type : types) {
List<BodyDeclaration> members = type.getMembers();
for (BodyDeclaration member : members) {
if (member instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) member;
if (isSetRootComponentMethod(method)) {
changeSetRootMethod(method, rootClassShort);
}
} else if (member instanceof FieldDeclaration) {
FieldDeclaration field = (FieldDeclaration) member;
if (isRootField(field)) {
changeRootField(field, rootClassShort);
}
}
}
}
}
示例10: getJavaType
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public final JavaType getJavaType(final String fileIdentifier) {
Validate.notBlank(fileIdentifier, "Compilation unit path required");
Validate.isTrue(new File(fileIdentifier).exists(),
"The file doesn't exist");
Validate.isTrue(new File(fileIdentifier).isFile(),
"The identifier doesn't represent a file");
try {
final File file = new File(fileIdentifier);
String typeContents = "";
try {
typeContents = FileUtils.readFileToString(file);
}
catch (IOException ignored) {
}
if (StringUtils.isBlank(typeContents)) {
return null;
}
final CompilationUnit compilationUnit = JavaParser
.parse(new ByteArrayInputStream(typeContents.getBytes()));
final String typeName = fileIdentifier.substring(
fileIdentifier.lastIndexOf(File.separator) + 1,
fileIdentifier.lastIndexOf("."));
for (final TypeDeclaration typeDeclaration : compilationUnit
.getTypes()) {
if (typeName.equals(typeDeclaration.getName())) {
return new JavaType(compilationUnit.getPackage().getName()
.getName()
+ "." + typeDeclaration.getName());
}
}
return null;
}
catch (final ParseException e) {
throw new IllegalStateException("Failed to parse " + fileIdentifier
+ " : " + e.getMessage());
}
}
示例11: testParse
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public void testParse() throws Exception {
final String text = "public class Dummy implements java.util.Comparator<java.lang.Integer>{}";
final CompilationUnit compilationUnit = JavaParser.parse(
new ByteArrayInputStream(text.getBytes("UTF-8")), "UTF-8");
final List<TypeDeclaration> typeDeclarations = compilationUnit
.getTypes();
assertEquals(1, typeDeclarations.size());
final TypeDeclaration typeDeclaration = typeDeclarations.get(0);
assertTrue(typeDeclaration instanceof ClassOrInterfaceDeclaration);
final ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
assertEquals("Dummy", classDeclaration.getName());
final List<ClassOrInterfaceType> implementedInterfaces = classDeclaration
.getImplements();
assertEquals(1, implementedInterfaces.size());
final ClassOrInterfaceType implementedInterface = implementedInterfaces
.get(0);
assertEquals("Comparator", implementedInterface.getName());
final List<Type> typeArgs = implementedInterface.getTypeArgs();
assertEquals(1, typeArgs.size());
final Type typeArg = typeArgs.get(0);
assertTrue(typeArg instanceof ReferenceType);
final ReferenceType referenceTypeArg = (ReferenceType) typeArg;
final Type referencedType = referenceTypeArg.getType();
assertTrue(referencedType instanceof ClassOrInterfaceType);
final ClassOrInterfaceType typeArgType = (ClassOrInterfaceType) referencedType;
assertEquals("Integer", typeArgType.getName());
}
示例12: writeBuilder
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
/**
* Finds the java file a generated builder belongs in, and attempts to insert the builder code, or replace the existing auto-generated types and methods if
* necessary.<br>
* <i>Requres JavaParser 1.0.8 or greater.</i><br>
* <br>
* This method is crazy. Finding and modifying the file is complicated, and this method makes a backup, but who knows what could go wrong. USE AT YOUR OWN
* RISK!
*
* @param builder
* a builder generated by {@code #generateBuilders(Class...)}.
* @throws Exception
* for any number of reasons...
*/
public static void writeBuilder( String builder ) throws Exception
{
CompilationUnit compunit = JavaParser.parse( new ByteArrayInputStream( builder.getBytes( ) ) );
for( TypeDeclaration typeDecl : compunit.getTypes( ) )
{
File javaFile = findJavaFile( typeDecl );
if( javaFile != null )
{
List<String> lines = insertBuilder( typeDecl , javaFile );
File backup = new File( javaFile + "_backup" );
File newFile = new File( javaFile + "_new" );
System.out.println( "Copying " + javaFile + " to " + backup );
copy( javaFile , backup );
System.out.println( "Writing " + newFile + "..." );
write( lines , newFile );
System.out.println( "Writing " + javaFile + "..." );
write( lines , javaFile );
System.out.println( "Deleting " + backup + "..." );
backup.delete( );
System.out.println( "Deleting " + newFile + "..." );
newFile.delete( );
}
}
}
示例13: visit
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public void visit(CompilationUnit n, A arg) {
if (n.getPackage() != null) {
n.getPackage().accept(this, arg);
}
if (n.getImports() != null) {
for (ImportDeclaration i : n.getImports()) {
i.accept(this, arg);
}
}
if (n.getTypes() != null) {
for (TypeDeclaration typeDeclaration : n.getTypes()) {
typeDeclaration.accept(this, arg);
}
}
}
示例14: addTypeDeclaration
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
/**
* Adds the given type declaration to the compilation unit. The list of
* types will be initialized if it is <code>null</code>.
*
* @param cu
* compilation unit
* @param type
* type declaration
*/
public static void addTypeDeclaration(CompilationUnit cu, TypeDeclaration type) {
List<TypeDeclaration> types = cu.getTypes();
if (types == null) {
types = new ArrayList<TypeDeclaration>();
cu.setTypes(types);
}
types.add(type);
}
示例15: cunstructShirinked
import japa.parser.ast.CompilationUnit; //导入方法依赖的package包/类
public static String cunstructShirinked(String original) throws ParseException {
CompilationUnit cu = null;
try {
cu = JavaParser.parse(new ByteArrayInputStream(original.getBytes("UTF-8")), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
for (TypeDeclaration td : cu.getTypes())
processType(td);
original = cu.toString();
original = shrink(original);
return original;
}